My name is Mason Ellwood, and I’m currently working on Flatiron School’s Online Full Stack Web Development Program. Each week, I’ll be writing about my experience, what I’m learning, and tips on learning to code.
In the lessons covered this week, we dived into basic SQL queries. A query in the simplest terms refers to something that answers a question. If you think of queries in this sense, then they become very simple. A query in a SQL statement retrieves data from your database.
Simple examples of queries have been used in the previous post, so I will move on to a little more complex examples.
In this post please follow along. In your terminal navigate to a newly created folder that will hold your new SQLite database.
Once that has been completed create your database using the prompt below.
Once you have completed this, now we will add values to our newly formed columns.
Now we will format our values to make them look a little more pleasing and easier to read.
.header on: This outputs the name of each column
.mode column: This enables column mode
Remember if you would like to check the status of your columns at any time run (SELECT * FROM cats)
On your own, add some more cats so we have some data to work with.
First we will use the ORDER BY modifier. This allows us to order the table rows and return by a certain SELECT statement.
As a default, ORDER BY is by ascending order. If we want to specify which order it returns by you can use ASC “ascending” or DESC “descending”, which is shown above.
Using the LIMIT modifier determines the number of records you want from a database.
In the above example, this should return just the first, oldest cat from the list.
In the next post we will build on the example above and modify it even further. SQL is a great way to modify existing data that is given to the user or programmer. So bear with me if this begins to get confusing, in the next post we will get into aggregate functions.