The SQL ORDER BY ASC is used to sort records in the result set in ascending order. This is the default behavior of most of the databases. That means if you miss the ASC keyword after the ORDER BY clause, the database by default sorts the data in ascending order.
Syntax
SELECT expressions FROM tables [WHERE conditions] ORDER BY expression ASC;
Here,
- expressions – expressions defined here the column(s) or calculation you want to retrieve. If you want to retrieve all the columns simply use * in the place of expressions.
- tables – one or more than one table from where you want to retrieve data.
- WHERE conditions – Optional. This is used to specify some conditions while selecting data. In case you are not using the WHERE clause, all the rows available will be selected.
- ORDER BY – This argument is used to sort the result set. If you want to sort on more than one column, you need to provide them in comma-separated.
- ASC – ASC sort the result set in ascending order. This is the default behavior if no modifier is mentioned.
Example
Consider an Employee
table with the following data.
ID | NAME | GENDER | AGE | SALARY |
1510 | Avinash | M | 35 | 33000 |
1511 | Ramjan | M | 31 | 56000 |
1512 | Priyanka | F | 32 | 35000 |
1513 | Priyam | M | 25 | 42000 |
1514 | Jitu | M | 23 | 43000 |
1515 | Raman | M | 26 | 49000 |
1516 | Lata | F | 25 | 27000 |
1517 | Prakash | M | 38 | 45000 |
1518 | Nitu | F | 40 | 45000 |
1519 | Pallab | M | 33 | 42000 |
1520 | Pankaj | M | 39 | 55000 |
The below statement sorts the result in ascending order by NAME
and SALARY
.
SELECT * FROM Employee ORDER BY NAME, SALARY;
This produces the following result:
ID | NAME | GENDER | AGE | SALARY |
1510 | Avinash | M | 35 | 33000 |
1514 | Jitu | M | 23 | 43000 |
1516 | Lata | F | 25 | 27000 |
1518 | Nitu | F | 40 | 45000 |
1519 | Pallab | M | 33 | 42000 |
1520 | Pankaj | M | 39 | 55000 |
1513 | Priyam | M | 25 | 42000 |
1512 | Priyanka | F | 32 | 35000 |
1517 | Prakash | M | 38 | 45000 |
1515 | Raman | M | 26 | 49000 |
1511 | Ramjan | M | 31 | 56000 |
1 thought on “SQL ORDER BY Ascending”