0% found this document useful (0 votes)
160 views9 pages

Order by Clause PDF

sql queries

Uploaded by

ashokkumar g
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
160 views9 pages

Order by Clause PDF

sql queries

Uploaded by

ashokkumar g
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 9
SQL - ORDER BY Difficulty Level: Easy © Last Updated : 04 Oct, 2021 The ORDER BY statement in SQL is used to sort the fetched data in either ascending or descending according to one or more columns. + By default ORDER BY sorts the data in ascending order. * We can use the keyword DESC to sort the data in descending order and the keyword ASC to sort in ascending order. Sort according to one column: To sort in ascending or descending order we can use the keywords ASC or DESC respectively. Syntax: SELECT * FROM table_name ORDER BY column_name ASC|DESC HMhere table_name: name of the table. column_name: name of the column according to which the data is needed to t ASC: to sort the data in ascending order. DESC: to sort the data in descending order. | : use either ASC or DESC to sort in ascending or descending order// Sort according to multiple columns: To sort in ascending or descending order we can use the keywords ASC or DESC respectively. To sort according to multiple columns, separate the names of columns by the (,) operator. Syntax: We use cookies to ensure you have the best browsi sxpetience on our website. By using our site, Got It! ‘ood our Cookie Policy & Privacy Policy you acknowledge that you have read and under Start Your Coding Journey Now! s HUVANKA SIUGURI | xxcoccccc zo 4 DEEP RAMINAGAR | EK 18 5 SAPTARHI KOLKATA | SXOREOCEOE 19 6 DHANRA] | BARABAJAR | >ogGdOOODOr 20 7 ROHIT BALURGHAT | scouCKURREE 18 8 NIRAS aupuR — | cococcoone 29 Now consider the above database table and find the results of different queries. Sort according to a single column: In this example, we will fetch all data from the table Student and sort the result in descending order according to the column ROLL_NO Query: SELECT * FROM Student ORDER BY ROLL_NO DESC; Output: 8 NIRAJ ALIPUR XXXXXXXXXX 19 7 ROHIT BALURGHAT XXXXXXXXXX 18 6 DHANRAJ BARABAJAR XXXXXXXXXX 20 5 SAPTARH] KOLKATA XXXXXXXXXX 19 4 DEEP RAMNAGAR — XXXXXXXXXX 18 3 RIYANKA SILIGURI XXXXXXXXXX 20 Got It! Start Your Coding Journey Now! In the above example, if we want to sort in ascending order we have to use ASC in place of DESC. Sort according to multiple columns: In this example we will fetch all data from the table Student and then sort the result in ascending order first according to the column Age. and then in descending order according to the column ROLL_NO. Query: SELECT * FROM Student ORDER BY Age ASC , ROLL_NO DESC; Output: ROLL_NO NAME ADDRESS PHONE Age 7 ROHIT BALURGHAT XXXXXXXXXX 18 4 DEEP RAMNAGAR — XXXXXXXXXX 18 1 HARSH DELHI XXXXXXXXXX 18 8 NIRAJ ALIPUR XXXXXXXXXX 19 5 SAPTARHI KOLKATA XXXXXXXXXX 19 2 PRATIK BIHAR XXXXXXXXXX 19 6 DHANRAJ BARABAJAR = XXXXXXXXXX 20 3 RIYANKA SILIGURI XXXXXXXXXX 20 We use cookies to ensure you have the best browsing experience on our w you acknowledge that you have read and understood our Cookie Policy & Privacy Policy te. By using our site Got It! Start Your Coding Journey Now! (Resistor ASC is the default value for the ORDER BY clause. So, if we don't specify anything after the column name in the ORDER BY clause, the output will be sorted in ascending order by default. Take another example of the following query will give similar output as the above Query: SELECT * FROM Student ORDER BY Age , ROLL_NO DESC; Output: 7 ROHIT BALURGHAT XXXXXXXXXX 18 4 DEEP RAMNAGAR — XXXXXXXXXX 18. 1 HARSH DELHI XXXXXXXXXX 18 8 NIRAJ ALIPUR, XXXXXXXXXX 19 5 SAPTARHI KOLKATA XXXXXXXXXX 19 2 PRATIK BIHAR, XXXXXXXXXX 19 6 DHANRAJ BARABAJAR XXXXXXXXXX 20 3 RIYANKA SILIGURI XXXXXXXXXX 20 Sorting by column number (instead of name): An integer that identifies the number of the column in the Selectltems in the underlvina auerv of the SFI FCT statement Column number must be areater (esto ensure you have the le. By using our site, Got It! you acknowledge that y r 1d our Cookie Policy & Privacy Policy Start Your Coding Journey Now! [sone] ine column number insteaa or ine column name. ine column numpers in ine ORDER BY clause impairs the readability of the SQL statement. Further, changing the order of columns in the SELECT list has no impact on the ORDER BY when the columns are referred by names instead of numbers. Syntax: Order by Column_Number asc/desc Here we take an example to sort a database table according to column 1 i.e Roll_Number. For this a query will be: Query: CREATE TABLE studentinfo ( Roll_no INT, NAME VARCHAR(25), Address VARCHAR(2@), CONTACTNO BIGINT NOT NULL, Age INT ); INSERT INTO studentinfo VALUES (7,"ROHIT", "GAZIABAD' ,9193458625,18), (4, DEEP" , 'RAMNAGAR' 9193458546, 18), (2, "HARSH" , DELHI’ ,9193342625, 18), (8, ‘NIRAJ', ‘ALIPUR' ,9193678625,19), (5, SAPTARHI" , ‘KOLKATA’ ,9193789625,19), (2, ‘PRATIK" , "BIHAR' ,9193457825,19), (6, "DHANRAI", "BARABAJAR’ 9193358625, 20) , (3, "RIVANKA" , ‘SILIGURI’ , 9193218625, 20) ; SELECT Name, Address FROM studentinfo ORDER BY 1 We use cookies to ensure you have the best browsi experience on our we By using our site, Got It! i004 our Cookie Policy & Privacy Policy you acknowledge that you have read and under Start Your Coding Journey Now! egister (4, DEEP" "RAMNAGAR” 9195458546, 18), (2, Warsi, ‘ELH 9193342625, 18) (8, -rRay";ALPUR",9193676625,19) (5. *saptanit’,“xoLxATA’ 9193799625, 18) (2) vemaraK", "BunaR’,9193457825,29), (6. “onaneaa®, “asrapéoaR", 9193358625, 20), (3) arvana | ssrLacuRt 9193210625, 28) SELECT name, Address FROH student info onoen ov a] Output: ee eet eet 5 Like Next We use cookies to ensure you have the best browsing experience on our website. By using our st Got It! you acknowledge that you have read and understood our Cookie Policy & Privacy Policy Start Your Coding Journey Now! Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL) 22, Aug 19 01 05 Configure SQL Jobs in SQL Server using T-SQL 01, Apr 24 02 03 Difference between T-SQL and PL- saQL 07 12, Apr 20 Difference between SQL and T- sau 06, Jul 20 04 08 Article Contributed By : © GeeksforGeeks Vote for difficulty Current dificuty : Easy [sone] ‘SQL Server | Convert tables in T- ‘SQL into XML 01, Mar 21 ‘SQL SERVER | Bulk insert data from csv file using T-SQL command 01, Mar 21 SQL - SELECT from Multiple Tables with MS SQL Server 14, dun 21 How to Execute SQL Server Stored Procedure in SQL Developer? 21, Sep 21 Easy | Normal | Medium Hard Expert Improved By: aditimantri2196, varshachoudhary Article Tags: SQL-Clauses-Operators, DBMS, experience on ou stood our Cookie Polis sab site. By using our site, 5 & Privacy Policy Got It! Start Your Coding Journey Now! Register Writing code in comment? Please use ide. geoksforgecks.org, generate link and share the link here. OG GeeksforGeeks Sth Floor, A-118, Sector-136, Noida, Uttar Pradesh - 201305 feedback @geeksforgeeks.org Company Learn ‘About Us Algorithms Careers Data Structures In Media SDE Cheat Sheet Contact Us Machine learning Privacy Policy CS Subjects Copyright Policy Video Tutorials News Languages Top News Python Technology Java Work & Career cPP Business Golang Finance CH Lifestyle sal Web Development Contribute Web Tutorials Write an Article We use cookies to ensure you have the best browsing experience on our website. By using our sit, Got It! you acknowledge that you have read and understood our Cookie Policy & Privacy Policy Start Your Coding Journey Now! Register @geeksforgeeks , Some rights reserved We use cookies to ensure you have the best browsing experience on our website. By using our ste, Got It! you acknowledge that you have read and understood our Cookie Policy & Privacy Policy

You might also like