SQL Topics
SQL Challenges
title: SQL Challenges
🎉 Congratulations!
You have completed:
Introduction
Setup
Basics
CRUD
Filtering
Functions
Joins
Subqueries
Set Operators
Constraints
Indexing
Views
Stored Procedures
Triggers
Transactions
Normalization
Advanced SQL
Optimization
Interview Questions
Exercises
ProjectsNow comes the final section:
SQL ChallengesThese challenges simulate:
Real Interviews
Production Problems
Data Analytics Tasks
Database Engineering ScenariosMost companies use similar questions during:
SQL Interviews
Online Assessments
Data Analyst Tests
Backend Developer InterviewsChallenge 1
Find Second Highest Salary
Table:
Employees| EmployeeID | Salary |
|---|---|
| 1 | 80000 |
| 2 | 70000 |
| 3 | 60000 |
Expected Output
70000Solution
SELECT MAX(Salary)
FROM Employees
WHERE Salary <
(
SELECT MAX(Salary)
FROM Employees
);Challenge 2
Find Nth Highest Salary
Solution
SELECT Salary
FROM
(
SELECT Salary,
DENSE_RANK()
OVER
(
ORDER BY Salary DESC
) AS Rnk
FROM Employees
) X
WHERE Rnk = N;Challenge 3
Find Duplicate Emails
Solution
SELECT
Email,
COUNT(*)
FROM Users
GROUP BY Email
HAVING COUNT(*) > 1;Challenge 4
Delete Duplicate Rows
Keep only one row.
Solution
WITH Duplicates AS
(
SELECT *,
ROW_NUMBER()
OVER
(
PARTITION BY Email
ORDER BY UserID
) AS RN
FROM Users
)
DELETE
FROM Duplicates
WHERE RN > 1;Challenge 5
Find Customers Who Never Ordered
Solution
SELECT *
FROM Customers C
LEFT JOIN Orders O
ON C.CustomerID =
O.CustomerID
WHERE O.OrderID IS NULL;Challenge 6
Find Products Never Sold
Solution
SELECT *
FROM Products P
LEFT JOIN OrderItems O
ON P.ProductID =
O.ProductID
WHERE O.ProductID IS NULL;Challenge 7
Highest Salary In Each Department
Solution
SELECT *
FROM
(
SELECT *,
DENSE_RANK()
OVER
(
PARTITION BY DepartmentID
ORDER BY Salary DESC
) AS Rnk
FROM Employees
) X
WHERE Rnk = 1;Challenge 8
Second Highest Salary In Each Department
Solution
SELECT *
FROM
(
SELECT *,
DENSE_RANK()
OVER
(
PARTITION BY DepartmentID
ORDER BY Salary DESC
) AS Rnk
FROM Employees
) X
WHERE Rnk = 2;Challenge 9
Running Total
Solution
SELECT
EmployeeID,
Salary,
SUM(Salary)
OVER
(
ORDER BY EmployeeID
)
AS RunningTotal
FROM Employees;Challenge 10
Moving Average
Solution
SELECT
EmployeeID,
Salary,
AVG(Salary)
OVER
(
ORDER BY EmployeeID
ROWS BETWEEN
2 PRECEDING
AND CURRENT ROW
)
AS MovingAverage
FROM Employees;Challenge 11
Find Missing IDs
Example:
1
2
4
5Missing:
3Solution
WITH Numbers AS
(
SELECT 1 AS Num
UNION ALL
SELECT Num + 1
FROM Numbers
WHERE Num < 100
)
SELECT Num
FROM Numbers
EXCEPT
SELECT EmployeeID
FROM Employees;Challenge 12
Consecutive Login Days
Interview Favorite.
Hint
Use:
ROW_NUMBER()
Date Difference
CTEChallenge 13
Find Top 10% Employees By Salary
Solution
SELECT *
FROM
(
SELECT *,
NTILE(10)
OVER
(
ORDER BY Salary DESC
) AS SalaryGroup
FROM Employees
) X
WHERE SalaryGroup = 1;Challenge 14
Latest Record Per Group
Solution
SELECT *
FROM
(
SELECT *,
ROW_NUMBER()
OVER
(
PARTITION BY DepartmentID
ORDER BY JoinDate DESC
) AS RN
FROM Employees
) X
WHERE RN = 1;Challenge 15
Most Frequently Ordered Product
Solution
SELECT
ProductID,
COUNT(*) AS Orders
FROM OrderItems
GROUP BY ProductID
ORDER BY Orders DESC
LIMIT 1;Challenge 16
Top Customer By Revenue
Solution
SELECT
CustomerID,
SUM(Amount) AS Revenue
FROM Orders
GROUP BY CustomerID
ORDER BY Revenue DESC
LIMIT 1;Challenge 17
Customer Retention Analysis
Task
Find customers who ordered in:
Month 1
and
Month 2Hint
Use:
CTE
JOIN
GROUP BYChallenge 18
Year-over-Year Growth
Task
Calculate:
Current Year Revenue
Previous Year Revenue
Growth %Hint
Use:
LAG()
Window FunctionsChallenge 19
Department Salary Contribution %
Solution
SELECT
DepartmentID,
SUM(Salary) * 100.0 /
SUM(SUM(Salary))
OVER()
AS Contribution
FROM Employees
GROUP BY DepartmentID;Challenge 20
Pivot Monthly Sales
Input:
Jan
Feb
MarOutput:
Jan Feb Marcolumns.
Hint
Use:
PIVOTChallenge 21
Build Employee Hierarchy
Solution
WITH EmployeeHierarchy AS
(
SELECT
EmployeeID,
EmployeeName,
ManagerID
FROM Employees
WHERE ManagerID IS NULL
UNION ALL
SELECT
E.EmployeeID,
E.EmployeeName,
E.ManagerID
FROM Employees E
JOIN EmployeeHierarchy EH
ON E.ManagerID =
EH.EmployeeID
)
SELECT *
FROM EmployeeHierarchy;Challenge 22
Detect Circular Manager Relationships
Hint
Use:
Recursive CTE
Hierarchy TraversalChallenge 23
Find Gaps And Islands
Classic SQL Interview Problem.
Example
1
2
3
7
8
12
13
14Find continuous ranges.
Challenge 24
Detect Duplicate Transactions
Hint
Check:
Customer
Amount
Timestampcombinations.
Challenge 25
Rank Products By Revenue
Solution
SELECT
ProductID,
SUM(Amount) AS Revenue,
RANK()
OVER
(
ORDER BY SUM(Amount) DESC
)
AS ProductRank
FROM Sales
GROUP BY ProductID;Challenge 26
Optimize Slow Query
Given:
SELECT *
FROM Orders
WHERE CustomerEmail =
'abc@gmail.com';Task
Improve performance.
Answer
Create index:
CREATE INDEX IX_Email
ON Orders(CustomerEmail);Challenge 27
Analyze Execution Plan
Task
Identify:
Table Scan
Index Scan
Index Seek
Sort
Hash Joinfrom an execution plan.
Challenge 28
Design Banking Transfer Transaction
Requirements:
Money Transfer
Rollback On Failure
ConsistencyHint
Use:
BEGIN
COMMIT
ROLLBACKChallenge 29
Design E-Commerce Schema
Tables:
Products
Customers
Orders
PaymentsTask
Create normalized schema.
Challenge 30
Build Complete Sales Dashboard Query
Metrics:
Revenue
Orders
Customers
Top Products
Growth %Hint
Combine:
CTE
Window Functions
GROUP BY
JoinsUltimate SQL Challenge
Build a complete:
E-Commerce Analytics Databaseincluding:
Schema Design
CRUD Operations
Indexes
Views
Stored Procedures
Triggers
Reports
Window Functions
Optimization
PartitioningSQL Master Checklist
After finishing all modules you should know:
✓ SQL Syntax
✓ CRUD Operations
✓ Filtering
✓ Joins
✓ Subqueries
✓ Set Operators
✓ Constraints
✓ Indexes
✓ Views
✓ Stored Procedures
✓ Triggers
✓ Transactions
✓ Normalization
✓ CTEs
✓ Window Functions
✓ MERGE
✓ Dynamic SQL
✓ Optimization
✓ Partitioning
✓ Interview Questions
✓ Real Projects
✓ Advanced ChallengesSQL Roadmap Completed ✅
Congratulations 🎉
You have now completed a Full SQL Learning Path covering:
- SQL Fundamentals
- Intermediate SQL
- Advanced SQL
- Query Optimization
- Database Design
- Interview Preparation
- Real Projects
- Enterprise-Level Concepts
The only remaining file in your structure is:
sql-roadmap.mdxwhich should serve as the master navigation page linking all folders and lessons.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for SQL Challenges.
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, challenges
Related SQL Topics