Foundations › SQL › Cheat sheet
Cheat sheetSQL Cheat Sheet
All the core SQL commands on one page, ready to copy and try.
Query structure
SELECT col1, col2
FROM table
WHERE condition
GROUP BY col1
HAVING aggregate_condition
ORDER BY col1 DESC
LIMIT 10;The full template in writing order.
Filtering
SELECT * FROM products WHERE price BETWEEN 100 AND 500;Between two values (inclusive).
SELECT * FROM customers WHERE city IN ('Cairo','Giza') AND name LIKE 'A%';A value list and a text pattern.
Sorting and limiting
Aggregation
SELECT COUNT(*), COUNT(email), COUNT(DISTINCT city), SUM(price), AVG(price), MIN(price), MAX(price)
FROM customers, products LIMIT 1;All the functions (for shape only).
SELECT city, COUNT(*) AS n
FROM customers
GROUP BY city
HAVING COUNT(*) > 20;Grouping with group filtering.
Joins
SELECT o.id, c.name
FROM orders o
JOIN customers c ON c.id = o.customer_id;INNER JOIN.
SELECT c.name, o.id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.id IS NULL;Customers with no orders.
SELECT e.name, m.name AS manager
FROM employees e
LEFT JOIN employees m ON m.id = e.manager_id;SELF JOIN.
Subqueries and set operations
SELECT name FROM products
WHERE price > (SELECT AVG(price) FROM products);Scalar subquery.
SELECT name FROM products p
WHERE NOT EXISTS (SELECT 1 FROM order_items oi WHERE oi.product_id = p.id);Does not exist.
NULL and CASE
SELECT name,
CASE WHEN price < 200 THEN 'Budget' ELSE 'Premium' END AS tier
FROM products;Conditional bucketing.
Dates and text (SQLite)
SELECT strftime('%Y-%m', order_date) AS month, COUNT(*)
FROM orders GROUP BY month;Group by month.
SELECT date('2026-09-30','-30 day'), julianday('2026-09-30') - julianday('2026-01-01');Subtracting days and the difference.
Execution order
FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMITThe database runs in this order regardless of writing order.
Want an expert to do it for you?
Gomaa Intelligence turns your data into clear reports and decisions. Tell us your problem and we will reply.