Schema and sample data
| emp_id | name | department | salary |
|---|---|---|---|
| 1 | Ava Torres | Engineering | 72000.00 |
| 2 | Ben Ncube | Sales | 48000.00 |
| 3 | Cara Singh | Engineering | 95000.00 |
| 4 | Deng Liu | Support | 50000.00 |
| 5 | Emma Rossi | Sales | 61000.00 |
Show setup SQL (copy to run)
CREATE TABLE Employees (
emp_id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
department VARCHAR(30),
salary DECIMAL(10,2) NOT NULL
);
INSERT INTO Employees (emp_id, name, department, salary) VALUES
(1, 'Ava Torres', 'Engineering', 72000.00),
(2, 'Ben Ncube', 'Sales', 48000.00),
(3, 'Cara Singh', 'Engineering', 95000.00),
(4, 'Deng Liu', 'Support', 50000.00),
(5, 'Emma Rossi', 'Sales', 61000.00);
From the Employees table, return the name and salary of every employee who earns more than 50000. Sort the result by salary from highest to lowest.
Expected result
| name | salary |
|---|---|
| Cara Singh | 95000.00 |
| Ava Torres | 72000.00 |
| Emma Rossi | 61000.00 |
Show hint
You need a WHERE clause to keep only the rows above the threshold, then ORDER BY to sort them. Remember that "more than 50000" is a strict greater-than, so the employee earning exactly 50000 should not appear.
Try to write the query yourself before you open this.
Show solution and explanation
SELECT name, salary
FROM Employees
WHERE salary > 50000
ORDER BY salary DESC;
The WHERE salary > 50000 filter runs first, keeping only the three rows whose salary is strictly greater than 50000. Deng Liu earns exactly 50000, and because the comparison is a strict greater-than (not >=), that row is excluded. This is the single most common off by one mistake on filtering problems, so read the wording carefully.
ORDER BY salary DESC then sorts the surviving rows from highest to lowest. Without DESC the default is ascending order. For a refresher on how the clauses run, see the logical query processing order: WHERE is applied before ORDER BY, which is why the filter shrinks the set before it is sorted.
Only the requested columns are selected. Avoid SELECT * in real queries: naming the columns you need keeps results predictable and lets an index cover the query. See why to avoid SELECT star.