SELECT, WHERE, and Boolean Logic
SELECT chooses the columns and expressions to return. WHERE filters rows using predicates. Use AND, OR, and NOT deliberately, with parentheses when precedence might be unclear. IS NULL checks nullness; = NULL does not work as an ordinary equality test because NULL represents an unknown or missing value.
Avoid SELECT * in stable application queries when only a few columns are needed. Selecting explicit columns reduces data transfer and makes dependencies clearer, especially when schemas evolve.
Filter products
SELECT id, name, price
FROM products
WHERE in_stock = TRUE
AND price >= 100
AND name IS NOT NULL
ORDER BY price DESC;💡 The query returns only the selected columns for in-stock products priced at least 100, ordered from highest to lowest price.
Key Takeaways
- WHERE filters rows before final result presentation.
- Use IS NULL and IS NOT NULL for null checks.
- Explicit column lists make application queries clearer.
Knowledge Check
How should SQL test whether a column is null?