Which query gives the row number based on salary for each department? You need to assign a row number.
💡 Model Answer
The query uses the ROW_NUMBER() window function to generate a sequential number for each row within a department, ordered by salary. A typical solution looks like this:
SELECT
department,
employee_id,
salary,
ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS salary_rank
FROM employees;
This partitions the result set by department, orders each partition by salary in descending order, and assigns a unique row number starting at 1 for the highest salary. The approach runs in O(n log n) time due to the sort, but modern RDBMS engines optimize window functions efficiently. If you want to treat ties specially, replace ROW_NUMBER() with RANK() or DENSE_RANK().
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 on a discreet on-screen overlay.
Get Assisting AI — Starts at ₹500