SQL Topics
SQL Exercises Intermediate
title: SQL Exercises Intermediate
Welcome to Intermediate SQL Practice.
At this stage, you should already know:
SELECT
WHERE
GROUP BY
HAVING
JOINS
CRUD Operations
Views
Aggregate FunctionsNow we focus on:
Subqueries
Advanced JOINs
CTEs
Window Functions
Transactions
Indexes
OptimizationThese exercises are commonly asked in:
Technical Interviews
Coding Assessments
Backend Development Roles
Data Analyst PositionsSample Tables
Employees
| EmployeeID | EmployeeName | DepartmentID | Salary |
|---|---|---|---|
| 1 | Rahul | 1 | 50000 |
| 2 | Priya | 2 | 70000 |
| 3 | Amit | 1 | 60000 |
| 4 | Neha | 3 | 80000 |
| 5 | Raj | 2 | 65000 |
Departments
| DepartmentID | DepartmentName |
|---|---|
| 1 | HR |
| 2 | IT |
| 3 | Finance |
Exercise 1
Find Employees Earning More Than Average Salary
SELECT *
FROM Employees
WHERE Salary >
(
SELECT AVG(Salary)
FROM Employees
);Exercise 2
Find Second Highest Salary
SELECT MAX(Salary)
FROM Employees
WHERE Salary <
(
SELECT MAX(Salary)
FROM Employees
);Exercise 3
Find Third Highest Salary
SELECT Salary
FROM
(
SELECT Salary,
DENSE_RANK()
OVER
(
ORDER BY Salary DESC
) AS Rnk
FROM Employees
) X
WHERE Rnk = 3;Exercise 4
Find Department Wise Average Salary
SELECT
DepartmentID,
AVG(Salary)
FROM Employees
GROUP BY DepartmentID;Exercise 5
Find Employees With Highest Salary In Each Department
SELECT *
FROM
(
SELECT *,
DENSE_RANK()
OVER
(
PARTITION BY DepartmentID
ORDER BY Salary DESC
) AS Rnk
FROM Employees
) X
WHERE Rnk = 1;Exercise 6
Find Employees Without Department
SELECT *
FROM Employees E
LEFT JOIN Departments D
ON E.DepartmentID =
D.DepartmentID
WHERE D.DepartmentID IS NULL;Exercise 7
Find Departments Without Employees
SELECT *
FROM Departments D
LEFT JOIN Employees E
ON D.DepartmentID =
E.DepartmentID
WHERE E.EmployeeID IS NULL;Exercise 8
Find Duplicate Salaries
SELECT
Salary,
COUNT(*)
FROM Employees
GROUP BY Salary
HAVING COUNT(*) > 1;Exercise 9
Use INNER JOIN
SELECT
EmployeeName,
DepartmentName
FROM Employees E
INNER JOIN Departments D
ON E.DepartmentID =
D.DepartmentID;Exercise 10
Use LEFT JOIN
SELECT
EmployeeName,
DepartmentName
FROM Employees E
LEFT JOIN Departments D
ON E.DepartmentID =
D.DepartmentID;Exercise 11
Find Top 3 Highest Paid Employees
SELECT *
FROM Employees
ORDER BY Salary DESC
LIMIT 3;Exercise 12
Find Bottom 3 Salaries
SELECT *
FROM Employees
ORDER BY Salary ASC
LIMIT 3;Exercise 13
Assign Row Numbers
SELECT
EmployeeName,
ROW_NUMBER()
OVER
(
ORDER BY Salary DESC
)
AS RowNum
FROM Employees;Exercise 14
Assign Rank
SELECT
EmployeeName,
RANK()
OVER
(
ORDER BY Salary DESC
)
AS RankNum
FROM Employees;Exercise 15
Assign Dense Rank
SELECT
EmployeeName,
DENSE_RANK()
OVER
(
ORDER BY Salary DESC
)
AS DenseRankNum
FROM Employees;Exercise 16
Calculate Running Total
SELECT
EmployeeName,
Salary,
SUM(Salary)
OVER
(
ORDER BY EmployeeID
)
AS RunningTotal
FROM Employees;Exercise 17
Department Wise Running Salary
SELECT
EmployeeName,
DepartmentID,
Salary,
SUM(Salary)
OVER
(
PARTITION BY DepartmentID
ORDER BY EmployeeID
)
AS RunningTotal
FROM Employees;Exercise 18
Use CTE
WITH HighSalary AS
(
SELECT *
FROM Employees
WHERE Salary > 60000
)
SELECT *
FROM HighSalary;Exercise 19
Use Recursive CTE
WITH Numbers AS
(
SELECT 1 AS Num
UNION ALL
SELECT Num + 1
FROM Numbers
WHERE Num < 10
)
SELECT *
FROM Numbers;Exercise 20
Find Employees Reporting To Managers
SELECT
E.EmployeeName,
M.EmployeeName AS Manager
FROM Employees E
LEFT JOIN Employees M
ON E.ManagerID =
M.EmployeeID;Exercise 21
Create Index
CREATE INDEX IX_Salary
ON Employees(Salary);Exercise 22
Create Composite Index
CREATE INDEX IX_DepSalary
ON Employees
(
DepartmentID,
Salary
);Exercise 23
Create Unique Index
CREATE UNIQUE INDEX IX_Email
ON Employees(Email);Exercise 24
Create View
CREATE VIEW ITEmployees AS
SELECT *
FROM Employees
WHERE DepartmentID = 2;Exercise 25
Create Materialized View
(PostgreSQL)
CREATE MATERIALIZED VIEW EmployeeSummary AS
SELECT
DepartmentID,
COUNT(*) AS TotalEmployees
FROM Employees
GROUP BY DepartmentID;Exercise 26
Start Transaction
BEGIN;Exercise 27
Commit Transaction
COMMIT;Exercise 28
Rollback Transaction
ROLLBACK;Exercise 29
Create Savepoint
SAVEPOINT SP1;Exercise 30
Rollback To Savepoint
ROLLBACK TO SP1;Exercise 31
Use CASE Expression
Exercise 32
Use EXISTS
SELECT *
FROM Employees E
WHERE EXISTS
(
SELECT 1
FROM Departments D
WHERE D.DepartmentID =
E.DepartmentID
);Exercise 33
Use NOT EXISTS
SELECT *
FROM Departments D
WHERE NOT EXISTS
(
SELECT 1
FROM Employees E
WHERE E.DepartmentID =
D.DepartmentID
);Exercise 34
Use UNION
SELECT EmployeeName
FROM Employees
UNION
SELECT CustomerName
FROM Customers;Exercise 35
Use UNION ALL
SELECT EmployeeName
FROM Employees
UNION ALL
SELECT CustomerName
FROM Customers;Exercise 36
Use INTERSECT
SELECT Email
FROM Employees
INTERSECT
SELECT Email
FROM Customers;Exercise 37
Use EXCEPT
SELECT Email
FROM Employees
EXCEPT
SELECT Email
FROM Customers;Exercise 38
Analyze Query Using EXPLAIN
EXPLAIN
SELECT *
FROM Employees
WHERE Salary > 60000;Exercise 39
Find Latest Employee
SELECT *
FROM Employees
ORDER BY EmployeeID DESC
LIMIT 1;Exercise 40
Find Highest Salary Department
SELECT
DepartmentID,
AVG(Salary) AS AvgSalary
FROM Employees
GROUP BY DepartmentID
ORDER BY AvgSalary DESC
LIMIT 1;Intermediate Challenge Questions
Try solving these yourself.
Challenge 1
Find Nth highest salary.
Challenge 2
Find duplicate employee names.
Challenge 3
Find employees earning above department average.
Challenge 4
Find latest employee from each department.
Challenge 5
Find department with maximum employees.
Challenge 6
Find employees whose salary ranks in top 10%.
Challenge 7
Generate numbers from 1 to 100 using Recursive CTE.
Challenge 8
Find gaps in EmployeeID sequence.
Challenge 9
Find employee with second highest salary in each department.
Challenge 10
Create a monthly salary report.
Intermediate Practice Checklist
✓ Subqueries
✓ Joins
✓ Self Joins
✓ CTE
✓ Recursive CTE
✓ Window Functions
✓ Transactions
✓ Indexes
✓ Views
✓ Materialized Views
✓ EXISTS
✓ UNION
✓ EXPLAINSummary
These intermediate SQL exercises focus on real-world querying, reporting, ranking, transactions, and performance concepts. Completing them will significantly improve your SQL problem-solving ability and prepare you for developer interviews.
Next Step
Continue to:
SQL Exercises (Advanced) →
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for SQL Exercises Intermediate.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this SQL topic.
Search Terms
sql, sql complete guide, sql tutorial, sql notes, complete, guide, practice, exercises
Related SQL Topics