Sorting Rows : Sort Order « Select Clause « SQL / MySQL

SQL / MySQL
1. Backup Load
2. Command MySQL
3. Cursor
4. Data Type
5. Database
6. Date Time
7. Flow Control
8. Function
9. Insert Delete Update
10. Join
11. Key
12. Math
13. Procedure Function
14. Select Clause
15. String
16. Table Index
17. Transaction
18. Trigger
19. User Permission
20. View
21. Where Clause
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
SQL / MySQL » Select Clause » Sort Order 
Sorting Rows

/*
mysql> Drop table Bird;
Query OK, 0 rows affected (0.01 sec)

mysql> CREATE TABLE Bird (
    ->     name VARCHAR(20),
    ->     owner VARCHAR(20),
    ->     species VARCHAR(20),
    ->     sex CHAR(1),
    ->     birth DATE,
    ->     death DATE
    -> );
Query OK, 0 rows affected (0.07 sec)

mysql> INSERT INTO  Bird VALUES ('BlueBird','Joe','Car','f','1999-03-30',NULL);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO  Bird VALUES ('RedBird','Yin','Bus','m','1979-03-30',NULL);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO  Bird VALUES ('RedBird','Yin','Bus','m','1979-03-30',NULL);
Query OK, 1 row affected (0.00 sec)

mysql> select * from Bird;
+----------+-------+---------+------+------------+-------+
| name     | owner | species | sex  | birth      | death |
+----------+-------+---------+------+------------+-------+
| BlueBird | Joe   | Car     | f    | 1999-03-30 | NULL  |
| RedBird  | Yin   | Bus     | m    | 1979-03-30 | NULL  |
| RedBird  | Yin   | Bus     | m    | 1979-03-30 | NULL  |
+----------+-------+---------+------+------------+-------+
3 rows in set (0.00 sec)

mysql> SELECT name, species, birth FROM Bird
    -> WHERE species = 'Car' OR species = 'Bus';
+----------+---------+------------+
| name     | species | birth      |
+----------+---------+------------+
| BlueBird | Car     | 1999-03-30 |
| RedBird  | Bus     | 1979-03-30 |
| RedBird  | Bus     | 1979-03-30 |
+----------+---------+------------+
3 rows in set (0.00 sec)


*/
Drop table Bird;

CREATE TABLE Bird (
    name VARCHAR(20)
    owner VARCHAR(20),
    species VARCHAR(20)
    sex CHAR(1)
    birth DATE, 
    death DATE
);
  
INSERT INTO  Bird VALUES ('BlueBird','Joe','Car','f','1999-03-30',NULL);
INSERT INTO  Bird VALUES ('RedBird','Yin','Bus','m','1979-04-30',NULL);
INSERT INTO  Bird VALUES ('RedBird','Yin','Bus','m','1998-01-30',NULL);
  
select from Bird;
  


/* To sort a result, use an ORDER BY clause.
   default sort order is ascending
*/

SELECT name, birth FROM Bird ORDER BY birth;

           
       
Related examples in the same category
1. Order result wiht ORDER
2. Use ORDER BY to list
3. Use two ORDER BY fields
4. Default sort order is ascending
5. To sort in reverse (descending) order
6. Sort on multiple columns
7. Sort columns in different directions
8. Order decending
9. Another decendingly
10. Order BY and Limit
11. Order row in select clause
12. Order two columns with different orders
13. Narrow down data with condition and order it
14. Simple ORDER by
15. Sorting Data
16. Use order by to sort the result
17. Indicate of ascend
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.