TGViewer
Data Analytics Data Analytics @sqlspecialist ยท 111K subscribers
Post #2887 5.24K
๐Ÿš€ SQL Scenario Based Interview Questions with Answers Part 2

๐Ÿ“Œ Question 11: Find the Second Highest Salary in Each Department 
Table: employees (employee_id, department_id, salary)

WITH ranked_salary AS (
    SELECT *,
           DENSE_RANK() OVER (
               PARTITION BY department_id
               ORDER BY salary DESC
           ) AS rnk
    FROM employees
)
SELECT department_id, employee_id, salary
FROM ranked_salary
WHERE rnk = 2;


๐Ÿ“Œ Question 12: Identify Users Who Purchased on Their First Visit 
Tables: visits (user_id, visit_date) | orders (user_id, order_date)

WITH first_visit AS (
    SELECT user_id,
           MIN(visit_date) AS first_visit_date
    FROM visits
    GROUP BY user_id
)
SELECT DISTINCT f.user_id
FROM first_visit f
JOIN orders o
    ON f.user_id = o.user_id
   AND f.first_visit_date = o.order_date;


๐Ÿ“Œ Question 13: Find Products Never Sold 
Tables: products (product_id, product_name) | sales (product_id)

SELECT p.product_id, p.product_name
FROM products p
LEFT JOIN sales s
       ON p.product_id = s.product_id
WHERE s.product_id IS NULL;


๐Ÿ“Œ Question 14: Calculate Month-over-Month Revenue Growth 
Table: orders (order_date, revenue)

WITH monthly_revenue AS (
    SELECT DATE_TRUNC('month', order_date) AS month,
           SUM(revenue) AS total_revenue
    FROM orders
    GROUP BY 1
)
SELECT month,
       total_revenue,
       LAG(total_revenue) OVER (ORDER BY month) AS previous_month,
       ROUND(
           100.0 *
           (total_revenue - LAG(total_revenue) OVER (ORDER BY month))
           /
           LAG(total_revenue) OVER (ORDER BY month),
           2
       ) AS growth_pct
FROM monthly_revenue;


๐Ÿ“Œ Question 15: Find Employees Earning More Than Department Average 
Table: employees (employee_id, department_id, salary)

SELECT employee_id, department_id, salary
FROM (
    SELECT *,
           AVG(salary) OVER (
               PARTITION BY department_id
           ) AS dept_avg
    FROM employees
) t
WHERE salary > dept_avg;


๐Ÿ“Œ Question 16: Find Longest Consecutive Login Streak 
Table: logins (user_id, login_date)

WITH cte AS (
    SELECT user_id,
           login_date,
           login_date -
           ROW_NUMBER() OVER (
               PARTITION BY user_id
               ORDER BY login_date
           ) * INTERVAL '1 day' AS grp
    FROM logins
)
SELECT user_id, COUNT(*) AS streak_days
FROM cte
GROUP BY user_id, grp
ORDER BY streak_days DESC;


๐Ÿ“Œ Question 17: Find Peak Sales Day of Every Month 
Table: sales (sale_date, amount)

WITH daily_sales AS (
    SELECT DATE(sale_date) AS sale_day,
           SUM(amount) AS revenue
    FROM sales
    GROUP BY DATE(sale_date)
)
SELECT *
FROM (
    SELECT *,
           ROW_NUMBER() OVER (
               PARTITION BY DATE_TRUNC('month', sale_day)
               ORDER BY revenue DESC
           ) rn
    FROM daily_sales
) t
WHERE rn = 1;


๐Ÿ“Œ Question 18: Find Customers Who Ordered Every Month 
Table: orders (customer_id, order_date)

WITH customer_months AS (
    SELECT customer_id,
           COUNT(DISTINCT DATE_TRUNC('month', order_date)) AS months_active
    FROM orders
    GROUP BY customer_id
),
total_months AS (
    SELECT COUNT(DISTINCT DATE_TRUNC('month', order_date)) AS total_months
    FROM orders
)
SELECT customer_id
FROM customer_months c
CROSS JOIN total_months t
WHERE c.months_active = t.total_months;


๐Ÿ“Œ Question 19: Find Top Selling Product Category 
Tables: products (product_id, category) | sales (product_id, quantity)

SELECT category, SUM(quantity) AS total_sold
FROM sales s
JOIN products p ON s.product_id = p.product_id
GROUP BY category
ORDER BY total_sold DESC
LIMIT 1;


๐Ÿ“Œ Question 20: Calculate Median Salary 
Table: employees (employee_id, salary)

SELECT PERCENTILE_CONT(0.5)
WITHIN GROUP (ORDER BY salary)
AS median_salary
FROM employees;


๐Ÿ’ก Double Tap โค๏ธ For More
  • โค 17
More from @sqlspecialist
  1. Oct 8, 2026๐ŸŽ“ ๐— ๐—ถ๐—ฐ๐—ฟ๐—ผ๐˜€๐—ผ๐—ณ๐˜ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ฒ๐˜€! ๐Ÿš€๐Ÿ”ฅ Upgrโ€ฆ
  2. Oct 7, 2026๐Ÿ“Š Kandinsky 6.0 Video: AI-Powered Content Creation for Analysts The new Kandinsky 6.0 Vidโ€ฆ
  3. Oct 7, 2026Alternatively, depending on the Excel version and requirement, I could use functions suchโ€ฆ
  4. Oct 7, 2026๐Ÿ“Š Data Analyst Interview Series โ€” Part 5 Guys, let's continue our Data Analyst Interviewโ€ฆ
  5. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  6. Oct 7, 2026๐Ÿ”Ÿ What is the difference between UNION and JOIN? Sample Answer: "JOIN combines columns frโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’