0% found this document useful (0 votes)
6 views4 pages

PDF Notes

This SQL interview cheat sheet provides essential queries, execution order, and key concepts such as WHERE vs HAVING, joins, aggregate functions, and subqueries. It includes examples for common conditions, constraints, indexing, and updating/deleting records. Additionally, it addresses common interview trick questions like finding the second highest salary and identifying duplicate records.

Uploaded by

nebega2685
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

PDF Notes

This SQL interview cheat sheet provides essential queries, execution order, and key concepts such as WHERE vs HAVING, joins, aggregate functions, and subqueries. It includes examples for common conditions, constraints, indexing, and updating/deleting records. Additionally, it addresses common interview trick questions like finding the second highest salary and identifying duplicate records.

Uploaded by

nebega2685
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SQL INTERVIEW CHEAT SHEET

(Quick Revision Guide)

1. Basic Queries

SELECT * FROM employees;

SELECT name, salary


FROM employees
WHERE salary > 50000;

2. SQL Execution Order (IMPORTANT)


1. FROM
2. WHERE
3. GROUP BY
4. HAVING
5. SELECT
6. ORDER BY

3. WHERE vs HAVING
• WHERE → Filters before grouping
• HAVING → Filters after grouping

SELECT department, COUNT(*)


FROM employees
GROUP BY department
HAVING COUNT(*) > 5;

4. Joins (Must Know)


INNER JOIN (Most common)

SELECT [Link], d.dept_name


FROM employees e
INNER JOIN departments d
ON e.dept_id = [Link];
LEFT JOIN

SELECT *
FROM employees e
LEFT JOIN departments d
ON e.dept_id = [Link];

RIGHT JOIN

SELECT *
FROM employees e
RIGHT JOIN departments d
ON e.dept_id = [Link];

5. Aggregate Functions

SELECT COUNT(*) FROM employees;


SELECT AVG(salary) FROM employees;
SELECT SUM(salary) FROM employees;
SELECT MAX(salary) FROM employees;
SELECT MIN(salary) FROM employees;

6. GROUP BY

SELECT department, AVG(salary)


FROM employees
GROUP BY department;

7. Subqueries (Interview Favorite)

SELECT name
FROM employees
WHERE salary > (
SELECT AVG(salary) FROM employees
);
8. Top N Records

SELECT * FROM employees


ORDER BY salary DESC
LIMIT 5;

9. Common Conditions

SELECT * FROM employees


WHERE name LIKE 'A%';

SELECT * FROM employees


WHERE salary BETWEEN 30000 AND 60000;

SELECT * FROM employees


WHERE department IN ('HR', 'IT');

10. Constraints

CREATE TABLE employees (


id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
email VARCHAR(100) UNIQUE,
age INT CHECK (age >= 18)
);

11. Index (Performance Boost)

CREATE INDEX idx_name


ON employees(name);

12. Update & Delete (Be Careful ⚠ )


UPDATE employees
SET salary = 60000
WHERE id = 1;

DELETE FROM employees


WHERE id = 1;

13. Interview Trick Questions


Find 2nd Highest Salary

SELECT MAX(salary)
FROM employees
WHERE salary < (
SELECT MAX(salary) FROM employees
);

Duplicate Records

SELECT name, COUNT(*)


FROM employees
GROUP BY name
HAVING COUNT(*) > 1;

You might also like