SQL order by

SQL order by is a keyword that is used to display data in ascending or descending order. On ordering ascending, your data will move from A to Z, and on descending order your data will reverse and move from Z to A. Let’s know how to use SQL order by.

Pay attention when you extract the data from a table, then that data already comes in ascending order. So, in that case, the DESC keyword is used to put the data in descending order.

There are many ways to SQL order by like order by descending, order by ascending, order by multiple, etc. Let’s know how to use order by in SQL.

SQL Order By Ascending

To show your data in ascending order, you only have to use the ORDER BY keyword. If you use the ASC keyword, you will get the same result as with the ORDER BY keyword. In the following examples, you will know how to use order by ascending.

/ ***** Displayed the name in ascending order ***** /

SELECT * FROM Employees
ORDER BY name;

/ ***** Displayed the age in ascending order ***** /

SELECT * FROM Employees
ORDER BY age;

/ ***** Displayed the salary in ascending order ***** /

SELECT * FROM Employees
ORDER BY salary;

/ ***** Displayed the joinDate in ascending order ***** /

SELECT * FROM Employees
ORDER BY joinDate;

/ ***** Displayed the name and age in ascending order ***** /

SELECT name, age FROM Employees
ORDER BY name;
EmployeeIDNameAgeSalaryJoinDate
1abc20200019-2-2020
2efg30540010-2-2019
3hij40560016-4-20015
4klm50360010-5-2004
5nop60240015-2-2009
6qrs70570010-6-2018
7tuv80800025-12-2016
8wxy90980010-7-2005
9zzz100520030-9-2014

SQL Order By Descending

To show your data in Descending Order, you have to use the DESC keyword. In the following examples, you will know how to use order by descending.

/ ***** Displayed the name in descending order ***** /

SELECT * FROM Employees
ORDER BY name DESC;

/ ***** Displayed the age in descending order ***** /

SELECT * FROM Employees
ORDER BY age DESC;

/ ***** Displayed the salary in descending order ***** /

SELECT * FROM Employees
ORDER BY salary DESC;

/ ***** Displayed the joinDate in descending order ***** /

SELECT * FROM Employees
ORDER BY joinDate DESC;

/ ***** Displayed the name and age in descending order ***** /

SELECT name, age FROM Employees
ORDER BY name DESC;
EmployeeIDNameAgeSalaryJoinDate
1abc20200019-2-2020
2efg30540010-2-2019
3hij40560016-4-20015
4klm50360010-5-2004
5nop60240015-2-2009
6qrs70570010-6-2018
7tuv80800025-12-2016
8wxy90980010-7-2005
9zzz100520030-9-2014

Order By Multiple

/ ***** Displayed the name and age in ascending order ***** /

SELECT * FROM Employees
ORDER BY name, age;

/ ***** Displayed the name and age in descending order ***** /

SELECT * FROM Employees
ORDER BY name, age DESC;

/ ***** Displayed the name and age ***** /

SELECT * FROM Employees
ORDER BY name ASC, age DESC;

Other Order By Examples

/ ***** Displayed to those younger than 70 ***** /

SELECT * FROM Employees
WHERE age <=70 ORDER BY age;

/ ***** Displayed to those whose salary is above 5000 ***** /

SELECT * FROM Employees
WHERE salary >=5000 ORDER BY salary;

/ ***** Displayed the data using LIMIT ***** /

SELECT * FROM Employees
WHERE salary >=5000 ORDER BY salary LIMIT 5;
/ ***** Displayed salary from 2000 to 5000 ***** /

SELECT * FROM Employees
WHERE salary >=2000 AND salary <=5000 ORDER BY salary;

SQL Keywords

We have some keywords of SQL available, which are used most commonly.
Let’s know what those keywords are and what they mean.

NoKeywordsDescription
1CREATEIt is used to create a database and table
2CREATE DATABASEIt is used to create a database only
3CREATE TABLEIt is used to create a table
4DELETEIt is used to delete record from the table
5DROPIt is used to delete a column, database, and table
6DROP COLUMNIt is used to delete a column in the table
7DROP TABLEIt is used to delete a table
8FOREIGN KEYIt is used to join two tables together
9JOINIt is used to join the tables
10UPDATEIt is used to update the record in the table

Note: All the examples you have seen inside this article have all been made by us and tested thoroughly. So you can use all these without any difficulty.

Related: 16 most useful examples of SQL between and not between