๐ง SQL Interview Question (Commonly Asked)
๐
products(product_id, product_name, category_id, price)
โ Ques :
๐ Find the second highest priced product in each category.
๐งฉ How Interviewers Expect You to Think
โข Partition data by category
โข Rank products based on price (descending)
โข Understand difference between RANK, DENSE_RANK, and ROW_NUMBER
โข Handle ties properly
โข Filter after ranking logic
๐ก SQL Solution
WITH ranked_products AS (
SELECT
product_id,
product_name,
category_id,
price,
DENSE_RANK() OVER (
PARTITION BY category_id
ORDER BY price DESC
) AS price_rank
FROM products
)
SELECT
product_id,
product_name,
category_id,
price
FROM ranked_products
WHERE price_rank = 2;
๐ฅ Why this question is powerful:
โข Tests window functions deeply
โข Checks ranking logic understanding
โข Very common in Data Analyst interviews
โค๏ธ React if you want more scenario-based SQL questions
Post #3071
1.18K
- โค 4