TGViewer
Coding Interview Resources Coding Interview Resources @crackingthecodinginterview ยท 52.2K subscribers
Post #3071 1.18K
๐Ÿง  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
  • โค 4
More from @crackingthecodinginterview
  1. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
  2. Sep 29, 2026โœ… Daily Coding Habits That Make You a Better Developer ๐Ÿง ๐Ÿ’ปโœจ 1๏ธโƒฃ Code Every Day (Even 30 Mโ€ฆ
  3. Sep 29, 2026๐—™๐—ฅ๐—˜๐—˜ ๐—ฅ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ๐˜€ ๐—ง๐—ผ ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป ๐—”๐—œ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ๐Ÿš€ โ€‹ Explore 6 free resourceโ€ฆ
  4. Sep 28, 2026Hereโ€™s a DSA problem-solving cheat sheet that will help you solve 90โ€“95% of questions thatโ€ฆ
  5. Sep 28, 2026๐ŸŽ“ ๐—›๐—”๐—ฅ๐—ฉ๐—”๐—ฅ๐—— ๐—จ๐—ก๐—œ๐—ฉ๐—˜๐—ฅ๐—ฆ๐—œ๐—ง๐—ฌ ๐—™๐—ฅ๐—˜๐—˜ ๐—ข๐—ก๐—Ÿ๐—œ๐—ก๐—˜ ๐—–๐—ข๐—จ๐—ฅ๐—ฆ๐—˜๐—ฆ ๐Ÿ˜ Dreaming ofโ€ฆ
  6. Sep 27, 2026Top 21 skills to learn this year ๐Ÿ‘‡ 1. Artificial Intelligence and Machine Learning: Underโ€ฆ
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 โ†’