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;The operators are =, <>, <, <=, >, >= — <> is the portable “not equal”. Text
compares with the same ones:
SELECT name, category FROM product WHERE category = '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;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);BETWEEN, IN, LIKE
Three shorthands. BETWEEN is inclusive at both ends:
SELECT name, price FROM product WHERE price BETWEEN 40 AND 90;IN replaces a chain of OR:
SELECT name FROM product WHERE category IN ('input', 'audio');LIKE matches a pattern: % is any run of characters, _ exactly one:
SELECT name FROM product WHERE name LIKE 'M%';ORDER BY
Several columns, each with its own direction:
SELECT name, category, price FROM product
ORDER BY category ASC, price DESC;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;Page two is the same query with an offset:
SELECT name, price FROM product
ORDER BY price DESC LIMIT 2 OFFSET 2;DISTINCT
Drops duplicate rows from the result:
SELECT DISTINCT category FROM product ORDER BY category;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;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;Division is where the engines stop agreeing: two integers divide to an integer on Postgres and SQLite, and to a decimal on MySQL.
SELECT 7 / 2;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;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;Joining text
|| looks like the obvious operator and is the chapter’s worst trap:
SELECT 'a' || 'b';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;