Basic SQL Queries

Overview

In this lesson, we'll cover how to write SQL queries to retrieve and add specific data to SQL database tables.

Objectives

  1. Define a query as an SQL statement that retrieves data from a database

  2. Use the ORDER BYmodifier to order tables by specific SELECT statements

  3. Use the ASC and DESC modifier to sort queries in ascending and descending orders

  4. Use the LIMIT modifier to determine the number of records to retrieve from a dataset

  5. Use the BETWEEN modifier to retrieve a specific data set between two ranges

  6. Use the NULL data type keyword to insert new records into a table

  7. Use the COUNT function to count the number of records that meet a certain condition

  8. Use the GROUP BY function to group your results according to the values in a given column

What is a SQL Query?

The term "query" refers to any SQL statement that retrieves data from your database. In fact, we've already written a number of SQL queries using basic SELECT statements. We've already seen how to retrieve single units of data, or rows, with queries like these:

To select all of the rows from a cats table:

To select only rows representing data meeting certain conditions:

What if, however, we wanted to select the oldest cat? Or all of the cats that don't currently belong to an owner? Or all of the cats with short names?

Data storage isn't very useful if we can't manipulate, view, and analyze that data. Luckily for us, SQL is actually a powerful tool for doing just that.

In this exercise, we'll walk through executing a handful of common and handy SQL queries.

Code Along: SQL Queries

Creating our Database

In this code along, we'll be creating a cats table in a pets_database.db. So, let's navigate to our terminal and get started.

First let's create our pets_database by running the following command.

Now that we have a database, let's create our cats table along with id, name, age, breed, and owner_id columns.

Good work. Let's quit the SQL interface by typing: .quit. You'll be returned to the shell prompt. Now, output the list of files (ls on Linux-based machines and dir on Windows-based machines) in the terminal and see what just happened. A new file should appear called pets_database.db! This is the binary representation of the database. You can think of this like a .jpg file. It won't open up in a text editor, but it does open up in the image viewer app. It is the same way for .db files. They won't open in your editor, but they can be read by the appropriate database engine.

Let's add some cats to our cats table to make this more interesting:

Let's check out our cats table now:

This should return:

Top-Tip: You can format the output of your select statements with a few helpful options:

Run the first two commands and then execute the above SELECT statement instead and you should see something like this:

Much better.

ORDER BY

The first query modifier we'll explore is ORDER BY. This modifier allows us to order the table rows returned by a certain SELECT statement. Here's a boilerplate SELECT statement that uses ORDER BY:

Let's select our cats and order them by age:

This should return the following:

When using ORDER BY, the default is to order in ascending order. If we want to specify though, we can use ASC for "ascending" or DESC for "descending." Let's try to select all of our cats and sort them by age in descending order.

This should return

LIMIT

What if we want the oldest cat? If we want to select extremes from a database table––for example, the employee with the highest paycheck or the patient with the most recent appointment––we can use ORDER BY in conjunction with LIMIT.

LIMIT is used to determine the number of records you want to return from a dataset. For example:

This part of the statement: SELECT * FROM cats ORDER BY age DESC returns all of the cats in order from oldest to youngest. Setting a LIMIT of 1 returns just the first, i.e. oldest, cat on the list.

Execute the above statement in your terminal and you should see:

Let's get the two oldest cats:

Execute that statement and you should see:

BETWEEN

As we've already established, being able to sort and select specific data sets is important. Continuing on with our example, let's say we urgently need to select all of the cats whose age is between 1 and 3. To create such a query, we can use BETWEEN. Here's an boilerplate SELECT statement using BETWEEN:

Let's try it out on our cats table:

This should return:

NULL

Let's say the administrator of our Pets Database has found a new cat. This kitty doesn't have a name yet, but should be added to our database right away. We can add data with missing values using the NULL keyword.

Let's insert our new cat into the database. Our abandoned kitty has a breed, but no name or age as of yet:

Now, if we look at our cats data with SELECT * FROM cats;, we should see:

We can even select the mysterious, nameless kitty with the following query:

This should return the following:

COUNT

Now, we'll talk about a SQL aggregate function, COUNT.

SQL aggregate functions are SQL statements that retrieve minimum and maximum values from a column, sum values in a column, get the average of a column's values, or count a number of records that meet certain conditions. You can learn more about these SQL aggregators here and here.

For now, we'll just focus on COUNT. COUNT will count the number of records that meet certain condition. Here's a standard SQL query using COUNT:

Let's try it out and count the number of cats who have an owner_id of 1:

This should return:

GROUP BY

Lastly, we'll talk about the handy aggregate function GROUP BY. Like its name suggests, it groups your results by a given column.

Let's take our table of cats

Here, we can see at a glance that there are three tabby cats and one of every other breed — but what if we had a larger database where we couldn't just tally up the number of cats grouped by breed? That's where — you guessed it! — GROUP BY comes in handy.

This should return

GROUP BY is a great function for aggregating results into different segments — you can even use it on multiple columns!

Note on SELECT

We are now familiar with this syntax:

However, you may not know that this can be written like this as well:

Both return:

SQLite allows us to explicitly state the tableName.columnName we want to select. This is particularly useful when we want data from two different tables.

Imagine we have another table called dogs with a column for the dog names:

If we want to get the names of all the dogs and cats, we can no longer run a query with just the column name. SELECT name FROM cats,dogs; will return Error: ambiguous column name: name.

Instead, we must explicitly follow the tableName.columnName syntax.

You may see this in the future. Don't let it trip you up.

View Basic SQL Queries on Learn.co and start learning to code for free.

View Basic SQL Queries on Learn.co and start learning to code for free.

Last updated