Skip to content
Sign inGet all access
Chapter 2·12 minFree

Querying One Table

Filtering, sorting and shaping rows.

Every query here runs against the product table from chapter 1.

WHERE

A condition per row. = is comparison, never assignment:

SELECT name, price FROM product WHERE price < 70;
Output+----------+-------+| name | price |+----------+-------+| Keyboard | 49.99 || Mouse | 19.95 || Webcam | 64.75 |+----------+-------+

The operators are =, <>, <, <=, >, >= — <> is the portable “not equal”. Text compares with the same ones:

SELECT name, category FROM product WHERE category = 'input';
Output+----------+----------+| name | category |+----------+----------+| Keyboard | input || Mouse | input |+----------+----------+

AND, OR and parentheses

AND binds tighter than OR, which is where the bugs come from:

SELECT name FROM product
WHERE category = 'input' AND price < 30 OR stock = 0;
Output+---------+| name |+---------+| Mouse || Headset |+---------+

Headset is not an input device. It matched because the query reads as (category = 'input' AND price < 30) OR (stock = 0):

SELECT name FROM product
WHERE category = 'input' AND (price < 30 OR stock = 0);
Output+-------+| name |+-------+| Mouse |+-------+

BETWEEN, IN, LIKE

Three shorthands. BETWEEN is inclusive at both ends:

SELECT name, price FROM product WHERE price BETWEEN 40 AND 90;
Output+----------+-------+| name | price |+----------+-------+| Keyboard | 49.99 || Headset | 79.25 || Webcam | 64.75 |+----------+-------+

IN replaces a chain of OR:

SELECT name FROM product WHERE category IN ('input', 'audio');
Output+----------+| name |+----------+| Keyboard || Mouse || Headset |+----------+

LIKE matches a pattern: % is any run of characters, _ exactly one:

SELECT name FROM product WHERE name LIKE 'M%';
Output+---------+| name |+---------+| Mouse || Monitor |+---------+

ORDER BY

Several columns, each with its own direction:

SELECT name, category, price FROM product
ORDER BY category ASC, price DESC;
Output+----------+----------+--------+| name | category | price |+----------+----------+--------+| Headset | audio | 79.25 || Monitor | display | 189.95 || Keyboard | input | 49.99 || Mouse | input | 19.95 || Webcam | video | 64.75 |+----------+----------+--------+

ASC is the default. The two input rows are ordered by price descending, because category tied first.

LIMIT and OFFSET

LIMIT caps the rows, OFFSET skips some first:

SELECT name, price FROM product ORDER BY price DESC LIMIT 2;
Output+---------+--------+| name | price |+---------+--------+| Monitor | 189.95 || Headset | 79.25 |+---------+--------+

Page two is the same query with an offset:

SELECT name, price FROM product
ORDER BY price DESC LIMIT 2 OFFSET 2;
Output+----------+-------+| name | price |+----------+-------+| Webcam | 64.75 || Keyboard | 49.99 |+----------+-------+

DISTINCT

Drops duplicate rows from the result:

SELECT DISTINCT category FROM product ORDER BY category;
Output+----------+| category |+----------+| audio || display || input || video |+----------+

The ORDER BY is not decoration. Without it the four rows come back in a different order on Postgres than on the other two — verified.

Column aliases

AS renames a column in the result:

SELECT name AS product_name, stock AS in_stock
FROM product WHERE id <= 2;
Output+--------------+-----------+| product_name | in_stock |+--------------+-----------+| Keyboard | 12 || Mouse | 40 |+--------------+-----------+

Arithmetic

The usual operators, and they apply per row:

SELECT name, price, price * 2 AS total
FROM product WHERE id = 1
UNION ALL
SELECT name, price, price FROM product WHERE id = 5;
Output+----------+-------+-------+| name | price | total |+----------+-------+-------+| Keyboard | 49.99 | 99.98 || Webcam | 64.75 | 64.75 |+----------+-------+-------+

Division is where the engines stop agreeing: two integers divide to an integer on Postgres and SQLite, and to a decimal on MySQL.

terminal
SELECT 7 / 2;
Outputsqlite SELECT 7 / 2; -> 3postgres SELECT 7 / 2; -> 3mysql SELECT 7 / 2; -> 3.5000

CASE

An if/else that produces a value, usable anywhere an expression is:

SELECT name, price,
       CASE WHEN price > 100 THEN 'high'
            WHEN price > 50  THEN 'mid'
            ELSE 'low'
       END AS band
FROM product
ORDER BY id;
Output+----------+--------+------+| name | price | band |+----------+--------+------+| Keyboard | 49.99 | low || Mouse | 19.95 | low || Monitor | 189.95 | high || Headset | 79.25 | mid || Webcam | 64.75 | mid |+----------+--------+------+

The first matching branch wins, so order the conditions from most to least specific. Without an ELSE an unmatched row gets NULL.

It also works in ORDER BY, which is how you sort by something other than the value:

SELECT name, category FROM product
ORDER BY CASE category WHEN 'input' THEN 1 ELSE 2 END, name;
Output+----------+----------+| name | category |+----------+----------+| Keyboard | input || Mouse | input || Headset | audio || Monitor | display || Webcam | video |+----------+----------+

Joining text

|| looks like the obvious operator and is the chapter’s worst trap:

terminal
SELECT 'a' || 'b';
Outputsqlite SELECT 'a' || 'b'; -> abpostgres SELECT 'a' || 'b'; -> abmysql SELECT 'a' || 'b'; -> 0

MySQL reads || as a logical OR, so it returns 0 — a wrong answer, not an error. CONCAT() works on all three:

SELECT CONCAT(name, ' - ', category) AS label
FROM product WHERE id <= 2;
Output+------------------+| label |+------------------+| Keyboard - input || Mouse - input |+------------------+
Tables