AWS Global Infrastructure

Databases

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

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

How To Use ORDER BY Clause In SQL?

Published on Aug 29,2019 92 Views
30 / 37 Blog from Introduction to SQL

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

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

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.

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

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.