SQL
Interview Questions
Window functions, CTEs, subqueries, GROUP BY pitfalls, indexing strategy, and query optimization.
SQL Interview Question: A Query Taking 2 Minutes Now Takes 20 Minutes. What Will You Do?
Medium"I wouldn't immediately rewrite the query. First, I would investigate the root cause step by step.
Check system performance: Verify CPU, memory, disk I/O, and database load.
Check execution plan: Compare the current execution plan with the previous one to identify performance changes.
Check data volume: See whether the number of records has increased significantly.
Check indexes: Verify whether the query is using indexes or performing unnecessary full table scans.
Check joins: Identify inefficient joins or unexpected increases in matching records.
Check partition pruning: Ensure the query reads only the required partitions instead of scanning historical data.
Check statistics: Verify that database statistics are updated so the optimizer can choose an efficient plan.
Check recent changes: Investigate recent deployments, query modifications, or infrastructure changes.
Finally, after identifying the root cause, I would apply the appropriate optimization, test it safely, and monitor execution time to confirm the improvement."
Fetch all orders where the total_value is greater than the average order value.?
Medium Input Table: Orders
| order_id | customer_id | total_value |
|---|---|---|
| 901 | 1 | 5000 |
| 902 | 2 | 7000 |
| 903 | 3 | 6000 |
| 904 | 4 | 8000 |
I use a subquery to calculate the average order value using AVG(). Then I use the result in the outer query to return orders whose total value is greater than that average.
Find the most frequently sold product in each category.?
MediumProducts
| product_id | category | product_name |
|---|---|---|
| 601 | A | Apple |
| 602 | A | Banana |
| 603 | B | Milk |
| 604 | B | Bread |
Sales
| sale_id | product_id | quantity |
|---|---|---|
| 1 | 601 | 10 |
| 2 | 602 | 5 |
| 3 | 601 | 8 |
| 4 | 603 | 12 |
| 5 | 604 | 4 |
Then we can calculate total quantity sold:
Apple → 10 + 8 = 18 Banana → 5 Milk → 12 Bread → 4
Therefore:
Category A → Apple Category B → Milk
SQL concept
The solution normally involves:
Products ↓ JOIN Sales ↓ GROUP BY category + product ↓ SUM(quantity) / COUNT(*) ↓ Rank products within each category ↓ Take rank = 1
Find customers who have never placed an order.?
MediumThe key concept here is a LEFT JOIN with IS NULL.
1. Customers Table
| customer_id | customer_name |
|---|---|
| 1 | John |
| 2 | Alice |
| 3 | Bob |
2. Orders Table
| order_id | customer_id |
|---|---|
| 101 | 1 |
| 102 | 2 |
I use a LEFT JOIN between Customers and Orders on customer_id. The LEFT JOIN keeps all customers, including those without orders. Then I filter where the Orders customer_id is NULL to identify customers who have never placed an order.
Find the second-highest salary from the Employees table without using LIMIT or TOP.
MediumExample Employees Table
Suppose we have:
| emp_id | emp_name | salary |
|---|---|---|
| 101 | Alice | 95000 |
| 102 | Bob | 87000 |
| 103 | Charlie | 91000 |
| 104 | David | 75000 |
| 105 | Eve | 99000 |
I use a subquery to find the maximum salary, filter out that salary using the WHERE clause, and then use MAX() on the remaining salaries to get the second-highest salary.
Calculate the average sales amount for each region.
Medium Input Table: Sales
| sales_id | region | sales_amount |
|---|---|---|
| 501 | North | 50000 |
| 502 | South | 70000 |
| 503 | North | 80000 |
| 504 | West | 90000 |
I group the sales records by region using GROUP BY, and then use the AVG() aggregate function to calculate the average sales amount for each region.
Write a SQL query to fetch all orders that were placed on weekends (Saturday or Sunday).?
MediumInput table: Orders
| order_id | customer_id | order_date |
|---|---|---|
| 401 | 1 | 2024-02-17 |
| 402 | 2 | 2024-02-18 |
| 403 | 3 | 2024-02-19 |
| 404 | 1 | 2024-02-24 |
Let's identify the days:
| order_id | order_date | Day | Weekend? |
|---|---|---|---|
| 401 | 2024-02-17 | Saturday | ✅ |
| 402 | 2024-02-18 | Sunday | ✅ |
| 403 | 2024-02-19 | Monday | ❌ |
| 404 | 2024-02-24 | Saturday | ✅ |
I use the DAYOFWEEK() function to determine the weekday for each order date, and then filter for Sunday and Saturday using IN (1, 7).
Write a query to retrieve the first and last purchase date for each customer.?
MediumInput Table: Transactions
| transaction_id | customer_id | purchase_date |
|---|---|---|
| 301 | 1 | 2024-01-10 |
| 302 | 2 | 2024-01-12 |
| 303 | 1 | 2024-02-15 |
| 304 | 2 | 2024-02-20 |
| 305 | 1 | 2024-03-05 |
Find customers who have placed more than 5 orders in the last 30 days.?
MediumInput Table: Orders
| order_id | customer_id | order_date |
|---|---|---|
| 201 | 1 | 2024-02-15 |
| 202 | 2 | 2024-02-10 |
| 203 | 1 | 2024-02-18 |
| 204 | 3 | 2024-02-20 |
| 205 | 1 | 2024-02-25 |
| 206 | 2 | 2024-02-27 |
| 207 | 1 | 2024-03-02 |
Write a query to fetch the top 3 employees with the highest salaries from an Employees table.?(Asked in Accenture)
EasyFetch the Top 3 Employees with the Highest Salaries
Input Table: Employees
| emp_id | emp_name | salary |
|---|---|---|
| 101 | Alice | 95000 |
| 102 | Bob | 87000 |
| 103 | Charlie | 91000 |
| 104 | David | 75000 |
| 105 | Eve | 99000 |