Sorting SQL data in ascending and descending order
Written by Devanshu Agarwal /
As a SQL user, sorting data in a logical and organized way is essential for efficient data analysis. The ORDER BY clause in SQL allows users to sort data in either ascending or descending order based on their preferences. In this article, we will delve into how to sort data in ascending and descending order using the ORDER BY ASC or DESC clause, including sorting by multiple columns.
Let's see the table how it looks
Employees Table:
id | name | department | salary |
---|---|---|---|
1 | John Doe | Marketing | 5000 |
2 | Jane Doe | Marketing | 6000 |
3 | Bob Smith | HR | 4000 |
4 | Sarah Lee | HR | 4500 |
5 | Tom Chen | IT | 7000 |
Sorting SQL Data in Ascending Order Using ORDER BY ASC
To sort SQL data in ascending order, you can use the ORDER BY ASC clause. The ASC keyword specifies that the data should be sorted in ascending order. For instance, you can use the following SQL query to sort data in ascending order:
SELECT * FROM employees
ORDER BY salary ASC;
This SQL query will sort the "employees" table by the "salary" column in ascending order.
Output
id | name | department | salary |
---|---|---|---|
3 | Bob Smith | HR | 4000 |
4 | Sarah Lee | HR | 4500 |
1 | John Doe | Marketing | 5000 |
2 | Jane Doe | Marketing | 6000 |
5 | Tom Chen | IT | 7000 |
Sorting SQL Data in Descending Order Using ORDER BY DESC
To sort SQL data in descending order, use the ORDER BY DESC clause. The DESC keyword specifies that the data should be sorted in descending order. Here's an example of sorting data in descending order:
SELECT * FROM employees
ORDER BY salary DESC;
This query will sort the employees table by the salary column in descending order.
Output
id | name | department | salary |
---|---|---|---|
5 | Tom Chen | IT | 7000 |
2 | Jane Doe | Marketing | 6000 |
1 | John Doe | Marketing | 5000 |
4 | Sarah Lee | HR | 4500 |
3 | Bob Smith | HR | 4000 |
Sorting SQL Data by Multiple Columns
SQL data can also be sorted by multiple columns by adding more than one column to the ORDER BY clause. Here's an example of sorting data by multiple columns:
SELECT * FROM employees
ORDER BY department, salary DESC;
This query will sort the employees table by the department column in ascending order first, and then by the salary column in descending order.
Output
id | name | department | salary |
---|---|---|---|
4 | Sarah Lee | HR | 4500 |
3 | Bob Smith | HR | 4000 |
5 | Tom Chen | IT | 7000 |
2 | Jane Doe | Marketing | 6000 |
1 | John Doe | Marketing | 5000 |
Conclusion
Sorting SQL data using the ORDER BY ASC or DESC clause is a crucial aspect of data analysis. It helps you to arrange your data in ascending or descending order as per your needs. Additionally, sorting by multiple columns further refines data analysis. Now that you have this knowledge, you can effectively sort and analyze your SQL data.
Read More
SQL Intro: The Beginner's Guide to SQL Basics
Discover SQL syntax and learn the fundamentals of SQL for beginners. Start coding SQL databases in minutes with this comprehensive guide.
SQL Practice Queries - 1
Looking to enhance ur DB skills? With our advanced sql queries, you'll learn techniques to improve your database skills.
SQL Practice Queries - 2
Looking to enhance your database management skills and take your SQL knowledge to the next level? With our advanced SQL functions and queries training, you'll learn the syntax and techniques to improv ...
Practical Examples of Using SQL Distinct, WHERE Clause and AND, OR, and NOT operators
See practical examples of how to use the SQL Distinct, WHERE Clause and AND, OR, and NOT operators