SQL Questions
SQL Questions
Write a query to find the second highest salary in a table without using LIMIT, OFFSET, or subqueries.
Using window functions, how would you retrieve the Nth highest salary from an employee table?
Given an employee’s table with a self-referencing manager column, write a recursive CTE to display the complete
reporting hierarchy for a specific employee.
Write an SQL query to detect cycles in a hierarchical table using recursive techniques.
How would you retrieve the top three orders per customer in a large orders table using window functions?
Write a query to pivot row data into columns using only CASE statements.
Design a query that calculates a 7-day rolling average of sales using window functions.
Write an SQL query to identify missing dates in a continuous date series and fill in those gaps with zero values.
Given a table of user logins with timestamps, write a query to compute the maximum gap between consecutive
logins per user.
Write a query to compute a running total of sales, partitioned by region and ordered by date.
How can you optimize a query that relies on multiple correlated subqueries by rewriting it with joins?
Discuss and demonstrate an indexing strategy that could improve performance on a table with billions of rows.
Write a query using a correlated subquery to compare each product’s sales against the average sales for its category.
Given three tables (customers, orders, products), write a query to compute total revenue per customer and rank
them accordingly.
Using advanced SQL techniques, write a query to generate a cumulative sum of orders for each day.
Write a recursive query to traverse a graph represented by node relationships and list all paths between two given
nodes.
How would you use dynamic SQL to pivot a dataset when the target columns are not known at runtime?
Given a table of order details with timestamps, write a query to compute the time difference between the first and
last order for each customer.
These questions cover advanced SQL concepts like window functions, self-joins, aggregations, string operations,
date/time handling, and recursion.
➤ 𝗦𝗤𝗟