SQL CHEAT SheetSQL Cheat Sheet: Essential Commands, Clauses, and Patterns for Data Analysts

Your ultimate SQL cheat sheet: essential commands, joins, filters, aggregations, window functions, and performance tips 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

sql
SELECT column1, column2
FROM table_name
WHERE condition
ORDER BY column1 ASC;

🧠 Key Clauses

ClausePurposeExample
SELECTChoose columnsSELECT name, age FROM users
FROMSpecify tableFROM employees
WHEREFilter rowsWHERE age > 30
ORDER BYSort resultsORDER BY salary DESC
GROUP BYAggregate rowsGROUP BY department
HAVINGFilter aggregated resultsHAVING COUNT(*) > 5
LIMITRestrict number of rows (MySQL, PG)LIMIT 10
TOPRestrict rows (SQL Server)SELECT TOP 5 * FROM sales

🧩 Filtering Data

WHERE Clause

sql
SELECT * FROM orders
WHERE status = 'Shipped' AND total > 100;

BETWEEN

sql
SELECT * FROM payments
WHERE amount BETWEEN 50 AND 500;

IN

sql
SELECT * FROM customers
WHERE country IN ('Italy', 'France', 'Germany');

LIKE

sql
SELECT * FROM products
WHERE name LIKE 'A%'; -- Starts with A

🔄 Joining Tables

INNER JOIN

sql
SELECT orders.id, customers.name
FROM orders
INNER JOIN customers ON orders.customer_id = customers.id;

LEFT JOIN

sql
SELECT employees.name, departments.name
FROM employees
LEFT JOIN departments ON employees.dept_id = departments.id;

RIGHT JOIN

sql
SELECT students.name, courses.title
FROM students
RIGHT JOIN courses ON students.course_id = courses.id;

FULL OUTER JOIN

sql
SELECT *
FROM table1
FULL OUTER JOIN table2 ON table1.id = table2.id;

📊 Aggregation Functions

FunctionDescriptionExample
COUNT()Number of rowsCOUNT(*)
SUM()Total of valuesSUM(sales)
AVG()Average valueAVG(score)
MIN()Minimum valueMIN(price)
MAX()Maximum valueMAX(age)

GROUP BY

sql
SELECT department, COUNT(*) AS total_employees
FROM employees
GROUP BY department;

🧠 Subqueries

Inline Subquery

sql
SELECT name
FROM customers
WHERE id IN (SELECT customer_id FROM orders WHERE total > 500);

Correlated Subquery

sql
SELECT name
FROM employees e
WHERE salary > (SELECT AVG(salary) FROM employees WHERE dept_id = e.dept_id);

🛠️ Table Management

CREATE TABLE

sql
CREATE TABLE users (
    id INT PRIMARY KEY,
    name VARCHAR(100),
    email VARCHAR(100),
    created_at DATE
);

ALTER TABLE

sql
ALTER TABLE users ADD COLUMN phone VARCHAR(20);

DROP TABLE

sql
DROP TABLE old_data;

🔐 Data Manipulation

INSERT

sql
INSERT INTO products (name, price)
VALUES ('Laptop', 1200);

UPDATE

sql
UPDATE orders
SET status = 'Delivered'
WHERE id = 101;

DELETE

sql
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

sql
SELECT name, ROW_NUMBER() OVER (ORDER BY salary DESC) AS rank
FROM employees;

RANK

sql
SELECT name, RANK() OVER (PARTITION BY dept_id ORDER BY salary DESC) AS dept_rank
FROM employees;

LEAD / LAG

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

sql
WITH HighValueOrders AS (
    SELECT * FROM orders WHERE total > 1000
)
SELECT * FROM HighValueOrders WHERE status = 'Shipped';

🧠 Useful Patterns

Find Duplicates

sql
SELECT email, COUNT(*)
FROM users
GROUP BY email
HAVING COUNT(*) > 1;

Top N per Group

sql
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, and ORDER BY

  • Avoid SELECT * in production queries

  • Use EXPLAIN or QUERY PLAN to analyze performance

  • Limit 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.

Welcome to my blog—a space dedicated to Business Intelligence, Data Analysis, and IT Project Management. As a Project Manager with hands-on experience in data-driven solutions, I share insights, case studies, and practical tools to help professionals turn data into decisions. My goal is to build a knowledge hub for those who value clarity, efficiency, and continuous learning. Whether you're exploring BI tools, managing agile projects, or optimizing workflows, you'll find content designed to inform, inspire, and support your growth.
NextGen Digital... Welcome to WhatsApp chat
Howdy! How can we help you today?
Type here...