Pcornel
Browse topics
Options
Recent
Favorites
Databases · Technical reference

SELECT and Filtering

Direct answer

SELECT retrieves rows: choose columns, FROM tables, filter with WHERE, sort with ORDER BY, and limit with LIMIT/TOP.

When to use this

Use it for reading data correctly and efficiently.

First 30 seconds

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.

Operational verification

FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
MariaDB / MySQL / PostgreSQL / Microsoft SQL Server concepts
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformMariaDB / MySQL / PostgreSQL / Microsoft SQL Server concepts
Scope / prerequisiteUse it for reading data correctly and efficiently.
What next?2 suggested next steps
Evidence0
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

2 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

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.

Key points

Select only needed columns. Filters on indexed columns without wrapping them in functions can use indexes. NULL needs IS NULL, not = NULL.

Key checks
  • IS NULL — first check for this topic
  • psql -c "SELECT now(), version();" — PostgreSQL reachable and version
  • mysql -e "SELECT NOW(), VERSION();" — MariaDB/MySQL reachable and version
  • sqlcmd -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

ExpressionResult when col is NULL
col = NULLNULL → row excluded
col <> 'x'NULL → row excluded (surprises people)
col IS NULLtrue
col IS DISTINCT FROM 'x'true (NULL-safe comparison)
col NOT IN (SELECT …) with a NULL in the subqueryNo rows at all — use NOT EXISTS

Writing filters an index can use (sargable)

AvoidPrefer
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 = 42WHERE 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).

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.

Common mistakes

  • SELECT * in application code.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Terminology layer

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.

Aliases that resolve here
select and filteringsql select wheresql null handling
Confirmation

Confirm action

Continue?