Skip to content
Activities · Activity 8.9

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;

Yanam CbseNCERTSubjective· 2mImportance★★★★★est
40% · 18/45 Questions
🔒 Locked · start free trial →

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;
EmpNoEnameSalaryBonusDeptId
101Aaliya10000234D02
107Vergese15000NULLD01
104Gurpreet19000565D04
108Nachaobi29000NULLD05
105Joseph34000875D03
109Daribha42000NULLD04
103Shabbir45000566D01
106Sanya48000695D02
110Tanya50000467D05
102Kritika60000123D01

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?

  1. Rows are first arranged by the first column (Salary, ascending).
  2. 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.