Distinct Keyword, Order By Clause


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>

And/Or, Between   < <  PREVIOUS   Table of Contents NEXT  > >   SQL Review

Developed with HTML-Kit
Sandersongs Web Tutorials
Contact the Webmasterwith comments.
©2017, by Bill Sanders, all rights reserved.
This domain had 3,995 different visits in the last 30 days.
435,561 hits on this domain since 24 Nov 2006.
http://www.sandersongs.com/PHPsqlCourse/SQL09.php
This page was last modified on our server on 4 Jun 2008
and last refreshed on our server at 3:56 am, MST
This file took 0.00953 seconds to process.