📎 Referral Code: —
🎁 Referral Earnings ₹0 →
📊 Dashboard Sign In
🎁 ₹0
Your Referral Earnings → Tap to view
Buy
🛒
Buy Course
Videos
📘
Basic Videos
🚀
Project Related Videos
🐍
Old Videos Style — will be removed soon
📚
Buy Book
After Course Completion
📒
My Interview Tracker — HR calls & follow-ups
📈
My Performance — Track your progress
🎯
Interview Prep — Resume & questions for a job
🏢
Company Interview Questions — with answers
🤖
Central Command — your HQ bot
💼
Jobs — AWS Data Engineer openings
Personal Training
🎬
Live Classes
🎯
My 1:1 Dashboard
🗺️
Course Roadmap
🎤
Mock Interview
🧑‍💼
HR Discussion
🗓️
Slot Booking
📅
My Booked Slots
Passive Income
🎁
Refer & Earn — ₹1,000 per referral
🤝
Browse Job Support
💰
Become a Supporter
🔗
Browse Referrals
📢
Post a Referral
🎓
Course Marketplace — Other Trainers
🧑‍🏫
Teach a Course
📊
My Courses & Earnings
🎬
Reels
AI Tools
🌐
Project Explanation Agent
👤
My Profile
🏢
Recruiter Board
🛟
Support Works

← Stellantis interview questions

🎬No video for this question yet. The written answer is below.

Rank vs Row Number vs Dense_Rank?
  • Write a query using LAG function. What are its use cases?
  • How to handle large joins efficiently in SQL?

🏢 Stellantis 🏷️ SQL #7 of 7

📝 Answer

Interviewer:
Can you explain the difference between RANK, ROW_NUMBER, and DENSE_RANK?
Candidate:
So basically, let me break this down step by step in a way I usually think about it during real-time implementations. First, whenever I hear this question, I immediately think about ranking use cases, like top customers, highest sales, or even deduplication scenarios. Let me start with ROW_NUMBER. ROW_NUMBER is the simplest one. It assigns a unique number to every row. Even if two rows have the same value, it will still give different numbers. So there is no concept of ties here. In my project, I used ROW_NUMBER mainly for deduplication. For example, when I had multiple records for the same property coming from different sources, I used ROW_NUMBER with partition by property_id and ordered by last_updated timestamp. Then I picked only the first row. Now coming to RANK. RANK handles ties. If two rows have the same value, they get the same rank. But the important thing is… it skips the next rank. So if two records are ranked 1, the next one will be rank 3. I used this in reporting scenarios where gaps were acceptable, like competition ranking. Now DENSE_RANK is similar to RANK, but it does not skip numbers. If two rows are ranked 1, the next rank will be 2. In my dashboards, I preferred DENSE_RANK because business users usually don’t like gaps in ranking. So in simple terms… ROW_NUMBER is for uniqueness, RANK is for ranking with gaps, and DENSE_RANK is for continuous ranking.
Interviewer:
Can you write a query using the LAG function and explain its use cases?
Candidate:
So basically, LAG is something I’ve used a lot when working with time-series or sequential data. Let me explain how I approach it. First, LAG allows me to access the previous row’s value without doing a self join. That itself makes the query much cleaner and efficient. Let me show a simple example from one of my real-time scenarios.
SELECT 
    property_id,
    city,
    sale_date,
    price,
    LAG(price) OVER (PARTITION BY city ORDER BY sale_date) AS previous_price,
    price - LAG(price) OVER (PARTITION BY city ORDER BY sale_date) AS price_difference
FROM property_sales;
So what I’m doing here is… for each city, I’m ordering the data by sale_date and getting the previous price. In my project, I used this for analyzing price trends over time. For example, I needed to calculate how much the property price increased compared to the last transaction. Another use case I worked on was anomaly detection. If the price difference was unusually high, we flagged that record. So in simple terms, LAG is very useful for comparing current and previous values without complex joins.
Interviewer:
How do you handle large joins efficiently in SQL?
Candidate:
This is actually something I faced in production, so let me explain how I usually approach it. First, I don’t directly jump into writing the join. I start by understanding the size of both tables and the type of join required. In one of my projects, I had to join a huge transaction dataset with a reference dataset. Initially, the query was taking a long time. So first, what I did was… I reduced the data as early as possible. I applied filters before joining, so that only required data participates in the join. Then I checked indexing. Making sure that join columns are properly indexed made a big difference. In distributed systems like Databricks, I also used partitioning strategies. For example, partitioning by date or region helped reduce data shuffling. Another thing I did was… if one table is small, I used broadcast join. This avoided moving large data across nodes. Sometimes, I also broke down large joins into multiple steps by creating intermediate tables. It made the execution more manageable. One important lesson I learned is… performance issues in joins are mostly about data movement, not just SQL syntax. So I always check execution plans and understand how data is flowing. So basically, my approach is… filter early, index properly, use partitioning, and choose the right join strategy based on data size.
Interviewer (Follow-up):
That’s great. Can you explain a situation where a bad join strategy caused a production issue and how you fixed it?