Create table
Define columns, constraints, and default values.
CREATE TABLE accounts (
id SERIAL PRIMARY KEY,
email TEXT UNIQUE NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW()
);ADVERT
SQL command reference for SELECT queries, filtering, joins, aggregation, schema changes, and performance tips.
SQL query patterns that power dashboards, reporting, and migrations. Copy the essentials for filtering, joining, aggregating, and managing schema.
Build reliable projections and calculations.
| Query | Purpose | Notes |
|---|---|---|
SELECT * FROM customers LIMIT 50; | Sample data | Use LIMIT for fast previews; replace * with named columns in production. |
SELECT name, email FROM users ORDER BY created_at DESC; | Sorted projection | ORDER BY supports ASC/DESC and multi-column priority. |
SELECT DISTINCT country FROM orders; | Unique values | DISTINCT applies to the full set of selected columns. |
SELECT NOW() AS server_time; | Built-in functions | Popular helpers: NOW(), CURRENT_DATE, LOWER(), UPPER(), LENGTH(). |
Target precise records with WHERE clauses.
| Query | Purpose | Notes |
|---|---|---|
SELECT * FROM orders WHERE status = 'paid'; | Equality filter | Use parameterized queries to avoid SQL injection. |
SELECT * FROM orders WHERE total BETWEEN 50 AND 200; | Range filter | BETWEEN is inclusive; combine with decimals for price bands. |
SELECT * FROM users WHERE email LIKE '%@toolhop.app'; | Pattern search | Escape % or _ when matching literal characters. |
SELECT * FROM events WHERE occurred_at >= NOW() - INTERVAL '7 days'; | Relative time | Intervals vary by database (INTERVAL, DATEADD, DATE_SUB). |
Combine tables and summarize metrics.
| Query | Purpose | Notes |
|---|---|---|
SELECT * FROM orders o JOIN customers c ON c.id = o.customer_id; | Inner join | Return rows where both tables match. |
SELECT * FROM posts p LEFT JOIN comments c ON c.post_id = p.id; | Left join | Return all posts even when comments are missing. |
SELECT city, COUNT(*) FROM users GROUP BY city; | Aggregation | Columns in SELECT must be aggregated or appear in GROUP BY. |
SELECT city, COUNT(*) FROM users GROUP BY city HAVING COUNT(*) > 100; | Aggregation filter | HAVING filters aggregated results after GROUP BY. |
DDL commands you reach for during migrations.
Define columns, constraints, and default values.
CREATE TABLE accounts (
id SERIAL PRIMARY KEY,
email TEXT UNIQUE NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW()
);Add/remove columns or constraints with ALTER.
ALTER TABLE accounts ADD COLUMN last_login TIMESTAMPTZ;Add indexes to columns used in joins or WHERE filters.
CREATE INDEX idx_orders_customer_id ON orders (customer_id);UPSERT prevents duplicate keys and keeps counts fresh.
INSERT INTO inventory (sku, qty) VALUES ($1, $2)
ON CONFLICT (sku) DO UPDATE SET qty = excluded.qty;Keep queries fast and resource-friendly.
Use EXPLAIN (ANALYZE, BUFFERS) SELECT ... to inspect query cost.
Paginate with LIMIT/OFFSET or keyset pagination for deep scrolls.
Select only necessary columns to reduce network and cache pressure.
INSERT ... VALUES (),(),() avoids chattiness.
BEGIN; ... COMMIT; ensures integrity across multiple statements.
ADVERT
ADVERT