SELECT | ENAME,JOB,SAL |
FROM | EMP |
WHERE | JOB = 'SALESMAN' |
ORDER BY | SAL |
To sort records, use ORDER BY, where ORDER BY is followed by the column name to be used as the sort key. To use multiple sort keys, use a comma-separated list of column names, such as ORDER BY SAL,DEPTNO.
ORDER BY is written after the WHERE condition; if no WHERE condition is specified, the ORDER BY clause is written after the FROM clause.
Normally sorted in ascending order, but can be sorted in descending order by adding the DESC keyword, as in ORDER BY SAL DESC. To explicitly sort in ascending order, add the ASC keyword.
ASC | ascending-order |
DESC | descending-order |
Now for the Practice, I will give you two questions.
employee code | Name | Department Code | salary |
10010 | Ito Sachiko | 101 | 200,000 |
10020 | Saito Eiji | 201 | 300,000 |
10030 | Suzuki Yuichi | 101 | 250,000 |
10040 | Honda Kazu | 102 | 350,000 |
10050 | Yamada Goro | 102 | 300,000 |
10060 | Wakayama Mari | 201 | 250,000 |
Department Code | employee code | Name |
101 | 10010 | Ito Sachiko |
101 | 10030 | Suzuki Yuichi |
102 | 10040 | Honda Kazu |
102 | 10050 | Yamada Goro |
201 | 10020 | Saito Eiji |
201 | 10060 | Wakayama Mari |
A | SELECT * FROM TableA WHERE Department Code <> NULL | |
B | SELECT Department Code,employee code,Name FROM TableA | |
C | SELECT Department Code,employee code,Name FROM TableA GROUP BY Department Code | |
D | SELECT Department Code,employee code,Name FROM TableA ORDER BY Department Code |
This completes [Sort (ORDER BY)].