SQL Fundamentals
What every other page in this section assumes you already know.
Beginner
SELECT name, price
FROM products
WHERE price > 10 AND category = 'electronics'
ORDER BY price DESC
LIMIT 10;
ORDER BY without it, row order is unspecified — not "insertion order" or anything else predictable. If order matters to your application, you need an explicit ORDER BY, full stop.TRUE, FALSE, or UNKNOWN. Any comparison involving NULL evaluates to UNKNOWN, and WHERE only keeps rows where the condition is TRUE — UNKNOWN rows are silently dropped.SELECT * FROM users WHERE age = NULL; -- returns ZERO rows, always — not an error, just always false
SELECT * FROM users WHERE age IS NULL; -- the correct way to test for NULL
SELECT * FROM users WHERE age <> NULL; -- also always UNKNOWN, same trap
NULL hiding in a comparison.| Operator | Notes |
|---|---|
BETWEEN a AND b | inclusive on both ends — equivalent to col >= a AND col <= b |
IN (1, 2, 3) | shorthand for chained OR equality checks; also accepts a subquery |
LIKE 'A%' | % matches any sequence, _ matches exactly one character; case-sensitive |
ILIKE 'a%' | PostgreSQL-specific case-insensitive LIKE |
NOT binds tighter than AND, which binds tighter than OR. When mixing them, parenthesize — relying on precedence rules you have to look up is how bugs happen.| Category | Examples |
|---|---|
| Numeric | integer, decimal/numeric, float |
| String | char(n) fixed-width, varchar(n)/text variable-width |
| Date/time | date, time, timestamp |
| Boolean | boolean (not all engines have a native one — MySQL historically faked it with tinyint) |
jsonb, arrays, ranges), is covered in Data Types & Schema Design.