Order By clause is used to sorts the record in Ascending or Descending Order.
1) ORDER BY ASC:- It is used to sorts the record in ascending order.
SELECT * FROM table_name OREDR BY column_name ASC
Example:- Suppose we have employees table which has 7 records
+----+------------+-----------+----------------+------------+
| id | first_name | last_name | email | address |
+----+------------+-----------+----------------+------------+
| 1 | John | Tailor | john@abc.com | California |
| 2 | Rom | Tailor | rom@abc.com | California |
| 3 | Andrew | Symonds | andrew@abc.com | Sydney |
| 4 | Miacle | clerk | miacle@abc.com | sydney |
| 5 | Sachin | Tendulkar | sachin@abc.com | Mumbai |
| 6 | Virat | Kohli | virat@abc.com | delhi |
| 7 | rohit | NULL | rohit@abc.com | NULL |
+----+------------+-----------+----------------+------------+
Now, we want to get the record Order by first_name in ascending order
SELECT * FROM employees ORDER BY first_name ASC
Output:-
+----+------------+-----------+----------------+------------+
| id | first_name | last_name | email | address |
+----+------------+-----------+----------------+------------+
| 3 | Andrew | Symonds | andrew@abc.com | Sydney |
| 1 | John | Tailor | john@abc.com | California |
| 4 | Miacle | clerk | miacle@abc.com | sydney |
| 7 | rohit | NULL | rohit@abc.com | NULL |
| 2 | Rom | Tailor | rom@abc.com | California |
| 5 | Sachin | Tendulkar | sachin@abc.com | Mumbai |
| 6 | Virat | Kohli | virat@abc.com | delhi |
+----+------------+-----------+----------------+------------+
2) ORDER BY DESC:- It is used to sorts the record in descending order.
Syntax:-
SELECT * FROM table_name OREDR BY column_name DESC
Now, we want to get the record Order by first_name in descending order
SELECT * FROM employees ORDER BY first_name DESC
Output:-
+----+------------+-----------+----------------+------------+
| id | first_name | last_name | email | address |
+----+------------+-----------+----------------+------------+
| 6 | Virat | Kohli | virat@abc.com | delhi |
| 5 | Sachin | Tendulkar | sachin@abc.com | Mumbai |
| 2 | Rom | Tailor | rom@abc.com | California |
| 7 | rohit | NULL | rohit@abc.com | NULL |
| 4 | Miacle | clerk | miacle@abc.com | sydney |
| 1 | John | Tailor | john@abc.com | California |
| 3 | Andrew | Symonds | andrew@abc.com | Sydney |
+----+------------+-----------+----------------+------------+
SQL Order By Clause – Interview Questions
Q 1: What is ORDER BY used for?
Ans: ORDER BY sorts the result set in ascending or descending order.
Q 2: What is the default sorting order?
Ans: Ascending (ASC).
Q 3: Can ORDER BY sort by multiple columns?
Ans: Yes, sorting is applied in the specified column order.
Q 4: Can ORDER BY use column aliases?
Ans: Yes, aliases can be used.
Q 5: Does ORDER BY affect stored data?
Ans: No, it only sorts the output.
SQL Order By Clause – Objective Questions (MCQs)
Q1. Which clause is used to sort the result set?
Q2. What is the default sorting order of ORDER BY?
Q3. Which keyword is used for descending order?
Q4. Can ORDER BY be used with multiple columns?
Q5. ORDER BY clause is written at which position in SELECT query?