Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
160 views
9 pages
Order by Clause PDF
sql queries
Uploaded by
ashokkumar g
AI-enhanced title
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
Download
Save
Save order by clause.pdf For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
0 ratings
0% found this document useful (0 votes)
160 views
9 pages
Order by Clause PDF
sql queries
Uploaded by
ashokkumar g
AI-enhanced title
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
Carousel Previous
Carousel Next
Download
Save
Save order by clause.pdf For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
Download
Save order by clause.pdf For Later
You are on page 1
/ 9
Search
Fullscreen
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 underStart 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 PolicyStart 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 underStart 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 PolicyStart 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 PolicyStart 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
T-SQL Northwind Database
PDF
100% (1)
T-SQL Northwind Database
52 pages
140+ SQL Interview Questions and Answers (2022) - Great Learning
PDF
No ratings yet
140+ SQL Interview Questions and Answers (2022) - Great Learning
60 pages
SQL ORDER BY Clause
PDF
No ratings yet
SQL ORDER BY Clause
14 pages
SQL
PDF
100% (1)
SQL
45 pages
Dbms Module3 Chapter1
PDF
No ratings yet
Dbms Module3 Chapter1
75 pages
SQL Queries
PDF
No ratings yet
SQL Queries
25 pages
SQL Notes Basic To Advanced (SQL Clauses)
PDF
No ratings yet
SQL Notes Basic To Advanced (SQL Clauses)
10 pages
Chapter 3 - SQL Notes
PDF
No ratings yet
Chapter 3 - SQL Notes
25 pages
Dbms Practical 5-8
PDF
No ratings yet
Dbms Practical 5-8
14 pages
Ism Lab File
PDF
No ratings yet
Ism Lab File
39 pages
Introduction To SQL
PDF
No ratings yet
Introduction To SQL
23 pages
SQL-Review of Class XI - Part 2
PDF
No ratings yet
SQL-Review of Class XI - Part 2
32 pages
SQL - Notes
PDF
No ratings yet
SQL - Notes
14 pages
My SQL Basic
PDF
No ratings yet
My SQL Basic
24 pages
Chapter 4 Part 1
PDF
No ratings yet
Chapter 4 Part 1
129 pages
Lesson 6 - SQL SELECT II
PDF
No ratings yet
Lesson 6 - SQL SELECT II
87 pages
Chapter 14 More On SQL
PDF
No ratings yet
Chapter 14 More On SQL
20 pages
Basic SQL Queries
PDF
No ratings yet
Basic SQL Queries
10 pages
SQL Notes
PDF
No ratings yet
SQL Notes
29 pages
Lab Lect 2
PDF
No ratings yet
Lab Lect 2
28 pages
Sorting and Filtering Data: Module Overview
PDF
No ratings yet
Sorting and Filtering Data: Module Overview
22 pages
Types of Ordering
PDF
No ratings yet
Types of Ordering
4 pages
6.SQL Queries Clause
PDF
No ratings yet
6.SQL Queries Clause
40 pages
Order by and Group by Cluase
PDF
No ratings yet
Order by and Group by Cluase
9 pages
SQL For Data Analysis Notes
PDF
No ratings yet
SQL For Data Analysis Notes
11 pages
Labsheet 9
PDF
No ratings yet
Labsheet 9
9 pages
Relational Modeling and SQL Basics
PDF
No ratings yet
Relational Modeling and SQL Basics
23 pages
Bagian 2 Tutorial SQL
PDF
No ratings yet
Bagian 2 Tutorial SQL
8 pages
SQL Order by
PDF
No ratings yet
SQL Order by
7 pages
The ORDER BY Operator in SQL
PDF
No ratings yet
The ORDER BY Operator in SQL
7 pages
SQL Notes
PDF
No ratings yet
SQL Notes
25 pages
Orderby
PDF
No ratings yet
Orderby
7 pages
Exp 5 Group By, Having, Orderby
PDF
No ratings yet
Exp 5 Group By, Having, Orderby
6 pages
(D) SQL ORDER BY Clause
PDF
No ratings yet
(D) SQL ORDER BY Clause
4 pages
How Expensive Is SQL ORDER BY
PDF
No ratings yet
How Expensive Is SQL ORDER BY
3 pages
DBMS SQL Clauses - Javatpoint
PDF
No ratings yet
DBMS SQL Clauses - Javatpoint
6 pages
Cid Bid Maname Mid Sid Bid, Cid, Sid: Print The Sid of All Salespeople Who Have Sold Both A Ford and A Toyota in 1997
PDF
No ratings yet
Cid Bid Maname Mid Sid Bid, Cid, Sid: Print The Sid of All Salespeople Who Have Sold Both A Ford and A Toyota in 1997
41 pages
SQL Order by
PDF
No ratings yet
SQL Order by
12 pages
Lec4 - SQL ASR
PDF
No ratings yet
Lec4 - SQL ASR
55 pages
Lesson 4
PDF
No ratings yet
Lesson 4
76 pages
What Is SQL?
PDF
No ratings yet
What Is SQL?
17 pages
03.sorting Retrieved Data
PDF
No ratings yet
03.sorting Retrieved Data
4 pages
SQL Lab
PDF
No ratings yet
SQL Lab
83 pages
Itm 500 Week 2 Notes
PDF
No ratings yet
Itm 500 Week 2 Notes
4 pages
Twenty-Five SQL Practice Exercises: These Questions and Example Solutions Will Keep Your Skills Sharp
PDF
No ratings yet
Twenty-Five SQL Practice Exercises: These Questions and Example Solutions Will Keep Your Skills Sharp
39 pages
Learn SQL
PDF
No ratings yet
Learn SQL
70 pages
Order by
PDF
No ratings yet
Order by
2 pages
Dbms Lab 3
PDF
No ratings yet
Dbms Lab 3
10 pages
SQL Queries
PDF
No ratings yet
SQL Queries
16 pages
Lab 2
PDF
No ratings yet
Lab 2
29 pages
Experiment No 2
PDF
No ratings yet
Experiment No 2
6 pages
57.10 - ORDER BY - mp4
PDF
No ratings yet
57.10 - ORDER BY - mp4
2 pages
4.PracticalSQL2E SampleCh3
PDF
No ratings yet
4.PracticalSQL2E SampleCh3
1 page
Column-Name: SQL Order by Clause
PDF
No ratings yet
Column-Name: SQL Order by Clause
2 pages
Default Order by SQL - Google Search
PDF
No ratings yet
Default Order by SQL - Google Search
1 page
Department of Product
PDF
No ratings yet
Department of Product
27 pages
ORDER BY Clause - Sort Data in SQL - 1keydata
PDF
No ratings yet
ORDER BY Clause - Sort Data in SQL - 1keydata
3 pages
Limiting Selected Rows
PDF
No ratings yet
Limiting Selected Rows
40 pages
9 SQL ORDER BY Clause
PDF
No ratings yet
9 SQL ORDER BY Clause
2 pages
Create Table Statement: Structured Query Language
PDF
No ratings yet
Create Table Statement: Structured Query Language
3 pages
Practical 9
PDF
No ratings yet
Practical 9
5 pages
Trade Life Cycle Management in Finpricing: Part 3
PDF
No ratings yet
Trade Life Cycle Management in Finpricing: Part 3
16 pages
Trade Life Cycle Management in Finpricing: Part 1
PDF
No ratings yet
Trade Life Cycle Management in Finpricing: Part 1
15 pages
What Is SQL?
PDF
No ratings yet
What Is SQL?
18 pages
SQL For Better Study
PDF
No ratings yet
SQL For Better Study
24 pages