Revenue below = quantity x price on non-cancelled orders, grouped by order month (Jan to Jun 2024).
Q33. Top 2 salaries in each department
Return employees who hold one of the top 2 distinct salaries in their department (ignore employees with no department). Tied employees must all be returned.
SELECT dept_id, name, salary, rnk
FROM (
SELECT dept_id, name, salary,
DENSE_RANK() OVER (PARTITION BY dept_id ORDER BY salary DESC) AS rnk
FROM employees
WHERE dept_id IS NOT NULL
) t
WHERE rnk <= 2
ORDER BY dept_id, rnk, name;
-- dept_id | name | salary | rnk
-- 1 | Arjun | 150000 | 1
-- 1 | Bhavna | 110000 | 2
-- 2 | Gauri | 120000 | 1
-- 2 | Harsh | 80000 | 2
-- 3 | Lakshmi | 90000 | 1
-- 3 | Manish | 55000 | 2
-- 4 | Omkar | 130000 | 1
-- 4 | Pooja | 75000 | 2Q34. ROW_NUMBER vs RANK vs DENSE_RANK
For Engineering (dept_id 1), show each employee's salary with ROW_NUMBER, RANK and DENSE_RANK ordered by salary descending.
SELECT name, salary,
ROW_NUMBER() OVER (ORDER BY salary DESC) AS row_num,
RANK() OVER (ORDER BY salary DESC) AS rnk,
DENSE_RANK() OVER (ORDER BY salary DESC) AS dense_rnk
FROM employees
WHERE dept_id = 1
ORDER BY salary DESC, name;
-- name | salary | row_num | rnk | dense_rnk
-- Arjun | 150000 | 1 | 1 | 1
-- Bhavna | 110000 | 2 | 2 | 2
-- Chirag | 95000 | 3 | 3 | 3
-- Tara | 95000 | 4 | 3 | 3
-- Deepa | 90000 | 5 | 5 | 4
-- Esha | 85000 | 6 | 6 | 5
-- Farhan | 85000 | 7 | 6 | 5Q35. Running total of monthly revenue
Calculate revenue per month (excluding cancelled orders) and the cumulative revenue up to each month.
WITH monthly AS (
SELECT EXTRACT(MONTH FROM o.order_date) AS mth,
SUM(oi.quantity * p.price) AS revenue
FROM orders o
JOIN order_items oi ON oi.order_id = o.order_id
JOIN products p ON p.product_id = oi.product_id
WHERE o.status <> 'Cancelled'
GROUP BY EXTRACT(MONTH FROM o.order_date)
)
SELECT mth, revenue,
SUM(revenue) OVER (ORDER BY mth) AS running_total
FROM monthly
ORDER BY mth;
-- mth | revenue | running_total
-- 1 | 94500 | 94500
-- 2 | 15500 | 110000
-- 3 | 92300 | 202300
-- 4 | 106500 | 308800
-- 5 | 15600 | 324400
-- 6 | 76050 | 400450Q36. Month-over-month revenue change (LAG)
For each month, show revenue, previous month's revenue, the change and the percentage change (2 decimals). Exclude cancelled orders. Reuse the monthly CTE from Q35.
WITH monthly AS (
SELECT EXTRACT(MONTH FROM o.order_date) AS mth,
SUM(oi.quantity * p.price) AS revenue
FROM orders o
JOIN order_items oi ON oi.order_id = o.order_id
JOIN products p ON p.product_id = oi.product_id
WHERE o.status <> 'Cancelled'
GROUP BY EXTRACT(MONTH FROM o.order_date)
)
SELECT mth, revenue,
LAG(revenue) OVER (ORDER BY mth) AS prev_revenue,
revenue - LAG(revenue) OVER (ORDER BY mth) AS change_amt,
ROUND(100.0 * (revenue - LAG(revenue) OVER (ORDER BY mth))
/ LAG(revenue) OVER (ORDER BY mth), 2) AS change_pct
FROM monthly
ORDER BY mth;
-- mth | revenue | prev_revenue | change_amt | change_pct
-- 1 | 94500 | NULL | NULL | NULL
-- 2 | 15500 | 94500 | -79000 | -83.60
-- 3 | 92300 | 15500 | 76800 | 495.48
-- 4 | 106500 | 92300 | 14200 | 15.38
-- 5 | 15600 | 106500 | -90900 | -85.35
-- 6 | 76050 | 15600 | 60450 | 387.50Q37. 3-month moving average
Show each month's revenue and the moving average of the current and previous 2 months (excluding cancelled orders). Reuse the monthly CTE from Q35.
WITH monthly AS (
SELECT EXTRACT(MONTH FROM o.order_date) AS mth,
SUM(oi.quantity * p.price) AS revenue
FROM orders o
JOIN order_items oi ON oi.order_id = o.order_id
JOIN products p ON p.product_id = oi.product_id
WHERE o.status <> 'Cancelled'
GROUP BY EXTRACT(MONTH FROM o.order_date)
)
SELECT mth, revenue,
ROUND(AVG(revenue) OVER (
ORDER BY mth
ROWS BETWEEN 2 PRECEDING AND CURRENT ROW
), 2) AS moving_avg_3m
FROM monthly
ORDER BY mth;
-- mth | revenue | moving_avg_3m
-- 1 | 94500 | 94500.00
-- 2 | 15500 | 55000.00
-- 3 | 92300 | 67433.33
-- 4 | 106500 | 71433.33
-- 5 | 15600 | 71466.67
-- 6 | 76050 | 66050.00Q38. Days between consecutive orders
For customer 1 (Aman), show each order date and the number of days since their previous order.
-- PostgreSQL
SELECT order_id, order_date,
order_date - LAG(order_date) OVER (ORDER BY order_date) AS days_since_prev
FROM orders
WHERE customer_id = 1
ORDER BY order_date;
-- MySQL: DATEDIFF(order_date, LAG(order_date) OVER (ORDER BY order_date))
-- order_id | order_date | days_since_prev
-- 1 | 2024-01-05 | NULL
-- 4 | 2024-02-03 | 29
-- 10 | 2024-03-15 | 41
-- 16 | 2024-04-25 | 41
-- 23 | 2024-06-18 | 54