Q.Execute the following two queries and find out what will happen if we specify two columns in the ORDER BY clause:
SELECT *
FROM EMPLOYEE
ORDER BY Salary,
Bonus;
SELECT *
FROM EMPLOYEE
ORDER BY Salary,Bonus
desc;
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →With two columns in ORDER BY, the first column is the primary sort key; the second column is used only to break ties among rows with equal first-column values.
desc written after Bonus applies to Bonus only — Salary still sorts ascending. Since all 10 salaries in Table 8.8 are distinct, both queries here display the same order.
Query 1 — both columns ascending (default)
SELECT *
FROM EMPLOYEE
ORDER BY Salary,
Bonus;
| EmpNo | Ename | Salary | Bonus | DeptId |
|---|---|---|---|---|
| 101 | Aaliya | 10000 | 234 | D02 |
| 107 | Vergese | 15000 | NULL | D01 |
| 104 | Gurpreet | 19000 | 565 | D04 |
| 108 | Nachaobi | 29000 | NULL | D05 |
| 105 | Joseph | 34000 | 875 | D03 |
| 109 | Daribha | 42000 | NULL | D04 |
| 103 | Shabbir | 45000 | 566 | D01 |
| 106 | Sanya | 48000 | 695 | D02 |
| 110 | Tanya | 50000 | 467 | D05 |
| 102 | Kritika | 60000 | 123 | D01 |
10 rows, ascending by Salary. Bonus (the second key) would only be consulted for rows having the same salary.
Query 2 — second column descending
SELECT *
FROM EMPLOYEE
ORDER BY Salary,Bonus
desc;
This is one statement spread over lines, so MySQL reads it as ORDER BY Salary, Bonus DESC. The DESC keyword binds only to the column it follows — Bonus. Salary keeps its default ASC. Output: the same 10 rows in the same order as Query 1, because there are no equal salaries for the Bonus direction to act on.
So what happens with two columns in ORDER BY?
- Rows are first arranged by the first column (Salary, ascending).
- Only within a group of equal Salary values does the second column (Bonus) decide the order — ascending in Query 1, descending in Query 2. …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.