ADVERT
๐๏ธ SQL Cheatsheet
SQL command reference for SELECT queries, filtering, joins, aggregation, schema changes, and performance tips.
SQL reference with dialect-aware snippets, in-page search, and a quick query builder for common SELECT workflows.
Query Builder
Generate a clean SELECT template and copy it into your editor.
SELECT id, customer_id, total FROM orders
WHERE status = 'paid'
ORDER BY created_at DESC
LIMIT 50;SELECT Essentials
Query patterns for projections and sorting.
| Query | Purpose | Notes | Copy |
|---|---|---|---|
SELECT id, email FROM users ORDER BY created_at DESC LIMIT 50; | Sample data | Limit previews and avoid SELECT * in production endpoints. | |
SELECT DISTINCT country FROM orders; | Unique values | DISTINCT applies to the full set of selected columns. | |
SELECT * FROM events WHERE occurred_at >= NOW() - INTERVAL '7 days'; | Date filtering | Relative date syntax varies by dialect. |
Joins and Aggregations
Combine entities and summarize metrics.
| Query | Purpose | Notes | Copy |
|---|---|---|---|
SELECT o.id, c.name FROM orders o JOIN customers c ON c.id = o.customer_id; | Inner join | Returns rows where both sides match the ON condition. | |
SELECT p.id, c.id FROM posts p LEFT JOIN comments c ON c.post_id = p.id; | Left join | Retains parent rows when child rows are missing. | |
SELECT city, COUNT(*) AS total FROM users GROUP BY city HAVING COUNT(*) > 100; | Group and HAVING | Use HAVING for aggregate filters after GROUP BY. |
Schema and Write Patterns
DDL and mutation snippets used in migrations and services.
| Query | Purpose | Notes | Copy |
|---|---|---|---|
CREATE TABLE accounts (
id BIGSERIAL PRIMARY KEY,
email TEXT UNIQUE NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW()
); | Create table | Define defaults and constraints up front. | |
INSERT INTO inventory (sku, qty) VALUES ($1, $2)
ON CONFLICT (sku) DO UPDATE SET qty = EXCLUDED.qty; | Upsert | Conflict syntax is dialect-specific. | |
BEGIN;
UPDATE accounts SET balance = balance - 20 WHERE id = 1;
COMMIT; | Transaction | Protect multi-step writes from partial failures. |
ADVERT
ADVERT