Skip to content
KALANBLON
Show contents

Chapter 1 of 3 · 15 min

SELECT and WHERE: choosing your data

By the end of this chapter, you'll be able to

  • Explain what a table, a row and a column are
  • Read columns with SELECT … FROM
  • Filter rows with WHERE
  • Sort and limit a result with ORDER BY and LIMIT

Tables, rows, columns

A database stores information in tables, like very strict spreadsheet sheets. Each row describes one item (here, a sale) and each column one characteristic (the product, the city…). All the values in a column have the same type: text, an integer, a decimal number.

SQL (Structured Query Language) is the language used to query these tables. A query is a question written in SQL. In this course, our database contains a single table, sales, with 10 example rows (data made up for the exercise).

SELECT and WHERE

SELECT chooses the columns, FROM names the table, WHERE keeps only the rows that meet a condition:

SELECT product, quantity
FROM sales
WHERE city = 'Lima'
ORDER BY quantity DESC;
product | quantity
Quinoa | 20
Avocado | 12
Mango | 8

ORDER BY quantity DESC sorts from largest to smallest (ASC, the default order, sorts from smallest to largest). The semicolon ends the query.

Finally, ORDER BY sorts the result by decreasing quantity.

Step 6 of 6
Text description of the animation
  1. To answer, the database reads the sales table row by row.
  2. Row 1: the city is Dakar, the row is left out.
  3. Row 2: Lima, we keep it, with only the requested columns.
  4. Row 4: Lima again, we keep it.
  5. Row 5: kept. The other rows are in Dakar or Hanoi.
  6. Finally, ORDER BY sorts the result by decreasing quantity.

A few useful filtering tools:

  • comparisons: =, <> (different), <, <=, >, >=;
  • combinations: AND, OR, NOT;
  • LIKE 'M%': text that starts with M (% stands for any sequence of characters);
  • LIMIT 3: keeps only the first 3 rows of the result.
SELECT product, city, price
FROM sales
WHERE category = 'fruit' AND price >= 1
ORDER BY price;
product | city | price
Avocado | Lima | 1.5
Avocado | Dakar | 1.8

Your turn

Mini-lab: the big salesSQLite

Result

Click “Run”: the SQL engine (about 1 MB) will load in your browser the first time.

A fresh SQLite database is created on every run, in your browser · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor

Worked exercise

Solved exercise: the drinks of Hanoi

Write a query that displays the product and price of the drinks sold in Hanoi, from cheapest to most expensive.

Hint 1
There are two conditions to combine with AND: the category and the city.
Hint 2
To sort by increasing price, ORDER BY price is enough (ASC is the default order).
Show the solution and common mistakes
SELECT product, price
FROM sales
WHERE category = 'drink' AND city = 'Hanoi'
ORDER BY price;
product | price
Tea | 2.5
Coffee | 4

Common mistakes:

  • Writing WHERE category = 'drink' OR city = 'Hanoi': you get all the drinks and all the sales in Hanoi, rice included.
  • Writing 'hanoi' in lower case: text comparison is exact, and 'hanoi' finds nothing.

Summary

  1. 1A table stores rows (the items) and columns (their characteristics).
  2. 2SELECT chooses the columns, FROM the table, WHERE filters the rows.
  3. 3ORDER BY sorts (ASC or DESC) and LIMIT keeps the first rows.
  4. 4Text is written between single quotes, numbers without.

Your progress is saved on this device. Create a free account to keep your progress on all your devices.