icon picker
SQL

🔹 Basic SQL

Table 1
Question
Concepts
1. Get the second highest salary from the Employee table.
There are no rows in this table
sql
CopyEdit
SELECT MAX(salary)
FROM Employee
WHERE salary < (SELECT MAX(salary) FROM Employee);

| Subquery, MAX |
| 2. Find all duplicate emails in the Users table.
sql
CopyEdit
SELECT email, COUNT(*)
FROM Users
GROUP BY email
HAVING COUNT(*) > 1;

| GROUP BY, HAVING |
| 3. Retrieve employees with names starting with ‘A’.
sql
CopyEdit
SELECT * FROM Employee WHERE name LIKE 'A%';

| LIKE |

🔸 Joins & Relationships

Table 2
Question
Concepts
4. List all customers and the orders they made (including customers who didn’t make any orders).
There are no rows in this table
sql
CopyEdit
SELECT c.name, o.order_id
FROM Customers c
LEFT JOIN Orders o ON c.id = o.customer_id;

| LEFT JOIN |
| 5. Find customers who didn’t place any orders.
sql
CopyEdit
SELECT c.name
FROM Customers c
LEFT JOIN Orders o ON c.id = o.customer_id
WHERE o.order_id IS NULL;

| Anti-Join |

🔹 Aggregations & GROUP BY

Table 3
Question
Concepts
6. Get department-wise average salary.
There are no rows in this table
sql
CopyEdit
SELECT department, AVG(salary)
FROM Employee
GROUP BY department;

| AVG, GROUP BY |
| 7. Count number of employees hired each year.
sql
CopyEdit
SELECT YEAR(hire_date) AS year, COUNT(*)
FROM Employee
GROUP BY YEAR(hire_date);

| DATE functions |

🔸 Window Functions

Table 4
Question
Concepts
8. Find the top 3 highest salaries per department.
There are no rows in this table
sql
CopyEdit
SELECT * FROM (
SELECT *,
DENSE_RANK() OVER (PARTITION BY department ORDER BY salary DESC) as rank
FROM Employee
) t WHERE rank <= 3;

| DENSE_RANK, PARTITION BY |
| 9. Running total of sales by date.
sql
CopyEdit
SELECT order_date,
SUM(sale_amount) OVER (ORDER BY order_date) AS running_total
FROM Sales;

| SUM, OVER |

🔹 CTEs & Subqueries

Table 5
Question
Concepts
10. With clause to get departments with avg salary > 50K.
There are no rows in this table
sql
CopyEdit
WITH dept_avg AS (
SELECT department, AVG(salary) AS avg_sal
FROM Employee
GROUP BY department
)
SELECT * FROM dept_avg WHERE avg_sal > 50000;

| CTE, WITH |
| 11. Find employees who earn more than their manager.
sql
CopyEdit
SELECT e.name
FROM Employee e
JOIN Employee m ON e.manager_id = m.id
WHERE e.salary > m.salary;

| Self-join |

🧠 Bonus: SQL Interview Scenarios

Table 6
Scenario
What You Practice
Design a leaderboard system
Window functions, ranks
Recommend friends (2nd degree connections)
Self-joins
Analyze churn rate over months
Joins, CASE, date manipulation
Product orders by category
GROUP BY, COUNT, JOINs
There are no rows in this table
Would you like:
📝 Practice dataset (e.g., for Employees, Sales)?
📚 Platform-specific practice links (LeetCode SQL, Hackerrank, StrataScratch)?
🧪 Custom SQL test with answers?
Let me know!
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.