Microsoft SQL Server Certification Course
- 6k Enrolled Learners
- Weekend
- Live Class
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.
The following topics will be covered in this article:
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.
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.
StudentID | FirstName | PhoneNumber | City |
1 | Rohan | 9876543210 | Hyderabad |
2 | Sonali | 9876567864 | Bengaluru |
3 | Ajay | 9966448811 | Lucknow |
4 | Geeta | 9765432786 | Lucknow |
5 | Shubham | 9944888756 | Delhi |
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.
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
India | India |
SQL Training in Bangalore | SQL Course in Pune |
SQL Training in Chennai | SQL Course in Mumbai |
SQL Training in Hyderabad | SQL Course in Kolkata |
If you have to sort data in descending order, then you have to mention the keyword DESC after the ORDER BY clause in SQL.
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 |
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.
edureka.co