Skip to main content
Log inGet a demo
Basic statements and clauses

SQL ORDER BY

What is it the SQL ORDER BY Clause?

The ORDER BY clause in SQL is used to sort the result set of a query in a specific order based on one or more columns. This clause is commonly used to arrange data in ascending or descending order, allowing you to control the presentation of data for better analysis and readability.

When to Use it

You would use the ORDER BY clause when you want to:

  1. Sort the results of a query in a specific order.
  2. Arrange data for easier analysis, reporting, or visualization.
  3. Retrieve data in a meaningful sequence for your application or reporting needs.

Syntax

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

SELECT column1, column2, ...
FROM table_name
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...
  • column1, column2, etc.: The columns by which you want to sort the result set.
  • table_name: The name of the table from which you're retrieving data.
  • ASC (optional): Sort in ascending order (default if not specified).
  • DESC (optional): Sort in descending order.

Parameter Values

  • column1, column2, ...: The columns by which you want to sort the result set. You can specify multiple columns, and the sorting is applied in the order of the columns listed.
  • ASC (Ascending): Sort the data in ascending order (from the smallest to the largest value).
  • DESC (Descending): Sort the data in descending order (from the largest to the smallest value).

Example Query

Let's assume we have a table named employees with the columns employee_id, first_name, and last_name. To retrieve a list of employees sorted by their last names in ascending order, you would use the following query:

SELECT employee_id, first_name, last_name
FROM employees
ORDER BY last_name ASC;

Example Table Response

Assuming the employees table contains the following data:

employee_idfirst_namelast_name
1JohnDoe
2AliceSmith
3BobJohnson

The result of the query will be:

employee_idfirst_namelast_name
1JohnDoe
3BobJohnson
2AliceSmith

Use Cases

  • Sorting Data: It's commonly used for sorting data in alphabetical, numerical, or chronological order.
  • Reporting: Ordering data is crucial for generating meaningful reports.
  • Pagination: To fetch data in manageable chunks, like the first 10 results, next 10, etc.
  • Analyzing Data: Arranging data can help in identifying patterns or anomalies.

SQL Languages

The ORDER BY clause is available in most SQL dialects, including but not limited to:

  • SQL Server
  • MySQL
  • PostgreSQL
  • Oracle Database
  • SQLite
  • IBM Db2
  • MariaDB
  • and more

The syntax may vary slightly between these SQL implementations, but the concept of sorting data remains consistent.


Keep reading

SQL WHERE

Read article

SQL CASE

Read article

SQL GROUP BY

Read article

Ready to put your SQL knowledge to work?

Practice writing SQL to call data from the warehouse and sync it into Google Sheets in this 5 minute interactive demo.

Hightouch Audiences user interface.