The SQL ORDER BY clause is used to sort the data in ascending or descending order, based on one or more columns. Some databases sort the query results in an ascending order by default.
SELECT column-list FROM table_name [WHERE condition] [ORDER BY column1, column2, .. columnN] [ASC | DESC];
Where, ASC: It is used to sort the result set in ascending order by expression
Where, DESC: It sorts the result set in descending order by expression.
CUSTOMER_ID | NAME | ADDRESS |
---|---|---|
12 | Kathrin | US |
23 | David | Bangkok |
34 | Alina | Dubai |
45 | John | UK |
56 | Harry | US |
SELECT * FROM CUSTOMER ORDER BY NAME;
CUSTOMER_ID | NAME | ADDRESS |
---|---|---|
34 | Alina | Dubai |
23 | David | Bangkok |
56 | Harry | US |
45 | John | UK |
12 | Kathrin | US |
SELECT * FROM CUSTOMER ORDER BY NAME DESC;
CUSTOMER_ID | NAME | ADDRESS |
---|---|---|
12 | Kathrin | US |
45 | John | UK |
56 | Harry | US |
23 | David | Bangkok |
34 | Alina | Dubai |