Advanced SQL Solutions (Job-Level)
Generated on: 2025-07-11 19:46:16
1. N-th Highest Salary Using DENSE_RANK()
Problem: Find the 3rd highest salary from the employees table.
Table Structure:
| employee_id | name | salary | department_id |
|-------------|-------|--------|----------------|
|1 | Ravi | 90000 | 101 |
|2 | Neha | 80000 | 102 |
|3 | Arjun | 85000 | 101 |
|4 | Meena | 80000 | 101 |
|5 | Riya | 75000 | 103 |
SQL Code:
WITH ranked_salaries AS (
SELECT name, salary,
DENSE_RANK() OVER (ORDER BY salary DESC) AS salary_rank
FROM employees
)
SELECT name, salary
FROM ranked_salaries
WHERE salary_rank = 3;
Explanation:
- DENSE_RANK gives rank without gaps.
- Useful when multiple employees have same salary.
- Change 3 to N for any N-th salary.
2. Generate a Date Series (Recursive CTE)
Problem: Generate a list of dates from 2024-01-01 to 2024-01-10.
SQL Code:
WITH RECURSIVE date_series AS (
SELECT DATE '2024-01-01' AS dt
UNION ALL
SELECT dt + INTERVAL '1 day'
FROM date_series
WHERE dt < '2024-01-10'
)
Advanced SQL Solutions (Job-Level)
Generated on: 2025-07-11 19:46:16
SELECT * FROM date_series;
Explanation:
- Starts at the given date and increments daily.
- Useful for calendar tables, time series completion.
3. Running Total (Cumulative SUM)
Problem: Calculate cumulative order amount per customer.
Table:
| order_id | customer_id | order_date | order_amount |
|----------|-------------|------------|--------------|
| 101 |1 | 2024-01-01 | 5000 |
| 102 |1 | 2024-01-03 | 2000 |
| 103 |1 | 2024-01-05 | 1000 |
| 104 |2 | 2024-01-02 | 3000 |
SQL Code:
SELECT customer_id, order_id, order_date, order_amount,
SUM(order_amount) OVER (
PARTITION BY customer_id
ORDER BY order_date
) AS running_total
FROM orders;
Explanation:
- PARTITION BY resets sum per customer.
- ORDER BY ensures chronological total.
4. Gaps in Order Dates (Using LAG)
Problem: Find customers with order gaps of 7+ days.
SQL Code (MySQL syntax):
SELECT customer_id, order_id, order_date,
LAG(order_date) OVER (PARTITION BY customer_id ORDER BY order_date) AS previous_order,
DATEDIFF(order_date, LAG(order_date) OVER (PARTITION BY customer_id ORDER BY order_date)) AS
gap_days
FROM orders
Advanced SQL Solutions (Job-Level)
Generated on: 2025-07-11 19:46:16
WHERE DATEDIFF(order_date, LAG(order_date) OVER (PARTITION BY customer_id ORDER BY order_date)) >= 7;
Explanation:
- LAG finds previous date.
- DATEDIFF gives the gap.
- Useful in churn analysis.
5. Pivot Table (Sales by Month)
Problem: Show monthly sales per product.
Table:
| product_id | order_date | amount |
|------------|------------|--------|
| 101 | 2024-01-10 | 500 |
| 101 | 2024-02-05 | 700 |
| 102 | 2024-01-15 | 400 |
SQL Code:
SELECT product_id,
SUM(CASE WHEN MONTH(order_date) = 1 THEN amount ELSE 0 END) AS Jan,
SUM(CASE WHEN MONTH(order_date) = 2 THEN amount ELSE 0 END) AS Feb,
SUM(CASE WHEN MONTH(order_date) = 3 THEN amount ELSE 0 END) AS Mar
FROM sales
GROUP BY product_id;
Explanation:
- Each CASE counts amount per month.
- Creates a pivot-style view.