Skip to the content

Your first query

SQL · Unit 1: First queries

Who are this store's customers? The answer is stored in a table named customers, and this line asks for it. Each row is a person and each column is one fact about them.

See the NULL in Chloë's city? It's not a city: it's how the table marks a value it doesn't have.

SELECT * FROM customers;

Returns

idnamecityage
1AnnDenver34
2LukeAustin27
3ChloëNULL41
4MasonTampa19
5ValerieDenver52
6DylanBoston27
7HannahAustinNULL
8BrianDenver38

That line is a query, and it reads almost like a sentence. SELECT says what you want to see, and the * means every column.

FROM says which table. And what does the store sell? Same query, different table.

SELECT * FROM products;

Returns

idnamecategorypricestock
1KeyboardAccessories45.512
2MouseAccessories19.930
3MonitorDisplays1895
4CableAccessories5.250
5LaptopDevices8993
6TabletDevices3207
7HeadphonesAudio59.915
8SpeakerAudio740

SQL doesn't care about line breaks, which is why it needs the ; to know where a query ends.

You do care. With SELECT on one line and FROM on another it's easier to read, and when something breaks you'll know which line to check.

SELECT *
FROM customers;

Returns

idnamecityage
1AnnDenver34
2LukeAustin27
3ChloëNULL41
4MasonTampa19
5ValerieDenver52
6DylanBoston27
7HannahAustinNULL
8BrianDenver38

When a query is written wrong, SQL stops and tells you the word where it got lost.

That word is almost never the mistake: it's the first one that no longer fit. Your mistake is usually right before it, so read the message and then look back.

SQL is about fifty years old, and almost every database speaks it: MySQL, PostgreSQL, SQLite, and many more.

Here your queries run on SQLite, inside your phone. Each database has its quirks, and we'll tell you when something changes from one to another.

Exercises in this lesson

You do them in the app, which checks them on the spot and explains why.

  1. 1. Multiple choice

    In the customers table, what is a row?

  2. 2. Multiple choice

    Tomorrow a new customer, Emily, comes in and the store adds her. What does the customers table gain?

  3. 3. Complete the code

    Someone new at the store wants to see everything it knows about each customer. Fill in the blank.

    SELECT ___ FROM customers;
  4. 4. Complete the code

    The store also records every purchase, in the orders table. Fill in the blank to see it.

    SELECT * ___ orders;
  5. 5. Put the lines in order

    What does the store sell? Put in order the query that answers it. One line doesn't belong.

  6. 6. Multiple choice

    Which query is written correctly?

  7. 7. Find the bug

    Here are two queries, one to see who buys and one to see what's for sale. SQL complains about the second SELECT. Which line is the real error on?

    SELECT *
    FROM customers
    SELECT *
    FROM products;
  8. 8. Put the lines in order

    Put the two queries in order to see first who buys and then what each person has ordered. One line doesn't belong.

  9. 9. Multiple choice

    You're learning SQL here, with SQLite. Tomorrow a job has you using PostgreSQL. Will what you know still help?

  10. 10. Write the code

    The store wants to go over everything it sells. Write the query that shows the whole products table, with all its columns.

    SELECT
Do this lesson

It opens in your browser. It is free and you do not need an account to start.

See all lessons →