SQL-ORDER BY single/multiple columns ASC and DESC | Data Sorting in SQL

This article is about “SQL-ORDER BY single/multiple columns ASC and DESC | Data Sorting in SQL” function, hope you will like the information. If yes please do share it with others.

ASC keyword is used at the end of SQL query to sort the records in ascending order.

Syntax: Select * from table order by salary ASC;

And DESC keyword instead of ASC to sort the data in descending order.

To sort by multiple columns mention name of both the columns instead of just one. Instead of writing the names one can use numbers as well for e.g.

Type 1: Select column_a, column_b from table order by column_a, column_b;

Type 2: Select column_a, column_b from table order by 1, 2;

So, there are two ways of writing the query order by clause as mentioned in above two sql statements.

How do you order by in SQL numerical column in ascending order i.e. | Data Sorting in SQL

Step 1: Write query:

  • Select * from table order by salary;

First step is writing the error free correct query and then executing it.

Step 2: Run the query.

  • By default the statement mentioned in Step-1 will order the salary column in ascending (lowest to highest) order for e.g. in example below data is sorted in increasing order of salary i.e from Unacademy to Glassdoor.
  • SQL sorting command is shown in red colour in all the example images below.
SQL order by or sorting numerical column in ascending order.

How do you write a query for ORDER BY? | SQL order by numerical column in descending order

Step 1: Write query in your sql console:

  • Select * from table order by salary desc;

where, desc in above query stands for descending order.

Step 2: Run the query.

  • Use of desc keyword in end in the above query will arrange the data in decreasing order of salary i.e. from Glassdoor to Unacademy.
  • Here, SQL sorting command is:

“Select * from table order by salary desc”

SQL order by or sorting numerical column in descending order.

How do I sort by alphabetical order in SQL? | SQL order by text column in ascending order | alphabetical order in sql

Step 1: Write query:

  • Select * from table order by website;

Step 2: Run the query.

  • By default this will order the website column in ascending (A to Z) order. Refer the image below for example i.e. data is sorted from Byjus to Unacademy.
SQL order by or sorting text column in ascending order.

So above query will arrange the table in alphabetical order using sql i.e. from A to z for opposite refer the next section.

SQL order by text column in descending order

Step 1: Write query:

  • Select * from table order by website desc;

Step 2: Run the query.

  • Use of desc keyword at the end orders the while dataset in descending (Z to A) order of website column. Look to below image for example i.e. data is sorted from Unacademy to Byju’s.
SQL order by or sorting text column in descending order.

Other important topics related to order by or sorting data in SQL query are:

  • How to Order by multiple columns in SQL with example:
    • Query example: Select * from table order by 1,2;
    • In the above example the final data will be sorted by first and second column of SQL query’s results.
  • Order by multiple columns ASC and DESC both
    • Query example: Select * from table order by 1 ASC, 2 DESC;
  • How to Order by date in SQL with example:
    • Query example: Select date, name from table order by 1;
    • The above example will sort the data by date in ascending order.
  • Order by 1,2 etc.

Other useful articles:


Similar Articles:

Final words:

So, this is all about “SQL-ORDER BY single/multiple columns ASC and DESC | Data Sorting in SQL” function, do let us know in comment section what else you want to read about or some other information you require in the current topic i.e. “SQL-ORDER BY single/multiple columns ASC and DESC | Data Sorting in SQL”, we are more than happy to help you.

FAQs:

Q. What is meant by ORDER BY 1 in SQL?

Ans. Using ORDER BY 1 in SQL query means sorting the data by first column of your SQL query’s result.

Q. What does ORDER BY 2 mean in SQL?

Ans. It means sorting the data by second column of your SQL query’s result.

Q. WHERE does ORDER BY Go in SQL?

Ans. In SQL query the ORDER BY clause comes at the end of select statement i.e. select > from > where > group by > order by.

Q. Can we use WHERE with ORDER BY?

Ans. Yes we can use WHERE clause with ORDER BY clause, also WHERE clause can be used with or without ORDER BY clause and vice versa.

Q. How do you use group by and ORDER BY together?

Ans. SELECT column_a, column_b FROM table WHERE column_a like ‘%new%’ GROUP BY 1,2 ORDER BY 1;
GROUP BY clause comes after WHERE clause and before ORDER BY clause, just follow the correct syntax and secondly do not apply GROUP BY on aggregates as in that case query will start throwing error.

Q. What is the default sort order of ORDER BY clause?

Ans. ASCENDING (ASC) is the default sort order of ORDER BY clause unless DESC is cpecifically mentioned with any column inside ORDER BY clause.

Q. What is the difference between GROUP BY and ORDER BY clause?

Ans. GROUP BY is used to group data by same values in a column, whereas ORDER BY is used to sort the data in ascending or descending order.
Also, both can be used without the other one i.e. it’s not necessary to use both together.