Sql Tutorial Class


How to use order by desc in sql

How to use order by in sql query


SQL ORDER BY Keyword: It is used to display the result on ascending  or descending order.

ORDER BY DESC: Indicates descending order.



SQL ORDER BY DESC Syntax:

Descending order syntax
SELECT */column_name,.. FROM table_name WHERE condition, ORDER BY column_name DESC


Order By DESC keyword syntax without WHERE clause:
SELECT */column_name,.. FROM table_name ORDER BY column_name DESC

Description:

SELECT: SQL Statement.
*/column_name: Name of column.
table_name: Name of table.
WHERE: SQL Clause.
condition: Condition(s).
ORDER BY: SQL Keyword.
DESC: Descending order.



Here is a Table ItemDetails
ItemCodeItemNameManufactureCompanyItemSerialNoItemCategoryItemQuantityItemPriceProductCountry
1Samsung MobileSamsungS12345Mobile500240Korea
2Samsung ProSamsungSP1234Mobile100300Korea
3Samsung HeadphoneSamsungSH2345Headphone5050Korea
4iPhoneiPoneiP12345Mobile1000500United Stat
5Apple MobileAppleA12345Mobile200300United Stat
6Apple ComputerAppleA12343Computer200400United Stat
7Apple MacbookAppleAM2345Macbook50600United Stat
8Dell ComputerDELLD02345Computer200240United Stat
9Dell LaptopDELLDL2345Laptop30240United Stat
10Apple LaptopAppleAL2345Laptop500100United Stat
11Apple HeadphoneAppleAH2345Headphone5020United Stat
12Sony CameraSonySN2345Camera200240Japan
13Sony HeadphoneSonySNH345Headphone50050Japan
14Ascer ComputerAscerAC2345Computer100600China
16Borother PrinterBrotherB12345Printer200200China
17Ascer PrinterAscerAP2345Printer200150China
18Brother PhotocopyBrotherBP2345Printer20150China
19Brother Multi PrinterBrotherBM243PhotocopyNULLNULLIndia




Now,
Example:
Select ItemName, ManufactureCompany,ItemCategory from ItemDetails order by ItemCategory DESC
Result:
ItemName ManufactureCompany ItemCategory
Borother Printer Brother Printer
Ascer Printer Ascer Printer
Brother Photocopy Brother Printer
Brother Multi Printer Brother Photocopy
Samsung Mobile Samsung Mobile
Samsung Pro Samsung Mobile
iPhone iPone Mobile
Apple Mobile Apple Mobile
Apple Macbook Apple Macbook
Dell Laptop DELL Laptop
Apple Laptop Apple Laptop
Apple Headphone Apple Headphone
Samsung Headphone Samsung Headphone
Sony Headphone Sony Headphone
Ascer Computer Ascer Computer
Dell Computer DELL Computer
Apple Computer Apple Computer
Sony Camera Sony Camera



Example:
Select ItemName, ManufactureCompany,ItemCategory,ItemPrice from ItemDetails order by ItemPrice DESC
Result:
ItemName ManufactureCompany ItemCategory ItemPrice
Apple Macbook Apple Macbook 600
Ascer Computer Ascer Computer 600
iPhone iPone Mobile 500
Apple Computer Apple Computer 400
Apple Mobile Apple Mobile 300
Samsung Pro Samsung Mobile 300
Dell Computer DELL Computer 240
Dell Laptop DELL Laptop 240
Samsung Mobile Samsung Mobile 240
Sony Camera Sony Camera 240
Borother Printer Brother Printer 200
Ascer Printer Ascer Printer 150
Brother Photocopy Brother Printer 150
Apple Laptop Apple Laptop 100
Samsung Headphone Samsung Headphone 50
Sony Headphone Sony Headphone 50
Apple Headphone Apple Headphone 20
Brother Multi Printer Brother Photocopy NULL





Example: 
Select ItemName, ManufactureCompany,ItemCategory,ProductCountry,ItemPrice from ItemDetails Where ProductCountry='United Stat' order by ItemPrice DESC
Result:
ItemName ManufactureCompany ItemCategory ProductCountry ItemPrice
Apple Macbook Apple Macbook United Stat 600
iPhone iPone Mobile United Stat 500
Apple Computer Apple Computer United Stat 400
Apple Mobile Apple Mobile United Stat 300
Dell Computer DELL Computer United Stat 240
Dell Laptop DELL Laptop United Stat 240
Apple Laptop Apple Laptop United Stat 100
Apple Headphone Apple Headphone United Stat 20





SQL ORDER BY DESC Keyword example in video
>






SQL ORDER BY ASC Keyword example in Image
order by desc in sql






[Note: We have created and inserted values into sql table in previous topic. For more details Click here.
OR
Execute the follow command in your sql query window for executing above sql select where statement]
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Create Table ItemDetails
(
ItemCode int,
ItemName varchar(30),
ManufactureCompany varchar(50),
ExpiredDate date,
ItemSearialNo varchar (30),
ItemCategory varchar(30),
ItemQuantity int,
ItemPrice float,
ProductCountry varchar(30),
ReceivedDate date,
ItemImage image
)

Insert into ItemDetails values('1','Samsung Mobile','Samsung','12-10-2024','12-01-2050','S12345','Mobile','500','240','Korea','12-10-2025')
Insert into ItemDetails values('2','Samsung Pro','Samsung','12-10-2024','12-01-2050','SP1234','Mobile','100','300','Korea','12-10-2025')
Insert into ItemDetails values('3','Samsung Headphone','Samsung','12-10-2023','12-01-2053','SH2345','Headphone','50','50','Korea','12-10-2025')
Insert into ItemDetails values('4','iPhone','iPone','12-01-2024','12-01-2049','iP12345','Mobile','1000','500','United Stat','12-10-2025')
Insert into ItemDetails values('5','Apple Mobile','Apple','12-11-2024','12-01-2049','A12345','Mobile','200','300','United Stat','12-10-2025')
Insert into ItemDetails values('6','Apple Computer','Apple','12-02-2024','12-01-2050','A12343','Computer','200','400','United Stat','12-10-2025')
Insert into ItemDetails values('7','Apple Macbook','Apple','12-11-2024','12-01-2050','AM2345','Macbook','50','600','United Stat','12-10-2025')
Insert into ItemDetails values('8','Dell Computer','DELL','12-09-2024','12-01-2050','D02345','Computer','200','240','United Stat','12-10-2025')
Insert into ItemDetails values('9','Dell Laptop','DELL','12-09-2024','12-01-2050','DL2345','Laptop','30','240','United Stat','12-10-2025')
Insert into ItemDetails values('10','Apple Laptop','Apple','12-08-2024','12-01-2050','AL2345','Laptop','500','100','United Stat','12-10-2025')
Insert into ItemDetails values('11','Apple Headphone','Apple','12-12-2024','12-01-2050','AH2345','Headphone','50','20','United Stat','12-10-2025')
Insert into ItemDetails values('12','Sony Camera','Sony','12-11-2024','12-11-2050','SN2345','Camera','200','240','Japan','12-11-2025')
Insert into ItemDetails values('13','Sony Headphone','Sony','12-09-2024','12-01-2050','SNH345','Headphone','500','50','Japan','12-11-2025')
Insert into ItemDetails values('14','Ascer Computer','Ascer','12-09-2024','12-07-2050','AC2345','Computer','100','600','China','12-10-2025')
Insert into ItemDetails values('16','Borother Printer','Brother','12-09-2024','12-01-2050','B12345','Printer','200','200','China','12-10-2025')
Insert into ItemDetails values('19','Brother Multi Printer','Brother',GETDATE(),'2050-10-10','BM243','Photocopy',null,null,'India',GETDATE())

Insert into ItemDetails (ItemCode,ItemName,ManufactureCompany,ProductDate,ExpiredDate,ItemSearialNo,ItemCategory,ItemQuantity,ItemPrice,ProductCountry,ReceivedDate) values('17','Ascer Printer','Ascer','12-09-2024','12-07-2050','AP2345','Printer','200','150','China','12-10-2025')
Insert into ItemDetails (ItemCode,ItemName,ManufactureCompany,ProductDate,ExpiredDate,ItemSearialNo,ItemCategory,ItemQuantity,ItemPrice,ProductCountry,ReceivedDate) values('18','Brother Photocopy','Brother','12-08-2024','12-07-2050','BP2345','Printer','20','150','China','12-10-2025')
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

No comments:

Post a Comment