Write a query to display all Engineering employees ordered by salary from highest to lowest?
Interview preparation resource from Gate Smashers.
SELECT employee_name, salary FROM Employees WHERE department = 'Engineering' ORDER BY salary DESC;
| employee_id | employee_name | department | salary | manager_id |
|---|---|---|---|---|
| 101 | Aarav | Engineering | 95000 | 105 |
| 102 | Meera | Engineering | 72000 | 105 |
| 103 | Kabir | Sales | 65000 | 106 |
| 104 | Isha | Sales | 92000 | 106 |
| 105 | Rohan | Engineering | 90000 | NULL |
| 106 | Neha | Sales | 88000 | NULL |
SELECT employee_name, salary
FROM Employees
WHERE department = 'Engineering'
ORDER BY salary DESC;Direct answer
Filter the Employees table to Engineering rows and sort the result by salary in descending order. The SQL query below returns employee_name and salary for Engineering employees with the highest salaries first.
How it works
The query uses a WHERE clause to restrict rows to the Engineering department and an ORDER BY clause to place the highest salary first.
- Filter: WHERE removes employees from other departments before the final result is returned.
- Sort: ORDER BY salary DESC places the highest salary first.
Dataset / Schema
The query operates on the Employees table which contains the following columns: employee_id, employee_name, department, salary, manager_id. The sample data from the supplied material is shown in the table block below.
Deterministic ordering (note)
If multiple employees share the same salary and you need a deterministic result order, add a second sort key such as employee_id to ORDER BY (for example: ORDER BY salary DESC, employee_id).
