Search⌘ K

ORDER BY & GROUP BY

Explore how to use the SQL ORDER BY clause to sort query results by one or more columns in ascending or descending order. Understand the GROUP BY clause to arrange data into groups based on specified columns, facilitating aggregation and analysis. This lesson helps you gain practical skills in managing and summarizing database data efficiently using these fundamental SQL clauses.

The ORDER BY clause

The SQL ORDER BY clause is used to sort the data of one or more columns in ascending or descending order. Some databases sort the query results in ascending order by default.

Syntax

The basic syntax of the ORDER BY clause is as follows:

SELECT column-list 

FROM table_name 

WHERE condition 

ORDER BY column1, column2, .. columnN;

You can use more than one column in the ORDER BY clause. Make sure whatever column you are using to sort is in the column-list.

Example

We will sort the CUSTOMERS table in ascending order by ...