General Note:
Here we have pulled in the data from a sample database. You can download it from the following link and use it for practicing Click Here For Sample Database
ORDER BY is used to sort column records either in ascending order or descending order. The default order is ascending. If you want to sort them in descending order use DESC keyword. The below query sorts all the records in brand name column to ascending order
Select mobile_name,price,brand_name from mobile_sales_details order by brand_name
mobile_name price brand_name
--------------------------------------------- ---------- ----------
Alcatel Onetouch Idol X+ (White with 16 GB) 16999 Alcatel
Apple iPhone 5S (Gold with 16 GB) 43199 Apple
Apple iPhone 4S (White with 8 GB) 21712 Apple
Apple iPhone 5C (Blue with 16 GB) 41900 Apple
Apple iPhone 5S (Silver with 32 GB) 62500 Apple
Apple iPhone 5 (White with 32 GB) 52500 Apple
Asus Zenfone 4 A400CG (Black with 8 GB) 5999 Asus
Asus Zenfone 5 A501CG (Black with 8 GB) 9999 Asus
Asus Zenfone 5 A501CG (Black with 16 GB) 12999 Asus
Asus Zenfone 6 A600CG / A601CG (Black with 1 16999 Asus
BlackBerry Z10 (Charcoal Black) 17990 BlackBerry
BlackBerry Z3 (Black with 8 GB) 15990 BlackBerry
BlackBerry Curve 9220 (Black) 9125 BlackBerry
BlackBerry 9720 (Black) 16000 BlackBerry
BlackBerry Z10 (Pure White) 43490 BlackBerry
Karbonn Smart A12 Star (Black & Silver) 4099 Karbonn
Karbonn Smart A52 Plus (Black & Gold) 2999 Karbonn
If you want to display high price mobile to low price mobile
Select mobile_name,price,brand_name from mobile_sales_details order by price desc
mobile_name price brand_name
------------------------------------ ---------- ----------
Apple iPhone 5S (Silver with 32 GB) 62500 Apple
Samsung Galaxy S5 (Charcoal Black) 54300 Samsung
Apple iPhone 5 (White with 32 GB) 52500 Apple
BlackBerry Z10 (Pure White) 43490 BlackBerry
Apple iPhone 5S (Gold with 16 GB) 43199 Apple
Apple iPhone 5C (Blue with 16 GB) 41900 Apple
Samsung Galaxy S3 (Marble White wit 31850 Samsung
Sony Xperia T2 Ultra (Purple) 28999 Sony
Samsung Galaxy S4 I9500 (White Frost 28071 Samsung
Samsung Galaxy S4 Mini I9192 (White 24730 Samsung
Moto X (16 GB) (Black) 23999 Motorola
Apple iPhone 4S (White with 8 GB) 21712 Apple
Sony Xperia ZR (Black) 19990 Sony
Samsung Galaxy Grand Duos I9082 19000 Samsung
Samsung Galaxy Grand 2 (Black) 18990 Samsung
BlackBerry Z10 (Charcoal Black) 17990 BlackBerry
Nokia Lumia 1320 (Black) 17190 Nokia
SORTING MORE THAN ONE COLUMN
You can sort the result using more than one column in ORDER BY. Here we have used brand name followed by price. So the brand name first gets ordered in ascending order then the price of their particular brand ordered in descending manner. Since price is marked with DESC keyword.
Select mobile_name,price,brand_name from mobile_sales_details order by brand_name, price desc
mobile_name price brand_name
--------------------------------------------- ---------- ----------
Alcatel Onetouch Idol X+ (White with 16 GB) 16999 Alcatel
Apple iPhone 5S (Silver with 32 GB) 62500 Apple
Apple iPhone 5 (White with 32 GB) 52500 Apple
Apple iPhone 5S (Gold with 16 GB) 43199 Apple
Apple iPhone 5C (Blue with 16 GB) 41900 Apple
Apple iPhone 4S (White with 8 GB) 21712 Apple
Asus Zenfone 6 A600CG / A601CG (Black with 1 16999 Asus
Asus Zenfone 5 A501CG (Black with 16 GB) 12999 Asus
Asus Zenfone 5 A501CG (Black with 8 GB) 9999 Asus
Asus Zenfone 4 A400CG (Black with 8 GB) 5999 Asus
BlackBerry Z10 (Pure White) 43490 BlackBerry
BlackBerry Z10 (Charcoal Black) 17990 BlackBerry
BlackBerry 9720 (Black) 16000 BlackBerry
BlackBerry Z3 (Black with 8 GB) 15990 BlackBerry
BlackBerry Curve 9220 (Black) 9125 BlackBerry
Karbonn Titanium S5 Plus (Pearl White) 11999 Karbonn
Karbonn Smart A11 Star (Black) 4499 Karbonn
Karbonn Smart A12 Star (Black & Silver) 4099 Karbonn
You can sort the column by using their position in the display. In the above query, the by brand_name,price will be replaced with 3, 2 because the brand_name column displayed at 3rd position and the price column is displayed at 2nd position.
Select mobile_name,price,brand_name from mobile_sales_details order by 3,2 desc
mobile_name price brand_name
--------------------------------------------- ---------- ----------
Alcatel Onetouch Idol X+ (White with 16 GB) 16999 Alcatel
Apple iPhone 5S (Silver with 32 GB) 62500 Apple
Apple iPhone 5 (White with 32 GB) 52500 Apple
Apple iPhone 5S (Gold with 16 GB) 43199 Apple
Apple iPhone 5C (Blue with 16 GB) 41900 Apple
Apple iPhone 4S (White with 8 GB) 21712 Apple
Asus Zenfone 6 A600CG / A601CG (Black with 1 16999 Asus
Asus Zenfone 5 A501CG (Black with 16 GB) 12999 Asus
Asus Zenfone 5 A501CG (Black with 8 GB) 9999 Asus
Asus Zenfone 4 A400CG (Black with 8 GB) 5999 Asus
BlackBerry Z10 (Pure White) 43490 BlackBerry
BlackBerry Z10 (Charcoal Black) 17990 BlackBerry
BlackBerry 9720 (Black) 16000 BlackBerry
BlackBerry Z3 (Black with 8 GB) 15990 BlackBerry
BlackBerry Curve 9220 (Black) 9125 BlackBerry
Karbonn Titanium S5 Plus (Pearl White) 11999 Karbonn
Karbonn Smart A11 Star (Black) 4499 Karbonn
Karbonn Smart A12 Star (Black & Silver) 4099 Karbonn
However, using the column position in ORDER BY is highly complicated. The reason being that if you choose any other column in between them, then the sorting will vary. For example
Select mobile_name, brand_name, price from mobile_sales_details order by 3,2 desc
In the above query 3 refers to price and 2 refers to brand_name. Therefore, first the price column will be sorted in ascending order then only the associated brand_name will be sorted in descending order.
This infers that using the column position will change your result format. Be cautious while using the column position. Best practise is to avoid the column position when you are repeatedly changing the query.