SQL-Last-Minute-Revision/
A practical and visual SQL revision guide for interviews, coding rounds, and quick refresh before technical discussions.
SQL is easy to learn but surprisingly easy to forget.
This repository is designed as a last-minute SQL revision handbook covering the concepts and query patterns that frequently appear in technical interviews and SQL-based assessments.
Instead of going through lengthy tutorials before an interview, you can use this repository to quickly revise:
- π Core SQL concepts
- π Filtering and sorting
- π Aggregate functions
- ποΈ
GROUP BYandHAVING - π SQL Joins
- π§© Subqueries and CTEs
- πͺ Window Functions
- π Keys and Constraints
- π§± Normalization
- β‘ Indexes and query performance
- π Transactions and ACID
- π― Frequently asked interview queries
β οΈ Common SQL mistakes and tricky concepts
Learn the concept β Understand the pattern β Write the query β Practice the variation.
- SQL Basics
- Filtering & Sorting
- Aggregate Functions
- GROUP BY & HAVING
- Joins
- Subqueries
- CTEs
- Window Functions
- Keys & Constraints
- Normalization
- Indexes
- Transactions & ACID
- Common Interview Queries
- Tricky SQL Questions
- Quick Revision Checklist
Before solving complex queries, understand the basic building blocks of SQL.
- What is SQL?
- SQL vs MySQL
- Database vs Table
- Rows and Columns
SELECTDISTINCTFROMWHEREORDER BYLIMIT- SQL comments
- SQL data types
SELECT name, salary
FROM employees
WHERE salary > 50000
ORDER BY salary DESC;Learn how to retrieve exactly the data you need.
= Equal
<> Not equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
BETWEEN Range
IN Match multiple values
LIKE Pattern matching
IS NULL NULL checking
SELECT *
FROM employees
WHERE department IN ('IT', 'HR')
AND salary BETWEEN 40000 AND 80000;Aggregate functions perform calculations across multiple rows.
| Function | Purpose |
|---|---|
COUNT() |
Count rows |
SUM() |
Calculate total |
AVG() |
Calculate average |
MIN() |
Find minimum |
MAX() |
Find maximum |
SELECT
department,
COUNT(*) AS employee_count,
AVG(salary) AS average_salary,
MAX(salary) AS highest_salary
FROM employees
GROUP BY department;This is one of the most important areas for SQL interviews.
WHERE
β
Filters individual rows
GROUP BY
β
Creates groups
HAVING
β
Filters groups
SELECT
department,
COUNT(*) AS employee_count
FROM employees
GROUP BY department
HAVING COUNT(*) > 5;β Incorrect:
WHERE COUNT(*) > 5β Correct:
HAVING COUNT(*) > 5Joins combine data from multiple tables.
INNER JOIN
LEFT JOIN
RIGHT JOIN
FULL OUTER JOIN
CROSS JOIN
SELF JOIN
INNER JOIN
A β© B
LEFT JOIN
A + matching B
RIGHT JOIN
B + matching A
FULL JOIN
A βͺ B
SELECT
e.name,
d.department_name
FROM employees e
INNER JOIN departments d
ON e.department_id = d.department_id;INNER JOIN β Matching rows
LEFT JOIN β Everything from the left table + matches
RIGHT JOIN β Everything from the right table + matches
FULL JOIN β Everything from both tables
A subquery is a query inside another query.
Find employees earning more than the average salary:
SELECT name, salary
FROM employees
WHERE salary > (
SELECT AVG(salary)
FROM employees
);- Scalar subquery
- Single-row subquery
- Multi-row subquery
- Correlated subquery
- Nested subquery
CTEs allow you to create a temporary named result set that can be referenced by the main query.
WITH department_salary AS (
SELECT
department,
AVG(salary) AS avg_salary
FROM employees
GROUP BY department
)
SELECT *
FROM department_salary
WHERE avg_salary > 60000;- Improve readability
- Break complex queries into steps
- Make debugging easier
- Useful for recursive queries
Window functions are one of the most important topics for modern SQL interviews.
Unlike GROUP BY, window functions do not collapse rows.
ROW_NUMBER()
RANK()
DENSE_RANK()
NTILE()
LEAD()
LAG()
FIRST_VALUE()
LAST_VALUE()
SUM() OVER()
AVG() OVER()
COUNT() OVER()Find salary ranking within each department:
SELECT
name,
department,
salary,
DENSE_RANK() OVER (
PARTITION BY department
ORDER BY salary DESC
) AS salary_rank
FROM employees;| Salary | RANK | DENSE_RANK |
|---|---|---|
| 100000 | 1 | 1 |
| 90000 | 2 | 2 |
| 90000 | 2 | 2 |
| 80000 | 4 | 3 |
RANK()leaves gaps after ties.DENSE_RANK()does not.
- Primary Key
- Foreign Key
- Candidate Key
- Composite Key
- Alternate Key
- Unique Key
PRIMARY KEY
FOREIGN KEY
UNIQUE
NOT NULL
CHECK
DEFAULTExample:
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(150) UNIQUE,
salary DECIMAL(10,2) CHECK (salary > 0),
department_id INT,
FOREIGN KEY (department_id)
REFERENCES departments(department_id)
);Normalization organizes data to reduce redundancy and improve data integrity.
1NF
β
2NF
β
3NF
β
BCNF
1NF
- Atomic values
- No repeating groups
2NF
- Must be in 1NF
- No partial dependency on a composite key
3NF
- Must be in 2NF
- No transitive dependency
Indexes help databases find rows more efficiently.
Think of an index like the index of a book:
Without Index
Database β Scan many rows β Find data
With Index
Database β Index β Locate rows β Fetch data
- Clustered Index
- Non-clustered Index
- Composite Index
- Unique Index
- Index Selectivity
- When indexes help
- When indexes can hurt performance
Indexes can improve read performance, but they also require storage and can add overhead to
INSERT,UPDATE, andDELETEoperations.
A transaction is a logical unit of database work.
A β Atomicity
C β Consistency
I β Isolation
D β Durability
BEGIN;
COMMIT;
ROLLBACK;
SAVEPOINT;BEGIN;
UPDATE accounts
SET balance = balance - 1000
WHERE account_id = 101;
UPDATE accounts
SET balance = balance + 1000
WHERE account_id = 102;
COMMIT;If something goes wrong:
ROLLBACK;These patterns are worth practicing repeatedly.
SELECT MAX(salary)
FROM employees
WHERE salary < (
SELECT MAX(salary)
FROM employees
);SELECT email, COUNT(*)
FROM employees
GROUP BY email
HAVING COUNT(*) > 1;SELECT department, MAX(salary)
FROM employees
GROUP BY department;SELECT *
FROM (
SELECT
name,
department,
salary,
DENSE_RANK() OVER (
PARTITION BY department
ORDER BY salary DESC
) AS rnk
FROM employees
) t
WHERE rnk <= 3;SELECT
e.name AS employee,
e.salary AS employee_salary,
m.name AS manager,
m.salary AS manager_salary
FROM employees e
JOIN employees m
ON e.manager_id = m.employee_id
WHERE e.salary > m.salary;These are common areas where candidates make mistakes:
NULLvs0COUNT(*)vsCOUNT(column)WHEREvsHAVINGUNIONvsUNION ALLDELETEvsTRUNCATEvsDROPRANK()vsDENSE_RANK()INNER JOINvsLEFT JOININvsEXISTSNOT INwithNULLCOUNT(DISTINCT column)- Duplicate rows after joins
- Filtering before vs after aggregation
- Window functions vs
GROUP BY
One of the most useful things to remember before an interview:
FROM
β
JOIN
β
WHERE
β
GROUP BY
β
HAVING
β
SELECT
β
DISTINCT
β
ORDER BY
β
LIMIT
Remember:
The order you write SQL is not necessarily the order in which the database logically processes it.
- SELECT
- DISTINCT
- WHERE
- ORDER BY
- LIMIT
- NULL
- LIKE
- IN
- BETWEEN
- COUNT
- SUM
- AVG
- MIN
- MAX
- GROUP BY
- HAVING
- INNER JOIN
- LEFT JOIN
- RIGHT JOIN
- FULL JOIN
- CROSS JOIN
- SELF JOIN
- Subqueries
- CTEs
- CASE
- UNION
- EXISTS
- Window Functions
- ROW_NUMBER
- RANK
- DENSE_RANK
- LEAD
- LAG
- Primary Key
- Foreign Key
- Constraints
- Normalization
- Indexes
- Transactions
- ACID
- Views
Focus on these first:
1. Joins
2. GROUP BY + HAVING
3. Subqueries
4. CTEs
5. Window Functions
6. Aggregate Functions
7. NULL handling
8. Common interview queries
9. DELETE vs TRUNCATE vs DROP
10. Primary Key / Foreign Key / Indexes
Then solve problems without looking at the answer.
The goal isn't to memorize 100 queries.
The goal is to recognize the query pattern and adapt it to a new problem.
Found an error or want to add a useful SQL interview problem?
Contributions are welcome.
- Fork the repository
- Create a new branch
- Add or improve the content
- Commit your changes
- Open a Pull Request
If you find this repository useful for your SQL preparation, consider giving it a β.
Happy querying! π
SELECT 'Keep Learning!' AS message;