Skip to the content

Reading a database

SQL · Unit 1: First queries

Ann says she bought a Monitor on January 4. Where do you check that? A database keeps each kind of thing in its own table, and this one has a third for purchases.

Look for that date, written 2026-01-04 in the table: there are two rows, and neither says Ann or Monitor. There are only numbers.

SELECT * FROM orders;

Returns

idcustomer_idproduct_idquantityplaced
11122026-01-03
21312026-01-04
32252026-01-04
444102026-01-09
55112026-01-10
62322026-01-11
71212026-01-12
85432026-01-12
96512026-01-14
103722026-01-15
116712026-01-15
122612026-01-20

In this database, every table has an id column with a number that never repeats. It's a very common convention, because two customers can both be named Ann, and they'll never have the same id.

This store's Ann is customer 1.

SELECT id, name
FROM customers;

Returns

idname
1Ann
2Luke
3Chloë
4Mason
5Valerie
6Dylan
7Hannah
8Brian

That's why an order doesn't store names: it stores ids. customer_id says who bought and product_id says what.

Ann is customer 1 and the Monitor is product 3. Her January 4 purchase is order 2, and she was telling the truth.

Each column stores a single type of value: number, like price and stock, or text, like name and category.

The type decides what you can do. Numbers are for calculations, and text gets glued with ||. Dates, in SQLite, are stored as text.

SELECT name, price, stock
FROM products;

Returns

namepricestock
Keyboard45.512
Mouse19.930
Monitor1895
Cable5.250
Laptop8993
Tablet3207
Headphones59.915
Speaker740

You've asked for prices cut in half, stock doubled, and quantities times two. None of that got stored.

SELECT only reads. The result is a new table that's put together on the spot and then disappears. The real one never finds out.

Exercises in this lesson

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

  1. 1. Complete the code

    Which days had sales? Fill in the blank to see the date of each purchase.

    SELECT placed
    FROM ___;
  2. 2. Put the lines in order

    Today is inventory day. Put the query in order to see each product's name and how many are left. Two tables don't belong.

  3. 3. Find the bug

    Planning the shipments takes each person's name and city. SQL says there's no column called city. Which line has the error?

    SELECT name, city
    FROM products;
  4. 4. Complete the code

    Who placed each order? Fill in the blank to see each order's id next to the value that says whose it is.

    SELECT id, ___
    FROM orders;
  5. 5. Multiple choice

    In customers, Ann has id 1 and Luke has id 2. Order 12 has customer_id 2 and quantity 1. Whose is it?

  6. 6. Write the code

    What was each purchase for, and how many? Write a query that shows two columns for each order, product_id and then quantity.

    SELECT
    FROM orders;
  7. 7. Complete the code

    The supplier offers to double what's in the warehouse. Fill in the blank with the column it makes sense to double.

    SELECT name, ___ * 2
    FROM products;
  8. 8. Predict the output

    What does this query return?

    SELECT 7 + 3, '7' || '3';
  9. 9. Put the lines in order

    During two-for-one week, every order is doubled. Put the query in order. It should show each order's id and double its quantity. One line doesn't belong.

  10. 10. Multiple choice

    The Keyboard costs 45.5. Someone runs this query by mistake. How much does the Keyboard cost in the table afterward?

    SELECT name, price * 0
    FROM products;
Do this lesson

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

See all lessons →