SQL Cheat Sheet: Full Guide for Beginners & Professionals

Every core SQL command you need, organized in one quick-reference guide.

Whether you're writing your first query or optimizing reports at work, having a reliable SQL cheat sheet saves time. This guide condenses the commands used daily by data analysts into one place.

Basic Query Structure

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

Filtering and Sorting

  • WHERE — filter rows based on a condition
  • ORDER BY — sort results ascending or descending
  • LIMIT / TOP — restrict the number of rows returned

Joining Tables

SELECT a.name, b.order_date
FROM customers a
INNER JOIN orders b ON a.id = b.customer_id;

Use INNER JOIN for matching rows only, LEFT JOIN to keep all rows from the left table, and FULL OUTER JOIN when you need everything from both sides.

Aggregation

SELECT department, COUNT(*) AS total_employees
FROM employees
GROUP BY department
HAVING COUNT(*) > 5;
💡 Tip: Remember the logical order SQL actually evaluates in: FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY.

Useful Functions

  • COUNT(), SUM(), AVG(), MIN(), MAX()
  • DISTINCT to remove duplicate rows
  • CASE WHEN for conditional logic inside a query

Bookmark this guide as your quick reference while you practice — the more queries you write, the more naturally these patterns will come.

Creating and Changing Data

SQL commands fall into families. DDL (data definition language) shapes the structure, DML (data manipulation language) changes the rows, and the SELECT queries above read them. Knowing which family a command belongs to helps you judge its risk: a DDL mistake can remove a whole table, while a DML mistake can rewrite thousands of rows.

Tables (DDL)

CREATE TABLE employees (
    id         INT PRIMARY KEY AUTO_INCREMENT,
    name       VARCHAR(80) NOT NULL,
    department VARCHAR(40),
    salary     DECIMAL(10,2) DEFAULT 0,
    hired_on   DATE
);

ALTER TABLE employees ADD COLUMN email VARCHAR(120);
ALTER TABLE employees MODIFY COLUMN department VARCHAR(60);
CREATE INDEX idx_emp_dept ON employees (department);
DROP TABLE IF EXISTS temp_import;

AUTO_INCREMENT and MODIFY COLUMN are MySQL syntax. SQL Server uses IDENTITY and ALTER COLUMN instead.

Rows (DML)

INSERT INTO employees (name, department, salary)
VALUES ('Aman', 'Sales', 45000),
       ('Priya', 'IT', 62000);

UPDATE employees
SET salary = salary * 1.10
WHERE department = 'IT';

DELETE FROM employees
WHERE hired_on IS NULL;

Always write the WHERE clause of an UPDATE or DELETE first and test it as a SELECT. Running either without WHERE changes every row in the table.

Filtering patterns worth memorising

PatternExample
Rangesalary BETWEEN 40000 AND 60000
Listdepartment IN ('IT', 'HR')
Text matchname LIKE 'A%' (starts with A)
Missing valuesemail IS NULL (never = NULL)
Fallback valueCOALESCE(department, 'Unassigned')

Subqueries, CTEs and Window Functions

A subquery puts one query inside another:

SELECT name, salary
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);

A common table expression (CTE) gives a subquery a name, which makes long queries readable:

WITH dept_avg AS (
    SELECT department, AVG(salary) AS avg_sal
    FROM employees
    GROUP BY department
)
SELECT e.name, e.department, e.salary, d.avg_sal
FROM employees e
JOIN dept_avg d ON d.department = e.department
WHERE e.salary > d.avg_sal;

Window functions calculate across related rows without collapsing them, which is ideal for rankings and running totals:

SELECT name, department, salary,
       RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS dept_rank,
       SUM(salary) OVER (ORDER BY hired_on) AS running_payroll
FROM employees;

Use ROW_NUMBER() when every row needs a unique position, RANK() when ties should share a rank and leave gaps, and DENSE_RANK() when ties share a rank without gaps.

Combining Results, Transactions and Safe Habits

Unions and transactions

  • UNION stacks results and removes duplicates; UNION ALL keeps duplicates and is faster.
  • Both queries in a union need the same number of columns with compatible types.
  • Group related changes in a transaction with START TRANSACTION, then COMMIT to save or ROLLBACK to undo.

A transaction is all or nothing. If you move money from one account to another, the debit and the credit belong in the same transaction, so a failure halfway through cannot leave one account changed and the other untouched.

Habits that prevent errors

  • List column names instead of using SELECT * in production queries.
  • Give tables short aliases when joining, and prefix every column with its alias.
  • Check row counts after a join; an unexpected jump usually means a missing join condition.
  • Remember that COUNT(column) skips NULLs while COUNT(*) counts every row.

Ready to go beyond the basics?

Get hands-on training, live mentorship, and placement support with Uncodemy's Data Analytics Course.

Explore Data Analytics Course →