SQL SELECT
Query rows and columns from tables with filtering and sorting
Overview
SELECT retrieves data from one or more tables. Specify columns (or * for all), filter with WHERE, sort with ORDER BY, and limit results. It is the foundation of read operations in relational databases.
Syntax / Usage
-- Basic select
SELECT id, name, email
FROM users
WHERE active = true
ORDER BY created_at DESC
LIMIT 10;
-- Distinct values
SELECT DISTINCT country FROM customers;
-- Expressions and aliases
SELECT
name,
price * quantity AS line_total
FROM order_items;
-- Pagination
SELECT * FROM posts
ORDER BY id
LIMIT 20 OFFSET 40;
Examples
Find recent signups:
SELECT id, email, created_at
FROM users
WHERE created_at >= NOW() - INTERVAL '7 days'
ORDER BY created_at DESC;
Search with pattern matching:
SELECT title, slug
FROM articles
WHERE title ILIKE '%react%'
AND status = 'published';
Common Mistakes
SELECT *in production queries—fetch only needed columns- Missing
ORDER BYwhen order matters—results can be non-deterministic - Forgetting quotes around string literals (
WHERE status = 'active') - Using
LIMITwithoutORDER BYfor paginated feeds
See Also
where-clause joins group-by crud