AWS Global Infrastructure

Databases

Topics Covered
  • Cassandra (14 Blogs)
  • MongoDB Dev and Admin (15 Blogs)
  • MySQL (30 Blogs)
  • SQL Essentials Training and Certification (4 Blogs)
SEE MORE

How To Use ORDER BY Clause In SQL?

Last updated on Mar 18,2024 2.6K Views

30 / 37 Blog from Introduction to SQL

In today’s market where a humongous amount of data is generated every day, it is very important for us to sort the data present in databases. Well, to sort data in SQL, developers usually use the ORDER BY clause. So, in this article on ORDER BY in SQL, I will discuss the various ways in which you can use this clause to sort data.

SQL-ORDER BY-SQL-Edureka-300x144The following topics will be covered in this article:

    1. What is ORDER BY clause in SQL?
    2. Syntax
    3. ORDER BY on one column
    4. ORDER BY DESC 
    5. ORDER BY on several columns

What is ORDER BY in SQL?

The ORDER BY clause is used to sort all the results in ascending or descending order. By default, the results-sets are sorted in ascending order. So, if you want to sort the result set in descending order, you can use the DESC keyword. Apart from this, you can also sort according to one column or multiple columns in a table.

Syntax:

SELECT Column1, Column2, ...ColumnN
FROM TableName
ORDER BY Column1, Column2, ... ASC|DESC;

Since you know the syntax of ORDER BY in SQL, let us see a few examples of this clause.

Consider the following table to see all the operations in the ORDER BY clause. Also, to create tables, in SQL, you can use the CREATE table statement

StudentIDFirstNamePhoneNumberCity

1

Rohan

9876543210

Hyderabad

2

Sonali

9876567864

Bengaluru

3

Ajay

9966448811

Lucknow

4

Geeta

9765432786

Lucknow

5

Shubham

9944888756

Delhi

ORDER BY clause on one column 

If you have to sort data according to a specific column, then you have to mention the name of the column after the ORDER BY clause in SQL.

Example:

Consider a scenario, where you have to write a query to select all students from the “Students” table, sorted by the “City” column.

SELECT * FROM Students
ORDER BY City;

On executing the above query, you will see an output as below:

StudentID

FirstName

PhoneNumber

City

2

Sonali

9876567864

Bengaluru

5

Shubham

9944888756

Delhi

1

Rohan

9876543210

Hyderabad

3

Ajay

9966448811

Lucknow

4

Geeta

9765432786

Lucknow

Find out our MS SQL Course in Top Cities

IndiaIndia
SQL Training in BangaloreSQL Course in Pune
SQL Training in ChennaiSQL Course in Mumbai
SQL Training in HyderabadSQL Course in Kolkata

ORDER BY DESC 

If you have to sort data in descending order, then you have to mention the keyword DESC after the ORDER BY clause in SQL.

Example:

Consider a scenario, where you have to write a query to select all students from the “Students” table, sorted by the “City” column in descending order.

SELECT * FROM Students
ORDER BY City DESC;

On executing the above query, you will see an output as below:

StudentID

FirstName

PhoneNumber

City

4

Geeta

9765432786

Lucknow

3

Ajay

9966448811

Lucknow

1

Rohan

9876543210

Hyderabad

5

Shubham

9944888756

Delhi

2

Sonali

9876567864

Bengaluru

ORDER BY on several columns

If you have to sort data according to many columns, then you have to mention the name of those columns after the ORDER BY clause in SQL.

Example:

Consider a scenario, where you have to write a query to select all students from the “Students” table, sorted by the “City” column and “Firstname” column.

SELECT * FROM Students
ORDER BY City, FirstName;

According to the above query, you will see a result-set, which orders by City, but if some rows have the same City, then they are ordered by FirstName. On executing the above query, you will see an output as below:

StudentID

FirstName

PhoneNumber

City

2

Sonali

9876567864

Bengaluru

5

Shubham

9944888756

Delhi

1

Rohan

9876543210

Hyderabad

3

Ajay

9966448811

Lucknow

4

Geeta

9765432786

Lucknow

You can also sort students based on, sorted by ascending order for “City”, and descending order for “Firstname”. To do that, you can write a code as below:

SELECT * FROM Students
ORDER BY City ASC, FirstName DESC;

On executing the above query, you will see an output as below:

StudentID

FirstName

PhoneNumber

City

2

Sonali

9876567864

Bengaluru

5

Shubham

9944888756

Delhi

1

Rohan

9876543210

Hyderabad

4

Geeta

9765432786

Lucknow

3

Ajay

9966448811

Lucknow

With this, we come to an end to this article on ORDER BY in SQL. I hope you understood how to use the ORDER BY clause in SQL. If you wish to learn more about MySQL and get to know this open-source relational database, then check out our MySQL DBA Certification Training which comes with instructor-led live training and real-life project experience. This training will help you understand MySQL in-depth and help you achieve mastery over the subject.

Learn to query and manage databases like a pro in our Microsoft SQL Training.

Got a question for us? Please mention it in the comments section of this article on “ORDER BY in SQL” and I will get back to you.

Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

How To Use ORDER BY Clause In SQL?

edureka.co