Filtering rows with WHERE and operators
So far you have asked for all the rows of a table. In real life you almost never want everything: you want the customers from Madrid, the expensive products, the orders of the last month. The WHERE clause filters: only the rows for which the condition is true reach the result. It is written after FROM and before any other clause. Mastering WHERE is mastering half of SQL, so we will dedicate two lessons to it.
-- Customers who live in Madrid
SELECT customer_id, name, city
FROM customers
WHERE city = 'Madrid';
-- Products more expensive than 50
SELECT name, price
FROM products
WHERE price > 50;Comparison operators
- = equal (a single sign, not ==)
- <> or != different
- < less than, > greater than
- <= less or equal, >= greater or equal
With numbers the comparison is the expected one. With text, keep in mind that in PostgreSQL uppercase and lowercase are different: 'madrid' is not equal to 'Madrid'. In MySQL, with the usual configuration, text comparison is not case sensitive, so the same code can give different results in different engines. Dates are written as text in year-month-day format ('2024-03-01') and compared in chronological order.
-- Delivered orders from March 2024 on
SELECT order_id, order_date, status
FROM orders
WHERE status = 'delivered'
AND order_date >= '2024-03-01';
-- Everything that is NOT stationery
SELECT name, category
FROM products
WHERE category <> 'Stationery';Combining conditions: AND, OR, NOT
AND requires both conditions to hold; OR needs only one; NOT reverses the result. The most frequent trap is precedence: AND is evaluated before OR, just like multiplication before addition. If you mix both without parentheses, the result can surprise you. When in doubt, use parentheses to make the intention clear; they cost nothing and prevent silent errors, the kind that give plausible but wrong results.
-- Without parentheses: Home products, OR (computers that cost more than 500)
SELECT name, category, price
FROM products
WHERE category = 'Home' OR category = 'Computers' AND price > 500;
-- With parentheses: (Home or Computers) AND price over 200
SELECT name, category, price
FROM products
WHERE (category = 'Home' OR category = 'Computers') AND price > 200;Compare the two results. The first query returns the Home products (the lamp and the chair) whatever their price, plus the expensive laptop. The second requires a price above 200 from the whole group, so the lamp (24.90) and the chair (189) are left out and only the laptop and the monitor remain. Same structure, different meaning: only the parentheses change. Get used to reading conditions aloud to check that they say what you mean.
A method tip for writing filters: build the condition little by little. Start with a single comparison, check how many rows it returns and add the next condition afterwards. If the result suddenly empties or grows too much, you will know exactly which condition caused it. This habit is much more effective than writing a huge condition at once and not understanding why it fails. Remember also that each condition is evaluated row by row, independently, and that only the true rows reach the final result.
✎ 我的笔记
仅保存在此浏览器中。登录后可保存到账号。0/5000 字符