HomeInterview QuestionsWhich department ranks third in revenue?

Which department ranks third in revenue?

🟡 Medium Coding Junior level
1Times asked
May 2026Last seen
May 2026First seen

💡 Model Answer

Use a window function to rank departments by revenue and then filter for rank 3. Example:

WITH dept_rev AS (

SELECT v.department,

     SUM(b.amount) AS revenue,
     ROW_NUMBER() OVER (ORDER BY SUM(b.amount) DESC) AS rn

FROM visits v

JOIN billing b ON v.visit_id = b.visit_id

GROUP BY v.department

)

SELECT department, revenue

FROM dept_rev

WHERE rn = 3;

This returns the department that has the third highest total revenue.

This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.

🎤 Get questions like this answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers — invisible to screen sharing.

Get Assisting AI — Starts at ₹500