TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst ยท 76.7K subscribers
Post #2549 2.33K
๐Ÿš€ SQL Scenario Based Interview Questions with Answers: Part-5

๐Ÿ“Œ Question 41: Find Customers Whose Spending Decreased for 3 Consecutive Months 

Table: orders (customer_id, amount, order_date)

WITH monthly_spend AS (

    SELECT

        customer_id,

        DATE_TRUNC('month', order_date) AS month,

        SUM(amount) AS revenue

    FROM orders

    GROUP BY customer_id, DATE_TRUNC('month', order_date)

),

spend_trend AS (

    SELECT *,

           LAG(revenue,1) OVER(PARTITION BY customer_id ORDER BY month) AS prev1,

           LAG(revenue,2) OVER(PARTITION BY customer_id ORDER BY month) AS prev2

    FROM monthly_spend

)

SELECT customer_id, month

FROM spend_trend

WHERE revenue < prev1

  AND prev1 < prev2;

๐Ÿ“Œ Question 42: Find the Median Order Amount for Each Month 

Table: orders (order_id, amount, order_date)

SELECT

    DATE_TRUNC('month', order_date) AS month,

    PERCENTILE_CONT(0.5)

        WITHIN GROUP (ORDER BY amount) AS median_order

FROM orders

GROUP BY DATE_TRUNC('month', order_date);

๐Ÿ“Œ Question 43: Find Customers Who Ordered on Every Weekend 

Table: orders (customer_id, order_date)

SELECT

    customer_id

FROM orders

WHERE EXTRACT(DOW FROM order_date) IN (0,6)

GROUP BY customer_id

HAVING COUNT(DISTINCT order_date) >= 8;

๐Ÿ“Œ Question 44: Find the Top 5% Highest Revenue Customers 

Table: orders (customer_id, amount)

WITH revenue AS (

    SELECT

        customer_id,

        SUM(amount) AS total_revenue

    FROM orders

    GROUP BY customer_id

)

SELECT *

FROM (

    SELECT *,

           NTILE(20) OVER (ORDER BY total_revenue DESC) AS bucket

    FROM revenue

) t

WHERE bucket = 1;

๐Ÿ“Œ Question 45: Find the Most Frequently Returned Product 

Tables:

sales (order_id, product_id)

returns (order_id)

SELECT

    s.product_id,

    COUNT(*) AS return_count

FROM sales s

JOIN returns r

ON s.order_id = r.order_id

GROUP BY s.product_id

ORDER BY return_count DESC

LIMIT 1;

๐Ÿ“Œ Question 46: Calculate Average Delivery Time 

Table: deliveries (order_id, order_date, delivery_date)

SELECT

    ROUND(

        AVG(delivery_date - order_date),

        2

    ) AS avg_delivery_days

FROM deliveries;

๐Ÿ“Œ Question 47: Find Users Who Logged In Every Day Last Week 

Table: logins (user_id, login_date)

SELECT

    user_id

FROM logins

WHERE login_date >= CURRENT_DATE - INTERVAL '6 day'

GROUP BY user_id

HAVING COUNT(DISTINCT login_date) = 7;

๐Ÿ“Œ Question 48: Find Products with Revenue Above Category Average 

Tables:

products (product_id, category)

sales (product_id, amount)

WITH product_revenue AS (

    SELECT

        p.product_id,

        p.category,

        SUM(s.amount) AS revenue

    FROM products p

    JOIN sales s

      ON p.product_id = s.product_id

    GROUP BY p.product_id, p.category

)

SELECT *

FROM (

    SELECT *,

           AVG(revenue) OVER (

               PARTITION BY category

           ) AS category_avg

    FROM product_revenue

) t

WHERE revenue > category_avg;

๐Ÿ“Œ Question 49: Find the Busiest Day of the Week 

Table: orders (order_date)

SELECT

    TO_CHAR(order_date, 'Day') AS weekday,

    COUNT(*) AS total_orders

FROM orders

GROUP BY weekday

ORDER BY total_orders DESC

LIMIT 1;

๐Ÿ“Œ Question 50: Calculate Customer Retention After First Purchase

Table: orders (customer_id, order_date)

WITH customer_orders AS (

    SELECT

        customer_id,

        COUNT() AS total_orders

    FROM orders

    GROUP BY customer_id

)

SELECT

    ROUND(

        100.0 *

        COUNT(CASE WHEN total_orders > 1 THEN 1 END)

        / COUNT(
),

        2

    ) AS retention_rate

FROM customer_orders;

๐ŸŽฏ Concepts Covered:

โœ… Window Functions

โœ… Percentiles

โœ… NTILE()

โœ… Retention Analysis

โœ… Revenue Analytics

โœ… Delivery KPIs

โœ… Customer Behavior Analysis

โœ… Advanced Business SQL 

โค๏ธ Double Tap For More
  • โค 9
More from @sqlanalyst
  1. Oct 8, 2026๐ŸŽ“ ๐— ๐—ถ๐—ฐ๐—ฟ๐—ผ๐˜€๐—ผ๐—ณ๐˜ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ฒ๐˜€! ๐Ÿš€๐Ÿ”ฅ Upgrโ€ฆ
  2. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  3. Oct 7, 2026SQL Interview Series โ€” Part 4 ๐Ÿ“Œ Question 4: Find the Highest Salary in Each Department Suโ€ฆ
  4. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
  5. Sep 29, 2026SQL Interview Series โ€” Part 2 ๐Ÿ“Œ Question 2: Find Duplicate Records Suppose you have an Emโ€ฆ
  6. Sep 29, 2026๐—™๐—ฅ๐—˜๐—˜ ๐—ฅ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ๐˜€ ๐—ง๐—ผ ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป ๐—”๐—œ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ๐Ÿš€ โ€‹ Explore 6 free resourceโ€ฆ
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 โ†’