Q39. Customers with 3 or more consecutive login days (gaps and islands)
Find every login streak of 3 or more consecutive days from web_logins, with start date, end date and streak length.
-- PostgreSQL
SELECT customer_id,
MIN(login_date) AS streak_start,
MAX(login_date) AS streak_end,
COUNT(*) AS streak_days
FROM (
SELECT customer_id, login_date,
login_date - (ROW_NUMBER() OVER (
PARTITION BY customer_id ORDER BY login_date))::int AS grp
FROM web_logins
) t
GROUP BY customer_id, grp
HAVING COUNT(*) >= 3
ORDER BY customer_id, streak_start;
-- MySQL: use DATE_SUB(login_date, INTERVAL ROW_NUMBER() OVER (
-- PARTITION BY customer_id ORDER BY login_date) DAY) AS grp
-- customer_id | streak_start | streak_end | streak_days
-- 1 | 2024-06-01 | 2024-06-03 | 3
-- 3 | 2024-06-10 | 2024-06-13 | 4
-- 7 | 2024-06-20 | 2024-06-22 | 3
-- 9 | 2024-06-04 | 2024-06-06 | 3Q40. Reporting hierarchy under Arjun (recursive CTE)
Show everyone who reports to Arjun (emp_id 1), directly or indirectly, with their level below Arjun (Arjun himself is level 0).
WITH RECURSIVE org AS (
SELECT emp_id, name, manager_id, 0 AS lvl
FROM employees
WHERE emp_id = 1
UNION ALL
SELECT e.emp_id, e.name, e.manager_id, o.lvl + 1
FROM employees e
JOIN org o ON e.manager_id = o.emp_id
)
SELECT name, lvl
FROM org
ORDER BY lvl, emp_id;
-- name | lvl
-- Arjun | 0
-- Bhavna | 1
-- Chirag | 1
-- Deepa | 2
-- Esha | 2
-- Farhan | 2
-- Tara | 2Q41. Units sold by category and month (pivot)
Show total units sold per product category as columns for January to June 2024 (excluding cancelled orders). Use 0 for no sales.
SELECT p.category,
SUM(CASE WHEN EXTRACT(MONTH FROM o.order_date) = 1 THEN oi.quantity ELSE 0 END) AS jan,
SUM(CASE WHEN EXTRACT(MONTH FROM o.order_date) = 2 THEN oi.quantity ELSE 0 END) AS feb,
SUM(CASE WHEN EXTRACT(MONTH FROM o.order_date) = 3 THEN oi.quantity ELSE 0 END) AS mar,
SUM(CASE WHEN EXTRACT(MONTH FROM o.order_date) = 4 THEN oi.quantity ELSE 0 END) AS apr,
SUM(CASE WHEN EXTRACT(MONTH FROM o.order_date) = 5 THEN oi.quantity ELSE 0 END) AS may,
SUM(CASE WHEN EXTRACT(MONTH FROM o.order_date) = 6 THEN oi.quantity ELSE 0 END) AS jun
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 p.category
ORDER BY p.category;
-- category | jan | feb | mar | apr | may | jun
-- Electronics | 3 | 2 | 2 | 3 | 1 | 5
-- Fashion | 0 | 0 | 1 | 0 | 2 | 1
-- Furniture | 0 | 1 | 0 | 1 | 1 | 0
-- Home | 0 | 3 | 0 | 1 | 2 | 5
-- Stationery | 12 | 0 | 5 | 4 | 0 | 23Q42. Median salary
Find the median salary of all employees.
SELECT AVG(salary) AS median_salary
FROM (
SELECT salary,
ROW_NUMBER() OVER (ORDER BY salary) AS rn,
COUNT(*) OVER () AS cnt
FROM employees
) t
WHERE rn IN (FLOOR((cnt + 1) / 2.0), CEIL((cnt + 1) / 2.0));
-- PostgreSQL shortcut:
-- SELECT PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY salary) FROM employees;
-- median_salary
-- 82500Q43. Best-selling product in each category
For each product category, find the product with the most units sold (excluding cancelled orders). If products tie, return all tied products.
WITH units AS (
SELECT p.category, p.name AS product, SUM(oi.quantity) AS units_sold
FROM order_items oi
JOIN orders o ON o.order_id = oi.order_id
JOIN products p ON p.product_id = oi.product_id
WHERE o.status <> 'Cancelled'
GROUP BY p.category, p.product_id, p.name
),
ranked AS (
SELECT category, product, units_sold,
RANK() OVER (PARTITION BY category ORDER BY units_sold DESC) AS rnk
FROM units
)
SELECT category, product, units_sold
FROM ranked
WHERE rnk = 1
ORDER BY category;
-- category | product | units_sold
-- Electronics | Headphones | 8
-- Fashion | Backpack | 4
-- Furniture | Desk Chair | 2
-- Home | Water Bottle | 11
-- Stationery | Notebook | 35Q44. Category revenue share
For each category, show revenue (excluding cancelled orders) and its percentage share of total revenue (2 decimals).
SELECT p.category,
SUM(oi.quantity * p.price) AS revenue,
ROUND(100.0 * SUM(oi.quantity * p.price)
/ SUM(SUM(oi.quantity * p.price)) OVER (), 2) AS revenue_pct
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 p.category
ORDER BY revenue DESC;
-- category | revenue | revenue_pct
-- Electronics | 354000 | 88.40
-- Furniture | 28000 | 6.99
-- Fashion | 7200 | 1.80
-- Stationery | 5750 | 1.44
-- Home | 5500 | 1.37