<-- Chapter 16: SQL And & Or
Chapter 17
SQL ORDER BY
ORDER BY is the keyword used to sort the database table data by one or more columns. ORDER BY sort the Records in Ascending order by default. To sort the Record in Descending Order, we have to use DESC keyword.
Lets see an example from the below table "employees" :-
employee_id |
name |
code |
designation |
salary |
101 |
ABC |
E-101 |
Engineer |
12000 |
102 |
DEF |
E-102 |
Doctor |
8000 |
103 |
GHI |
E-103 |
Software Developer |
8000 |
104 |
JKL |
E-104 |
CEO |
12000 |
105 |
MNO |
E-105 |
Software Developer |
100000 |
ORDER BY SQL Statement Example :-
SELECT *
FROM `employees`
ORDER BY `designation` DESC;
Now Run this Query, we will see the output below :-
employee_id |
name |
code |
designation |
salary |
105 |
MNO |
E-105 |
Software Developer |
100000 |
103 |
GHI |
E-103 |
Software Developer |
8000 |
101 |
ABC |
E-101 |
Engineer |
12000 |
102 |
DEF |
E-102 |
Doctor |
8000 |
104 |
JKL |
E-104 |
CEO |
12000 |
We can see here, records are sorted on the basis of column designation values in Descending Order.ORDER BY also works with more than one column. Lets see the example below
SELECT *
FROM `employees`
ORDER BY `designation`,`name`;
Now Run this Query, we will see the output below :-
employee_id |
name |
code |
designation |
salary |
104 |
JKL |
E-104 |
CEO |
12000 |
102 |
DEF |
E-102 |
Doctor |
8000 |
101 |
ABC |
E-101 |
Engineer |
12000 |
103 |
GHI |
E-103 |
Software Developer |
8000 |
105 |
MNO |
E-105 |
Software Developer |
100000 |
We can see here, records are sorted on the basis of two columns designation and name values in Ascending Order. Here the SQL Statement will give first priority to designation column and it will sort the data according to designation column values and then it will sort for column name.Similarly like this we can do the same for Descending Sorting.
Chapter 18: SQL INSERT -->
0 Comment(s)