SQL ORDER BY statement: sorting

In SQL, the ORDER BY keyword is used to sort the result-set in ascending or descending order. It sorts the records in a result set by one or more columns.

The ORDER BY keyword sorts the records in ascending order by default. If you want to sort the records in a descending order, you can use the DESC keyword.

Here's the basic syntax:

SELECT column1, column2, ...
FROM table_name
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...;

Examples:

Consider the following Employees table:

EmployeeIDFirstNameLastName
1JohnDoe
2JaneSmith
3AliceJohnson
4CharlieBrown
5DavidThompson
  • Order by ascending FirstName:
SELECT * FROM Employees
ORDER BY FirstName ASC;

This SQL statement selects all data from the "Employees" table, sorted in ascending order by the "FirstName" column.

  • Order by descending LastName:
SELECT * FROM Employees
ORDER BY LastName DESC;

This SQL statement selects all data from the "Employees" table, sorted in descending order by the "LastName" column.

  • Order by multiple columns:
SELECT * FROM Employees
ORDER BY FirstName ASC, LastName DESC;

This SQL statement selects all data from the "Employees" table, sorted in ascending order by the "FirstName" and then in descending order by the "LastName".

Note:

  • If the ORDER BY clause is not specified, the order of the rows in the result-set is undefined.
  • Always place the ORDER BY clause at the end of your SQL statement.
  • As always, the exact syntax may vary between different SQL dialects, so be sure to consult the documentation for the SQL dialect you're using.
  1. How to Use ORDER BY in SQL:

    • Description: The ORDER BY clause is used to sort the result set in ascending (default) or descending order based on one or more columns.
    • Code Example:
      SELECT column1, column2
      FROM example_table
      ORDER BY column1;
      
  2. SQL ORDER BY Ascending and Descending:

    • Description: You can specify the sorting order with ASC (ascending) or DESC (descending) in the ORDER BY clause.
    • Code Example:
      SELECT column1, column2
      FROM example_table
      ORDER BY column1 DESC;
      
  3. Sorting Multiple Columns in SQL ORDER BY:

    • Description: You can sort by multiple columns by specifying their order in the ORDER BY clause.
    • Code Example:
      SELECT column1, column2, column3
      FROM example_table
      ORDER BY column1 ASC, column2 DESC;
      
  4. ORDER BY with NULL Values in SQL:

    • Description: By default, NULL values are sorted first in ascending order. Use NULLS LAST to sort them last.
    • Code Example:
      SELECT column1, column2
      FROM example_table
      ORDER BY column1 NULLS LAST;
      
  5. SQL ORDER BY String and Numeric Sorting:

    • Description: SQL automatically sorts strings and numbers appropriately. Ensure the data types match for correct sorting.
    • Code Example:
      SELECT column1, column2
      FROM example_table
      ORDER BY column1, column2;
      
  6. Custom Sorting in SQL ORDER BY:

    • Description: Use a CASE statement in the ORDER BY clause for custom sorting logic.
    • Code Example:
      SELECT column1, column2
      FROM example_table
      ORDER BY CASE WHEN column1 = 'CustomValue' THEN 0 ELSE 1 END, column2;
      
  7. Sorting by Date in SQL ORDER BY:

    • Description: Dates can be sorted using ORDER BY just like other data types.
    • Code Example:
      SELECT column1, column2, date_column
      FROM example_table
      ORDER BY date_column DESC;
      
  8. Limiting Rows with TOP and ORDER BY in SQL:

    • Description: Use TOP (SQL Server) or LIMIT (for other databases) with ORDER BY to limit the number of rows returned.
    • Code Example:
      SELECT TOP 5 column1, column2
      FROM example_table
      ORDER BY column1 DESC;
      
  9. Sorting Results of JOIN in SQL:

    • Description: When joining tables, use ORDER BY to sort the combined result set based on columns from any joined table.
    • Code Example:
      SELECT table1.column1, table2.column2
      FROM table1
      INNER JOIN table2 ON table1.id = table2.id
      ORDER BY table1.column1 ASC, table2.column2 DESC;