DISTINCT Keyword
The DISTINCT keyword is used to return only distinct (different) values. Consider:
mysql> select POSITION from people;
+------------+ | POSITION | +------------+ | Manager | | Technician | | Clerk | | Manager | +------------+4 rows in set (0.00 sec) mysql> select distinct POSITION from people;
+------------+ | POSITION | +------------+ | Manager | | Technician | | Clerk | +------------+3 rows in set (0.02 sec) mysql>
ORDER BY
The ORDER BY clause is used to sort the records (rows). Sorting is by numbers, text or dates. If the values are text, it is an alphabetical sorting. Try it.
mysql> select ID, POSITION from people
-> order by ID;
+--------+------------+ | ID | POSITION | +--------+------------+ | bsmith | Clerk | | nsmith | Manager | | rjones | Manager | | tjones | Technician | +--------+------------+4 rows in set (0.00 sec) mysql>
To order by more than one field, use commas. Try it.
mysql> select POSITION, NAME_LAST from people
-> order by POSITION, NAME_LAST;
+------------+-----------+ | POSITION | NAME_LAST | +------------+-----------+ | Clerk | Smith | | Manager | Jones | | Manager | Smith | | Technician | Jones | +------------+-----------+4 rows in set (0.00 sec) mysql>
To sort in descending order, use the DESC Keyword. Try it.
mysql> select POSITION, NAME_LAST from people
-> order by POSITION desc;
+------------+-----------+ | POSITION | NAME_LAST | +------------+-----------+ | Technician | Jones | | Manager | Jones | | Manager | Smith | | Clerk | Smith | +------------+-----------+4 rows in set (0.00 sec) mysql>