TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst ยท 76.7K subscribers
Post #2560 2.03K
๐Ÿš€ SQL Scenario-Based Interview Questions with Answers (Part 9)

๐Ÿ’ผ FAANG & Product Company SQL Interview Scenarios

๐Ÿ“Œ Question 81: Find Users Who Were Active on 3 Consecutive Days

Table: user_activity (user_id, activity_date)

WITH activity AS (

SELECT DISTINCT

user_id,

activity_date

FROM user_activity

),

groups AS (

SELECT

user_id,

activity_date,

activity_date -

ROW_NUMBER() OVER (

PARTITION BY user_id

ORDER BY activity_date

) * INTERVAL '1 day' AS grp

FROM activity

)

SELECT

user_id,

COUNT() AS consecutive_days

FROM groups

GROUP BY user_id, grp

HAVING COUNT(
) >= 3;

๐Ÿ“Œ Question 82: Find the Top 5 Selling Products in Each Category

Tables: products (product_id, category) sales (product_id, quantity)

WITH product_sales AS (

SELECT

p.category,

s.product_id,

SUM(s.quantity) AS total_quantity

FROM sales s

JOIN products p

ON s.product_id = p.product_id

GROUP BY p.category, s.product_id

)

SELECT *

FROM (

SELECT *,

DENSE_RANK() OVER (

PARTITION BY category

ORDER BY total_quantity DESC

) AS rnk

FROM product_sales

) t

WHERE rnk <= 5;

๐Ÿ“Œ Question 83: Find Customers Whose Latest Order Is Their Highest Value Order

Table: orders (customer_id, order_date, amount)

WITH customer_orders AS (

SELECT *,

ROW_NUMBER() OVER (

PARTITION BY customer_id

ORDER BY order_date DESC

) AS latest_order,

RANK() OVER (

PARTITION BY customer_id

ORDER BY amount DESC

) AS highest_order

FROM orders

)

SELECT

customer_id,

order_date,

amount

FROM customer_orders

WHERE latest_order = 1

AND highest_order = 1;

๐Ÿ“Œ Question 84: Calculate Rolling 30-Day Revenue

Table: sales (sale_date, amount)

SELECT

sale_date,

SUM(amount) OVER (

ORDER BY sale_date

RANGE BETWEEN INTERVAL '29 days' PRECEDING

AND CURRENT ROW

) AS rolling_30_day_revenue

FROM sales;

๐Ÿ“Œ Question 85: Find Products Purchased by Exactly One Customer

Table: orders (customer_id, product_id)

SELECT

product_id

FROM orders

GROUP BY product_id

HAVING COUNT(DISTINCT customer_id) = 1;

๐Ÿ“Œ Question 86: Find the Longest Inactive Period for Each Customer

Table: orders (customer_id, order_date)

WITH gaps AS (

SELECT

customer_id,

order_date,

order_date -

LAG(order_date) OVER (

PARTITION BY customer_id

ORDER BY order_date

) AS inactive_days

FROM orders

)

SELECT

customer_id,

MAX(inactive_days) AS longest_gap

FROM gaps

GROUP BY customer_id;

๐Ÿ“Œ Question 87: Calculate Revenue Share by Product Category

Tables: products (product_id, category) sales (product_id, amount)

SELECT

category,

SUM(amount) AS revenue,

ROUND(

100.0 * SUM(amount) /

SUM(SUM(amount)) OVER (),

2

) AS revenue_share

FROM sales s

JOIN products p

ON s.product_id = p.product_id

GROUP BY category

ORDER BY revenue DESC;
  • โค 1
More from @sqlanalyst
  1. Oct 9, 2026SQL Interview Series โ€” Part 5 ๐Ÿ“Œ Question 5: Find Employees Who Earn More Than Their Managโ€ฆ
  2. Oct 9, 2026๐Ÿ‡ฎ๐Ÿ‡ณ ๐—š๐—ข๐—ฉ๐—˜๐—ฅ๐—ก๐— ๐—˜๐—ก๐—ง ๐—ข๐—™ ๐—œ๐—ก๐——๐—œ๐—” โ€” ๐—”๐—œ๐—–๐—ง๐—˜ ๐—œ๐—ก๐—ง๐—˜๐—ฅ๐—ก๐—ฆ๐—›๐—œ๐—ฃ๐—ฆ ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ ๐Ÿš€โ€ฆ
  3. Oct 8, 2026๐ŸŽ“ ๐— ๐—ถ๐—ฐ๐—ฟ๐—ผ๐˜€๐—ผ๐—ณ๐˜ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ฒ๐˜€! ๐Ÿš€๐Ÿ”ฅ Upgrโ€ฆ
  4. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  5. Oct 7, 2026SQL Interview Series โ€” Part 4 ๐Ÿ“Œ Question 4: Find the Highest Salary in Each Department Suโ€ฆ
  6. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
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 โ†’