Use it for reading data correctly and efficiently.
SELECT and Filtering
SELECT retrieves rows: choose columns, FROM tables, filter with WHERE, sort with ORDER BY, and limit with LIMIT/TOP.
1. Write the query with explicit columns.
2. Check NULL handling (IS NULL).
3. EXPLAIN for large tables.
4. Limit result size.
5. Validate counts.
FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 How to verify Verify boundary
Compare the result count with an independent check. Add ORDER BY for any output that users or code depend on. Review EXPLAIN for full scans on large tables.
Step 2 Common failures Narrow the fault
WHERE col = NULL returns nothing. SELECT * pulls unused columns and breaks when columns change. OFFSET pagination gets slow on deep pages; keyset pagination (WHERE id > last_id) scales better.
Select only needed columns. Filters on indexed columns without wrapping them in functions can use indexes. NULL needs IS NULL, not = NULL.
IS NULL— first check for this topicpsql -c "SELECT now(), version();"— PostgreSQL reachable and versionmysql -e "SELECT NOW(), VERSION();"— MariaDB/MySQL reachable and versionsqlcmd -S <server> -Q "SELECT @@VERSION"— SQL Server reachable and version
ReferenceWhat it is1 section · depth on demand
How it works
The database plans how to read the tables, filters rows with the WHERE predicates, then projects the selected columns. Functions on indexed columns in WHERE (for example lower(email)) prevent normal index use unless an expression index exists.
ReferenceImportant terms & boundaries3 sections · depth on demand
Examples
SELECT id, name, created_at
FROM customers
WHERE country = 'DK' AND created_at >= '2026-01-01'
ORDER BY created_at DESC
LIMIT 50;
- Select only needed columns (avoid
SELECT *in applications). WHERE col IS NULL, not= NULL.- Functions on indexed columns (
WHERE date(created_at) = ...) prevent index use — compare ranges instead. - Use parameters, never string concatenation (SQL injection).
In depth
Logical order of evaluation
FROM/JOIN → WHERE → GROUP BY → HAVING → SELECT (expressions, window functions) → DISTINCT → ORDER BY → LIMIT/OFFSET
This explains why a column alias from SELECT cannot be used in WHERE (it does not exist yet) but can be used in ORDER BY.
NULL in filters
| Expression | Result when col is NULL |
|---|---|
col = NULL | NULL → row excluded |
col <> 'x' | NULL → row excluded (surprises people) |
col IS NULL | true |
col IS DISTINCT FROM 'x' | true (NULL-safe comparison) |
col NOT IN (SELECT …) with a NULL in the subquery | No rows at all — use NOT EXISTS |
Writing filters an index can use (sargable)
| Avoid | Prefer |
|---|---|
WHERE date(created_at) = '2026-11-27' | WHERE created_at >= '2026-11-27' AND created_at < '2026-11-28' |
WHERE lower(email) = $1 (no expression index) | Expression index on lower(email), or a case-insensitive collation |
WHERE id + 0 = 42 | WHERE id = 42 |
WHERE name LIKE '%son' | Full-text or trigram index if suffix search is needed |
Keyset pagination instead of deep OFFSET
SELECT id, created_at, total FROM orders
WHERE (created_at, id) < ($last_created_at, $last_id)
ORDER BY created_at DESC, id DESC
LIMIT 50;
OFFSET 100000 still reads and discards 100,000 rows; the keyset form jumps straight to the next page through an index on (created_at, id).
Common mistakes
SELECT *in application code.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Terms & aliases
Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.