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 | +----+------------+-----------+----------------+------------+