SQL Cheat Sheet

Core SQL syntax and patterns for querying, filtering, sorting, joining, grouping, and modifying data.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all
## SELECT Basics
Select all columns
SELECT *
FROM users;

# Return all columns from a table.

Select specific columns
SELECT id, email, created_at
FROM users;

# Return only the columns you need.

Select distinct values
SELECT DISTINCT country
FROM customers;

# Remove duplicate rows for the selected expression set.

Use column aliases
SELECT first_name AS given_name, last_name AS family_name
FROM users;

# Rename output columns in query results.

Select literals and expressions
SELECT order_id, subtotal, tax, subtotal + tax AS total
FROM orders;

# Project computed values directly in the result set.

## Filtering Rows
Filter with equality
SELECT *
FROM users
WHERE status = 'active';

# Return rows matching an exact value.

Use comparison operators
SELECT *
FROM invoices
WHERE amount > 1000
  AND due_date < CURRENT_DATE;

# Compare numeric or date values.

Filter with IN
SELECT *
FROM products
WHERE category IN ('books', 'games', 'toys');

# Match any value from a list.

Filter with BETWEEN
SELECT *
FROM events
WHERE created_at BETWEEN DATE "2025-01-01" AND DATE "2025-01-31";

# Filter a value within an inclusive range.

Pattern matching with LIKE
SELECT *
FROM customers
WHERE email LIKE '%@example.com';

# Find rows using wildcard patterns.

Test for NULL
SELECT *
FROM tasks
WHERE completed_at IS NULL;

# Filter rows with missing values.

## Sorting and Limiting
Sort rows
SELECT id, created_at
FROM orders
ORDER BY created_at DESC, id ASC;

# Order query results ascending or descending.

Limit returned rows
SELECT *
FROM logs
ORDER BY created_at DESC
LIMIT 50;

# Return only the first N rows.

Paginate with LIMIT and OFFSET
SELECT *
FROM posts
ORDER BY published_at DESC
LIMIT 20 OFFSET 40;

# Skip rows and return a page-sized result set.

Fetch first rows
SELECT *
FROM employees
ORDER BY salary DESC
FETCH FIRST 10 ROWS ONLY;

# Use standard SQL row limiting syntax.

## Joins
Inner join two tables
SELECT o.order_id, c.name
FROM orders o
JOIN customers c ON c.customer_id = o.customer_id;

# Return rows with matches in both tables.

Left join
SELECT c.customer_id, o.order_id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id;

# Keep all rows from the left table and match rows from the right table.

Right join
SELECT e.employee_id, d.department_name
FROM employees e
RIGHT JOIN departments d ON d.department_id = e.department_id;

# Keep all rows from the right table.

Full outer join
SELECT a.id AS a_id, b.id AS b_id
FROM a
FULL OUTER JOIN b ON b.id = a.id;

# Return matched and unmatched rows from both tables.

Self join
SELECT e.employee_id, e.name, m.name AS manager_name
FROM employees e
LEFT JOIN employees m ON m.employee_id = e.manager_id;

# Join a table to itself.

Cross join
SELECT c.color, s.size
FROM colors c
CROSS JOIN sizes s;

# Return the Cartesian product of two sets.

## Aggregation and Grouping
Count rows
SELECT COUNT(*) AS row_count
FROM users;

# Return total row count.

Group by a column
SELECT status, COUNT(*) AS total
FROM orders
GROUP BY status;

# Aggregate values per group.

Group by multiple columns
SELECT country, status, COUNT(*) AS total
FROM customers
GROUP BY country, status;

# Aggregate by more than one dimension.

Filter groups with HAVING
SELECT customer_id, COUNT(*) AS order_count
FROM orders
GROUP BY customer_id
HAVING COUNT(*) >= 5;

# Apply conditions after aggregation.

Use common aggregate functions
SELECT
  COUNT(*) AS total_rows,
  SUM(amount) AS total_amount,
  AVG(amount) AS avg_amount,
  MIN(amount) AS min_amount,
  MAX(amount) AS max_amount
FROM payments;

# Calculate summary metrics.

Recommended next

No recommendations yet.