SQL CHEAT SheetSQL Cheat Sheet: Essential Commands, Clauses, and Patterns for Data Analysts
Structured Query Language (SQL) is the backbone of data analysis, data engineering, and business intelligence. Whether you're querying a relational database, building ETL pipelines, or exploring datasets in Power BI, SQL is the language that lets you interact with data efficiently and precisely.
This cheat sheet is designed to be your quick-access reference for the most commonly used SQL commands, clauses, functions, and patterns. It’s ideal for beginners who want to learn fast, and for experienced analysts who need a refresher or a compact guide.
📌 What Is SQL?
SQL (Structured Query Language) is a domain-specific language used to manage and manipulate relational databases. It allows users to:
Retrieve data (
SELECT
)Insert new records (
INSERT
)Update existing data (
UPDATE
)Delete records (
DELETE
)Create and modify tables (
CREATE
,ALTER
)Control access and permissions (
GRANT
,REVOKE
)
SQL is supported by most relational database systems, including:
Microsoft SQL Server
PostgreSQL
MySQL
Oracle
SQLite
🔍 Basic SQL Syntax
SELECT column1, column2
FROM table_name
WHERE condition
ORDER BY column1 ASC;
🧠 Key Clauses
Clause | Purpose | Example |
---|---|---|
SELECT | Choose columns | SELECT name, age FROM users |
FROM | Specify table | FROM employees |
WHERE | Filter rows | WHERE age > 30 |
ORDER BY | Sort results | ORDER BY salary DESC |
GROUP BY | Aggregate rows | GROUP BY department |
HAVING | Filter aggregated results | HAVING COUNT(*) > 5 |
LIMIT | Restrict number of rows (MySQL, PG) | LIMIT 10 |
TOP | Restrict rows (SQL Server) | SELECT TOP 5 * FROM sales |
🧩 Filtering Data
WHERE Clause
SELECT * FROM orders
WHERE status = 'Shipped' AND total > 100;
BETWEEN
SELECT * FROM payments
WHERE amount BETWEEN 50 AND 500;
IN
SELECT * FROM customers
WHERE country IN ('Italy', 'France', 'Germany');
LIKE
SELECT * FROM products
WHERE name LIKE 'A%'; -- Starts with A
🔄 Joining Tables
INNER JOIN
SELECT orders.id, customers.name
FROM orders
INNER JOIN customers ON orders.customer_id = customers.id;
LEFT JOIN
SELECT employees.name, departments.name
FROM employees
LEFT JOIN departments ON employees.dept_id = departments.id;
RIGHT JOIN
SELECT students.name, courses.title
FROM students
RIGHT JOIN courses ON students.course_id = courses.id;
FULL OUTER JOIN
SELECT *
FROM table1
FULL OUTER JOIN table2 ON table1.id = table2.id;
📊 Aggregation Functions
Function | Description | Example |
---|---|---|
COUNT() | Number of rows | COUNT(*) |
SUM() | Total of values | SUM(sales) |
AVG() | Average value | AVG(score) |
MIN() | Minimum value | MIN(price) |
MAX() | Maximum value | MAX(age) |
GROUP BY
SELECT department, COUNT(*) AS total_employees
FROM employees
GROUP BY department;
🧠 Subqueries
Inline Subquery
SELECT name
FROM customers
WHERE id IN (SELECT customer_id FROM orders WHERE total > 500);
Correlated Subquery
SELECT name
FROM employees e
WHERE salary > (SELECT AVG(salary) FROM employees WHERE dept_id = e.dept_id);
🛠️ Table Management
CREATE TABLE
CREATE TABLE users (
id INT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100),
created_at DATE
);
ALTER TABLE
ALTER TABLE users ADD COLUMN phone VARCHAR(20);
DROP TABLE
DROP TABLE old_data;
🔐 Data Manipulation
INSERT
INSERT INTO products (name, price)
VALUES ('Laptop', 1200);
UPDATE
UPDATE orders
SET status = 'Delivered'
WHERE id = 101;
DELETE
DELETE FROM users
WHERE last_login < '2023-01-01';
🧠 Window Functions
Window functions allow you to perform calculations across a set of rows related to the current row.
ROW_NUMBER
SELECT name, ROW_NUMBER() OVER (ORDER BY salary DESC) AS rank
FROM employees;
RANK
SELECT name, RANK() OVER (PARTITION BY dept_id ORDER BY salary DESC) AS dept_rank
FROM employees;
LEAD / LAG
SELECT name, salary,
LAG(salary) OVER (ORDER BY salary) AS previous_salary
FROM employees;
🧠 Common Table Expressions (CTEs)
CTEs improve readability and allow recursive queries.
WITH HighValueOrders AS (
SELECT * FROM orders WHERE total > 1000
)
SELECT * FROM HighValueOrders WHERE status = 'Shipped';
🧠 Useful Patterns
Find Duplicates
SELECT email, COUNT(*)
FROM users
GROUP BY email
HAVING COUNT(*) > 1;
Top N per Group
SELECT *
FROM (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY category ORDER BY price DESC) AS rank
FROM products
) ranked
WHERE rank <= 3;
🧠 Performance Tips
Use indexes on columns used in
WHERE
,JOIN
, andORDER BY
Avoid
SELECT *
in production queriesUse
EXPLAIN
orQUERY PLAN
to analyze performanceLimit subqueries and nested joins when possible
Normalize data but denormalize for reporting when needed
Final Thoughts
SQL is a foundational skill for any data professional. Whether you're building dashboards, writing ETL scripts, or exploring datasets, knowing how to write clean, efficient SQL queries is essential.
This cheat sheet is meant to be a living reference—bookmark it, expand it, and adapt it to your database environment. With practice, you’ll be able to write queries that are not only correct, but elegant and optimized.
Join the conversation