SQL is the Structured Query Language that is used to communicate commands to a database server. It is used by databases like MySQL, PostgreSQL, SQL Server, etc. SQL can be used to insert, delete, update, and search database records. This article will discuss the ORDER BY keyword in SQL.

Order By Keyword in SQL

The ORDER BY keyword in SQL is used to sort the result in ascending or descending order.

Note: By default, the results are sorted in ascending order. 

Syntax: 

SELECT column1, column2, ...

FROM table_name

ORDER BY column1, column2, ... ASC|DESC

For our learning purpose, we will be using the given below data to understand the ORDER BY keyword.

sample-data

Become The Highest-Paid Business Analysis Expert

With Business Analyst Master's ProgramExplore Now
Become The Highest-Paid Business Analysis Expert

Order By ASC

The following SQL statement selects all the customers from the customer table and sorts them in the ascending order of the first name alphabetically:

orderby-in-SQL-asc

As you can see, your data is now sorted in ascending order by “first_name”.

Order By DESC

The following SQL statement selects all the customers from the customer table and sorts them in descending order:

orderby-desc

The data is now sorted in descending order by “first_name”.

Order By Several Columns

The following SQL statement selects all customers from the "Customers" table, sorted ascending by the "first_name" and descending by the "city" column. This means that it orders by “first_name”, but if some rows have the same first name, it orders them by city:

orderbyseveral

Gain expertise in the latest Business analytics tools and techniques with the Post Graduate Program in Business Analysis. Enroll now!

Conclusion

This article discussed the ORDER BY keyword in SQL and how it is used to sort the results. If you wish to learn more about SQL, then check out our SQL certification course.

This SQL certification course gives you all of the information you need to successfully start working with SQL databases and make use of the database in your applications. Learn how to correctly structure your database, author efficient SQL statements, and clauses, and manage your SQL database for scalable growth.

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 17 Jun, 2024

6 Months$ 8,000
Full Stack Java Developer

Cohort Starts: 4 Jun, 2024

6 Months$ 1,449
Full Stack Developer - MERN Stack

Cohort Starts: 18 Jun, 2024

6 Months$ 1,449
Automation Test Engineer

Cohort Starts: 19 Jun, 2024

11 Months$ 1,499