Gomaa Intelligence.Data Hub
عربي

Foundations › SQL › Cheat sheet

Cheat sheet

SQL 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.

SELECT * FROM customers WHERE email IS NULL;

Testing for NULL.

Sorting and limiting

SELECT name, price FROM products ORDER BY price DESC LIMIT 5;

Top 5.

SELECT DISTINCT city FROM customers;

Distinct values.

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.

SELECT city FROM customers
UNION
SELECT 'Online';

Combine without duplicates.

NULL and CASE

SELECT name, COALESCE(email, 'no email') FROM customers;

Replacing NULL.

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.

SELECT UPPER(name), LENGTH(name), name || ' ' || city FROM customers;

Text.

Execution order

FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT

The 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.

Get your data analyzed by an expert ↗