alternative
  • Home (current)
  • About
  • Tutorial
    Technologies
    C#
    Deep Learning
    Statistics for AIML
    Natural Language Processing
    Machine Learning
    SQL -Structured Query Language
    Python
    Ethical Hacking
    Placement Preparation
    Quantitative Aptitude
    View All Tutorial
  • Quiz
    C#
    SQL -Structured Query Language
    Quantitative Aptitude
    Java
    View All Quiz Course
  • Q & A
    C#
    Quantitative Aptitude
    Java
    View All Q & A course
  • Programs
  • Articles
    Identity And Access Management
    Artificial Intelligence & Machine Learning Project
    How to publish your local website on github pages with a custom domain name?
    How to download and install Xampp on Window Operating System ?
    How To Download And Install MySql Workbench
    How to install Pycharm ?
    How to install Python ?
    How to download and install Visual Studio IDE taking an example of C# (C Sharp)
    View All Post
  • Tools
    Program Compiler
    Sql Compiler
    Replace Multiple Text
    Meta Data From Multiple Url
  • Contact
  • User
    Login
    Register

SQL -Structured Query Language - Sql Order By - Ascending Descending Multiple Column Tutorial

To retrieve a record in multiple orders for different columns from the existing created table. Then we can use an ' ORDER BY ' Clause multiple times with ASC or DESC differently depending upon the requirement.

 

Syntax-

The basic syntax of the ORDER BY statement with ASC / DESC Attribute is given below –

SELECT * FROM 
table_name 
ORDER BY 
column_name1 [ASC / DESC], 
column_name2 [ASC / DESC],…, 
column_nameN [ASC / DESC];

 

Here 1st priority will always be given to column_name1, 2nd priority to column_name2, and last priority to column_nameN.

Suppose if the user retrieves a record in ascending order of a name, And there are two or more records with the same name. So for the same name record, he wants to retrieve its age in descending order

 

For example-

Example 1

SELECT first_name,last_name 
FROM 
employees 
ORDER BY 
first_name ASC, last_name DESC;

Run Query

Here it will retrieve all records from the Employees table in ascending order of a first_name column, and if the record having two or more same first_name. then it will check the last_name, and order it in descending manner.

 

Example 2

SELECT first_name,last_name 
FROM 
employees 
ORDER BY 
last_name DESC, first_name ASC;

Run Query

Here it will retrieve all records from the Employees table in descending order of a last_name column, and if the record having two or more same last_name. then it will check the first_name, and order it in ascending manner.

In this 'ORDER BY first_name ASC, last_name DESC ' and 'ORDER BY last_name DESC, first_name ASC' both look the same, but both will retrieve data in a different order.

Example 1 and  Example 2 will work differently.

SQL -Structured Query Language

SQL -Structured Query Language

  • Introduction
  • Overview
    • How To Download And Install MySql Workbench
    • SQL Syntax
  • SQL Database
    • Create Database
    • Drop Database
    • Use / Select Database
  • SQL Table
    • Create Table
    • Alter - Rename Table
    • Alter - Add Column
    • Alter - Modify Column
    • Alter - Rename Column
    • Alter - Drop Column
    • Copy Table
    • Drop Table
  • SQL Insert
    • Insert Record
  • Sql Update
    • Update Record
  • Sql Select
    • Select Record
    • Select - Count
    • Select - Average
    • Select - Sum
    • Select - Min Max
    • Select - Distinct
    • Select - Limit
    • Select - First
    • Select - Last
  • Sql Clause
    • Like Clause
    • And Operator
    • Or Operator
    • Between & Not Between Operator
    • As or Alias
    • In Clause
    • Group By Clause
    • Having Clause
  • Sql Order By
    • Ascending
    • Descending
    • Random
    • Ascending Descending Multiple Column
  • SQL Joins
    • Joins and its type
    • Inner Join
    • Left Join
    • Right Join
    • Full Join
    • Cross Join
  • SQL View
    • Create View
    • Update View
    • Delete View
  • SQL Delete
    • Delete Table
    • Truncate Table
  • Sql Key & Constraint
    • Primary Key
    • Foreign Key
    • Unique Key
    • Not Null Constraint
    • Check Constraint
    • Default Constraint
  • Normalization
    • Overview
    • 1 NF
    • 2 NF
    • 3 NF
    • Boyce Codd normal form ( BCNF )

About Fresherbell

Best learning portal that provides you great learning experience of various technologies with modern compilation tools and technique

Important Links

Don't hesitate to give us a call or send us a contact form message

Terms & Conditions
Privacy Policy
Contact Us

Social Media

© Untitled. All rights reserved. Demo Images: Unsplash. Design: HTML5 UP.