SlideShare a Scribd company logo
1 of 3
Download to read offline
8/10/13 SQL ORDER BY Clause
beginner-sql-tutorial.com/sql-order-by-clause.htm 1/3
SQL ORDER BY
The ORDER BY clause is used in a SELECT statement to sort results either in ascending or descending order.
Oracle sorts query results in ascending order by default.
Syntax for using SQL ORDER BY clause to sort data is:
SELECT column-list
FROM table_name [WHERE condition]
[ORDER BY column1 [, column2, .. columnN] [DESC]];
database table "employee";
id name dept agesalary location
100RameshElectrical 24 25000 Bangalore
101Hrithik Electronics 28 35000 Bangalore
102Harsha Aeronautics 28 35000 Mysore
103Soumya Electronics 22 20000 Bangalore
104Priya InfoTech 25 30000 Mangalore
For Example: If you want to sort the employee table by salary of the employee, the sql query would be.
SELECT name, salary FROM employee ORDER BY salary;
The output would be like
name salary
---------- ----------
Soumya 20000
Ramesh 25000
Priya 30000
Hrithik 35000
Harsha 35000
The query first sorts the result according to name and then displays it.
You can also use more than one column in the ORDER BY clause.
8/10/13 SQL ORDER BY Clause
beginner-sql-tutorial.com/sql-order-by-clause.htm 2/3
If you want to sort the employee table by the name and salary, the query would be like,
SELECT name, salary FROM employee ORDER BY name, salary;
The output would be like:
name salary
----------
---
----------
---
Soumya 20000
Ramesh 25000
Priya 30000
Harsha 35000
Hrithik 35000
NOTE:The columns specified in ORDER BY clause should be one of the columns selected in the SELECT column
list.
You can represent the columns in the ORDER BY clause by specifying the position of a column in the SELECT list,
instead of writing the column name.
The above query can also be written as given below,
SELECT name, salary FROM employee ORDER BY 1, 2;
By default, the ORDER BY Clause sorts data in ascending order. If you want to sort the data in descending
order, you must explicitly specify it as shown below.
SELECT name, salary
FROM employee
ORDER BY name, salary DESC;
The above query sorts only the column 'salary' in descending order and the column 'name' by ascending order.
8/10/13 SQL ORDER BY Clause
beginner-sql-tutorial.com/sql-order-by-clause.htm 3/3
If you want to select both name and salary in descending order, the query would be as given below.
SELECT name, salary
FROM employee
ORDER BY name DESC, salary DESC;
How to use expressions in the ORDER BY Clause?
Expressions in the ORDER BY clause of a SELECT statement.
For example: If you want to display employee name, current salary, and a 20% increase in the salary for only
those employees for whom the percentage increase in salary is greater than 30000 and in descending order of
the increased price, the SELECT statement can be written as shown below
SELECT name, salary, salary*1.2 AS new_salary
FROM employee
WHERE salary*1.2 > 30000
ORDER BY new_salary DESC;
The output for the above query is as follows.
name salarynew_salary
--------
--
-------
---
-------------
Hrithik 35000 37000
Harsha35000 37000
Priya 30000 36000
NOTE:Aliases defined in the SELECT Statement can be used in ORDER BY Clause.

More Related Content

What's hot

What's hot (20)

Stacks and Queue - Data Structures
Stacks and Queue - Data StructuresStacks and Queue - Data Structures
Stacks and Queue - Data Structures
 
DATABASE CONSTRAINTS
DATABASE CONSTRAINTSDATABASE CONSTRAINTS
DATABASE CONSTRAINTS
 
sql function(ppt)
sql function(ppt)sql function(ppt)
sql function(ppt)
 
Joins And Its Types
Joins And Its TypesJoins And Its Types
Joins And Its Types
 
Sql subquery
Sql  subquerySql  subquery
Sql subquery
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Sql queries with answers
Sql queries with answersSql queries with answers
Sql queries with answers
 
DBMS Keys
DBMS KeysDBMS Keys
DBMS Keys
 
SQL Views
SQL ViewsSQL Views
SQL Views
 
Data structure lecture 1
Data structure lecture 1Data structure lecture 1
Data structure lecture 1
 
Sub query_SQL
Sub query_SQLSub query_SQL
Sub query_SQL
 
Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applications
 
Aggregate Function - Database
Aggregate Function - DatabaseAggregate Function - Database
Aggregate Function - Database
 
Data Modeling
Data ModelingData Modeling
Data Modeling
 
Sql select
Sql select Sql select
Sql select
 
Null values, insert, delete and update in database
Null values, insert, delete and update in databaseNull values, insert, delete and update in database
Null values, insert, delete and update in database
 
Unit 4-booth algorithm
Unit 4-booth algorithmUnit 4-booth algorithm
Unit 4-booth algorithm
 
Commands of DML in SQL
Commands of DML in SQLCommands of DML in SQL
Commands of DML in SQL
 
ROL ROR SHL SHR Assembly Language Programmin 8086
ROL ROR SHL SHR Assembly Language Programmin 8086ROL ROR SHL SHR Assembly Language Programmin 8086
ROL ROR SHL SHR Assembly Language Programmin 8086
 
Relational Algebra,Types of join
Relational Algebra,Types of joinRelational Algebra,Types of join
Relational Algebra,Types of join
 

Similar to Sql order by clause

Similar to Sql order by clause (20)

Sql query [select, sub] 4
Sql query [select, sub] 4Sql query [select, sub] 4
Sql query [select, sub] 4
 
Sql2
Sql2Sql2
Sql2
 
Sql where clause
Sql where clauseSql where clause
Sql where clause
 
Sql where clause
Sql where clauseSql where clause
Sql where clause
 
Data Manipulation Language.pptx
Data Manipulation Language.pptxData Manipulation Language.pptx
Data Manipulation Language.pptx
 
Sql select statement
Sql select statementSql select statement
Sql select statement
 
Basic SQL Statments
Basic SQL StatmentsBasic SQL Statments
Basic SQL Statments
 
Sql queries
Sql queriesSql queries
Sql queries
 
Module 3.1.pptx
Module 3.1.pptxModule 3.1.pptx
Module 3.1.pptx
 
Query
QueryQuery
Query
 
Sql
SqlSql
Sql
 
Sql insert statement
Sql insert statementSql insert statement
Sql insert statement
 
Introduction to-sql
Introduction to-sqlIntroduction to-sql
Introduction to-sql
 
Les02
Les02Les02
Les02
 
Les02 (restricting and sorting data)
Les02 (restricting and sorting data)Les02 (restricting and sorting data)
Les02 (restricting and sorting data)
 
Aggregate Functions,Final
Aggregate Functions,FinalAggregate Functions,Final
Aggregate Functions,Final
 
Chinabankppt
ChinabankpptChinabankppt
Chinabankppt
 
Database systems administration traning 04
Database systems administration traning  04Database systems administration traning  04
Database systems administration traning 04
 
Teradata imp
Teradata impTeradata imp
Teradata imp
 
Sql 2006
Sql 2006Sql 2006
Sql 2006
 

More from Vivek Singh

C programming session 14
C programming session 14C programming session 14
C programming session 14Vivek Singh
 
C programming session 13
C programming session 13C programming session 13
C programming session 13Vivek Singh
 
C programming session 11
C programming session 11C programming session 11
C programming session 11Vivek Singh
 
C programming session 10
C programming session 10C programming session 10
C programming session 10Vivek Singh
 
C programming session 08
C programming session 08C programming session 08
C programming session 08Vivek Singh
 
C programming session 07
C programming session 07C programming session 07
C programming session 07Vivek Singh
 
C programming session 05
C programming session 05C programming session 05
C programming session 05Vivek Singh
 
C programming session 04
C programming session 04C programming session 04
C programming session 04Vivek Singh
 
C programming session 02
C programming session 02C programming session 02
C programming session 02Vivek Singh
 
C programming session 01
C programming session 01C programming session 01
C programming session 01Vivek Singh
 
C programming session 16
C programming session 16C programming session 16
C programming session 16Vivek Singh
 
Niit aptitude question paper
Niit aptitude question paperNiit aptitude question paper
Niit aptitude question paperVivek Singh
 
Excel shortcut and tips
Excel shortcut and tipsExcel shortcut and tips
Excel shortcut and tipsVivek Singh
 
Sql update statement
Sql update statementSql update statement
Sql update statementVivek Singh
 
Sql tutorial, tutorials sql
Sql tutorial, tutorials sqlSql tutorial, tutorials sql
Sql tutorial, tutorials sqlVivek Singh
 
Sql query tuning or query optimization
Sql query tuning or query optimizationSql query tuning or query optimization
Sql query tuning or query optimizationVivek Singh
 
Sql query tips or query optimization
Sql query tips or query optimizationSql query tips or query optimization
Sql query tips or query optimizationVivek Singh
 
Sql operators comparision & logical operators
Sql operators   comparision & logical operatorsSql operators   comparision & logical operators
Sql operators comparision & logical operatorsVivek Singh
 

More from Vivek Singh (20)

C programming session 14
C programming session 14C programming session 14
C programming session 14
 
C programming session 13
C programming session 13C programming session 13
C programming session 13
 
C programming session 11
C programming session 11C programming session 11
C programming session 11
 
C programming session 10
C programming session 10C programming session 10
C programming session 10
 
C programming session 08
C programming session 08C programming session 08
C programming session 08
 
C programming session 07
C programming session 07C programming session 07
C programming session 07
 
C programming session 05
C programming session 05C programming session 05
C programming session 05
 
C programming session 04
C programming session 04C programming session 04
C programming session 04
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
 
C programming session 01
C programming session 01C programming session 01
C programming session 01
 
C programming session 16
C programming session 16C programming session 16
C programming session 16
 
Niit aptitude question paper
Niit aptitude question paperNiit aptitude question paper
Niit aptitude question paper
 
Excel shortcut and tips
Excel shortcut and tipsExcel shortcut and tips
Excel shortcut and tips
 
Sql update statement
Sql update statementSql update statement
Sql update statement
 
Sql tutorial, tutorials sql
Sql tutorial, tutorials sqlSql tutorial, tutorials sql
Sql tutorial, tutorials sql
 
Sql subquery
Sql subquerySql subquery
Sql subquery
 
Sql rename
Sql renameSql rename
Sql rename
 
Sql query tuning or query optimization
Sql query tuning or query optimizationSql query tuning or query optimization
Sql query tuning or query optimization
 
Sql query tips or query optimization
Sql query tips or query optimizationSql query tips or query optimization
Sql query tips or query optimization
 
Sql operators comparision & logical operators
Sql operators   comparision & logical operatorsSql operators   comparision & logical operators
Sql operators comparision & logical operators
 

Recently uploaded

Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
FILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinoFILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinojohnmickonozaleda
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 

Recently uploaded (20)

Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
FILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinoFILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipino
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 

Sql order by clause

  • 1. 8/10/13 SQL ORDER BY Clause beginner-sql-tutorial.com/sql-order-by-clause.htm 1/3 SQL ORDER BY The ORDER BY clause is used in a SELECT statement to sort results either in ascending or descending order. Oracle sorts query results in ascending order by default. Syntax for using SQL ORDER BY clause to sort data is: SELECT column-list FROM table_name [WHERE condition] [ORDER BY column1 [, column2, .. columnN] [DESC]]; database table "employee"; id name dept agesalary location 100RameshElectrical 24 25000 Bangalore 101Hrithik Electronics 28 35000 Bangalore 102Harsha Aeronautics 28 35000 Mysore 103Soumya Electronics 22 20000 Bangalore 104Priya InfoTech 25 30000 Mangalore For Example: If you want to sort the employee table by salary of the employee, the sql query would be. SELECT name, salary FROM employee ORDER BY salary; The output would be like name salary ---------- ---------- Soumya 20000 Ramesh 25000 Priya 30000 Hrithik 35000 Harsha 35000 The query first sorts the result according to name and then displays it. You can also use more than one column in the ORDER BY clause.
  • 2. 8/10/13 SQL ORDER BY Clause beginner-sql-tutorial.com/sql-order-by-clause.htm 2/3 If you want to sort the employee table by the name and salary, the query would be like, SELECT name, salary FROM employee ORDER BY name, salary; The output would be like: name salary ---------- --- ---------- --- Soumya 20000 Ramesh 25000 Priya 30000 Harsha 35000 Hrithik 35000 NOTE:The columns specified in ORDER BY clause should be one of the columns selected in the SELECT column list. You can represent the columns in the ORDER BY clause by specifying the position of a column in the SELECT list, instead of writing the column name. The above query can also be written as given below, SELECT name, salary FROM employee ORDER BY 1, 2; By default, the ORDER BY Clause sorts data in ascending order. If you want to sort the data in descending order, you must explicitly specify it as shown below. SELECT name, salary FROM employee ORDER BY name, salary DESC; The above query sorts only the column 'salary' in descending order and the column 'name' by ascending order.
  • 3. 8/10/13 SQL ORDER BY Clause beginner-sql-tutorial.com/sql-order-by-clause.htm 3/3 If you want to select both name and salary in descending order, the query would be as given below. SELECT name, salary FROM employee ORDER BY name DESC, salary DESC; How to use expressions in the ORDER BY Clause? Expressions in the ORDER BY clause of a SELECT statement. For example: If you want to display employee name, current salary, and a 20% increase in the salary for only those employees for whom the percentage increase in salary is greater than 30000 and in descending order of the increased price, the SELECT statement can be written as shown below SELECT name, salary, salary*1.2 AS new_salary FROM employee WHERE salary*1.2 > 30000 ORDER BY new_salary DESC; The output for the above query is as follows. name salarynew_salary -------- -- ------- --- ------------- Hrithik 35000 37000 Harsha35000 37000 Priya 30000 36000 NOTE:Aliases defined in the SELECT Statement can be used in ORDER BY Clause.