Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Asking for the rows you need

After this lesson: I can ask a database for the rows I need.

55 min lesson10 commandsMySQL and phpMyAdmin lab with a sample shop database to practise SQL

Which laptops are out of warranty?

The support desk of a firm is asked a simple question. Which laptops were bought before 2023, and who has them?

In a spreadsheet you would scroll and squint. A database answers in a moment.

You only have to ask the question in a way it understands.

By the end of this lesson

Note

I can ask a database for the rows I need.

Key words

WordWhat it meansExample
Querya question you ask a database in SQL
Clauseone part of a query, started by a keywordWHERE
Conditiona test that a row passes or failsprice < 30
Resultthe rows that come back from a query
Wildcarda symbol in a pattern that stands for any text%
NULLthe mark for a missing value

The cells students fill in are left blank in this preview.

WHERE keeps the rows that pass

A query starts with SELECT, which names the columns. FROM names the table.

WHERE adds a condition. A row stays in the result only if it passes. Join conditions with AND and OR.

Text goes in single quotes. Numbers do not.

Run the two setup lines first. You did this in the last lesson, and it does no harm to repeat it.

Accept the server's certificate

Command
printf '[client]\nssl-verify-server-cert=0\n' > ~/.my.cnf

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Make a short name for the shop

Command
alias sql='mariadb -h db -u student -pstudent shop -t -e'

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Match a piece of text

Command
sql "SELECT first_name, city FROM customers WHERE country = 'France' ORDER BY city, first_name"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Two conditions together

Command
sql "SELECT name, price FROM products WHERE category_id = 4 AND price < 30 ORDER BY price"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Worked example: the tests you can use

TestMeansExample
= <>Equal, not equalcountry = 'Spain'
< <= > >=Smaller or largerprice < 30
BETWEENInside a range, ends includedstock BETWEEN 5 AND 10
LIKEMatches a pattern, % is any textname LIKE '%Set%'
IS NULLThe value is missingphone IS NULL

The cells students fill in are left blank in this preview.

Find names with a pattern

Command
sql "SELECT name FROM products WHERE name LIKE '%Set%' ORDER BY name"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Stock from 5 to 10

Command
sql "SELECT name, stock FROM products WHERE stock BETWEEN 5 AND 10 ORDER BY stock, name"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

NULL is missing, not zero

Some customers have no phone number. The column holds NULL: a mark that says nothing is known.

NULL is not equal to anything, not even to NULL. So = NULL never matches a row.

Ask with IS NULL instead. Run both and compare.

Count customers with no phone

Command
sql "SELECT COUNT(*) AS no_phone FROM customers WHERE phone IS NULL"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

The same question, written wrongly

Command
sql "SELECT COUNT(*) AS no_phone FROM customers WHERE phone = NULL"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A common mistake

Warning

Many people think phone = NULL finds the missing phones. It finds none, and it does not warn you. A missing value is not equal to anything. Always write IS NULL or IS NOT NULL.

ORDER BY and LIMIT shape the result

A table has no fixed order. If the order matters, say so with ORDER BY. DESC puts the largest first.

LIMIT 3 keeps the first three rows of the result. OFFSET 2 skips two rows first.

Use LIMIT with ORDER BY. Without an order, "the first three" means nothing.

The three cheapest products

Command
sql "SELECT name, price FROM products ORDER BY price LIMIT 3"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Skip two, then take two

Command
sql "SELECT name, price FROM products ORDER BY price DESC LIMIT 2 OFFSET 2"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Worked example: how a query is read

StepClauseWhat the database does
1FROMPicks the table
2WHEREThrows away rows that fail the test
3SELECTKeeps only the columns you named
4ORDER BYPuts the rows in order
5LIMITCuts the list to the size you asked

The cells students fill in are left blank in this preview.

Try it: laptops by department

Code: sql
-- Laptops in Legal or Sales.
SELECT tag, model, dept FROM laptops
WHERE dept = 'Legal' OR dept = 'Sales';
-- Change it to find laptops bought in 2024 or later.

Try it: the dearest laptops

Code: sql
-- The three dearest laptops, dearest first.
SELECT tag, price FROM laptops
ORDER BY price DESC
LIMIT 3;
-- Change DESC to ASC and run it again.

Going further

A shop's website lists products five to a page, cheapest first. Write the query for page three with LIMIT and OFFSET.

Two products in the shop cost 19.99. Explain why such a pair could swap places between two runs of the query. Then add a second column to the ORDER BY so the order is always the same.

About this lab and its source: MySQL and phpMyAdmin lab with a sample shop database to practise SQL

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.