Use order by to sort the result : 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 
Use order by to sort the result

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

mysql> CREATE TABLE Item
    -> (
    ->    ID SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    ->    Name VARCHAR(50) NOT NULL,
    ->    InStock SMALLINT UNSIGNED NOT NULL,
    ->    OnOrder SMALLINT UNSIGNED NOT NULL,
    ->    Reserved SMALLINT UNSIGNED NOT NULL,
    ->    Department ENUM('Classical', 'Popular') NOT NULL,
    ->    Category VARCHAR(20) NOT NULL,
    ->    RowUpdate TIMESTAMP NOT NULL
    -> );
Query OK, 0 rows affected (0.14 sec)

mysql> INSERT INTO Item (Name, InStock, OnOrder, Reserved, Department, Category)

    ->           VALUES ('Bloodshot',      10,      5,       1, 'Popular',
'Rock'),
    ->                  ('Most',           10,      5,       2, 'Classical',
'Opera'),
    ->                  ('Jazz',           17,      4,       3, 'Popular',
'Jazz'),
    ->                  ('Class',           9,      4,       4, 'Classical',
'Dance'),
    ->                  ('Violin',         24,      2,       5, 'Classical',
'General'),
    ->                  ('Cha Cha',        16,      6,       6, 'Classical',
'Vocal'),
    ->                  ('Blues',           2,     25,       7, 'Popular',
'Blues'),
    ->                  ('Pure',           32,      3,      18, 'Popular',
'Jazz'),
    ->                  ('Mud',            12,     15,      19, 'Popular',
'Country'),
    ->                  ('The',             5,     20,      11, 'Popular',
'New Age'),
    ->                  ('Embrace',        24,     11,      12, 'Popular',
'New Age'),
    ->                  ('Magic',          42,     17,      13, 'Classical',
'General'),
    ->                  ('Lake',           25,     44,      24, 'Classical',
'Dance'),
    ->                  ('LaLala',         20,     10,       5, 'Classical',
'Opera'),
    ->                  ('Soul',           15,     30,      16, 'Popular',
'Blues'),
    ->                  ('Stages',         42,      0,       7, 'Popular',
'Blues'),
    ->                  ('Six',            16,      8,       6, 'Classical',
'General');
Query OK, 17 rows affected (0.00 sec)
Records: 17  Duplicates: 0  Warnings: 0

mysql> select * from Item;
+----+-----------+---------+---------+----------+------------+----------+---------------------+
| ID | Name      | InStock | OnOrder | Reserved | Department | Category | RowUpdate           |
+----+-----------+---------+---------+----------+------------+----------+---------------------+
|  1 | Bloodshot |      10 |       5 |        1 | Popular    | Rock     | 2005-10-09 09:19:48 |
|  2 | Most      |      10 |       5 |        2 | Classical  | Opera    | 2005-10-09 09:19:48 |
|  3 | Jazz      |      17 |       4 |        3 | Popular    | Jazz     | 2005-10-09 09:19:48 |
|  4 | Class     |       9 |       4 |        4 | Classical  | Dance    | 2005-10-09 09:19:48 |
|  5 | Violin    |      24 |       2 |        5 | Classical  | General  | 2005-10-09 09:19:48 |
|  6 | Cha Cha   |      16 |       6 |        6 | Classical  | Vocal    | 2005-10-09 09:19:48 |
|  7 | Blues     |       2 |      25 |        7 | Popular    | Blues    | 2005-10-09 09:19:48 |
|  8 | Pure      |      32 |       3 |       18 | Popular    | Jazz     | 2005-10-09 09:19:48 |
|  9 | Mud       |      12 |      15 |       19 | Popular    | Country  | 2005-10-09 09:19:48 |
| 10 | The       |       5 |      20 |       11 | Popular    | New Age  | 2005-10-09 09:19:48 |
| 11 | Embrace   |      24 |      11 |       12 | Popular    | New Age  | 2005-10-09 09:19:48 |
| 12 | Magic     |      42 |      17 |       13 | Classical  | General  | 2005-10-09 09:19:48 |
| 13 | Lake      |      25 |      44 |       24 | Classical  | Dance    | 2005-10-09 09:19:48 |
| 14 | LaLala    |      20 |      10 |        5 | Classical  | Opera    | 2005-10-09 09:19:48 |
| 15 | Soul      |      15 |      30 |       16 | Popular    | Blues    | 2005-10-09 09:19:48 |
| 16 | Stages    |      42 |       0 |        7 | Popular    | Blues    | 2005-10-09 09:19:48 |
| 17 | Six       |      16 |       8 |        6 | Classical  | General  | 2005-10-09 09:19:48 |
+----+-----------+---------+---------+----------+------------+----------+---------------------+
17 rows in set (0.00 sec)

mysql> SELECT Name, InStock, OnOrder
    -> FROM Item
    -> WHERE InStock>20
    -> ORDER BY Name DESC;
+---------+---------+---------+
| Name    | InStock | OnOrder |
+---------+---------+---------+
| Violin  |      24 |       2 |
| Stages  |      42 |       0 |
| Pure    |      32 |       3 |
| Magic   |      42 |      17 |
| Lake    |      25 |      44 |
| Embrace |      24 |      11 |
+---------+---------+---------+
6 rows in set (0.00 sec)
*/

Drop table Item;

CREATE TABLE Item
(
   ID SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
   Name VARCHAR(50NOT NULL,
   InStock SMALLINT UNSIGNED NOT NULL,
   OnOrder SMALLINT UNSIGNED NOT NULL,
   Reserved SMALLINT UNSIGNED NOT NULL,
   Department ENUM('Classical', 'Popular') NOT NULL,
   Category VARCHAR(20NOT NULL,
   RowUpdate TIMESTAMP NOT NULL
);


INSERT INTO Item (Name, InStock, OnOrder, Reserved, Department, Category)
          VALUES ('Bloodshot',      10,      5,       1'Popular',      'Rock'),
                 ('Most',           10,      5,       2'Classical',    'Opera'),
                 ('Jazz',           17,      4,       3'Popular',      'Jazz'),
                 ('Class',           9,      4,       4'Classical',    'Dance'),
                 ('Violin',         24,      2,       5'Classical',    'General'),
                 ('Cha Cha',        16,      6,       6'Classical',    'Vocal'),
                 ('Blues',           2,     25,       7'Popular',      'Blues'),
                 ('Pure',           32,      3,      18'Popular',      'Jazz'),
                 ('Mud',            12,     15,      19'Popular',      'Country'),
                 ('The',             5,     20,      11'Popular',      'New Age'),
                 ('Embrace',        24,     11,      12'Popular',      'New Age'),
                 ('Magic',          42,     17,      13'Classical',    'General'),
                 ('Lake',           25,     44,      24'Classical',    'Dance'),
                 ('LaLala',         20,     10,       5'Classical',    'Opera'),
                 ('Soul',           15,     30,      16'Popular',      'Blues'),
                 ('Stages',         42,      0,       7'Popular',      'Blues'),
                 ('Six',            16,      8,       6'Classical',    'General');

select from Item;

SELECT Name, InStock, OnOrder
FROM Item
WHERE InStock>20
ORDER BY Name DESC;



           
       
Related examples in the same category
1. Order result wiht ORDER
2. Use ORDER BY to list
3. Use two ORDER BY fields
4. Sorting Rows
5. Default sort order is ascending
6. To sort in reverse (descending) order
7. Sort on multiple columns
8. Sort columns in different directions
9. Order decending
10. Another decendingly
11. Order BY and Limit
12. Order row in select clause
13. Order two columns with different orders
14. Narrow down data with condition and order it
15. Simple ORDER by
16. Sorting Data
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.