SQL Tutorial

SQL Clauses / Operators

SQL-Injection

SQL Functions

SQL Queries

PL/SQL

MySQL

SQL Server

Misc

SQL | ORDER BY

The ORDER BY clause in SQL is used to sort the result set of a query by one or more columns. It allows you to present your results in a more meaningful order, especially when the inherent order of rows in a database table is not guaranteed by most RDBMS systems.

Basic Syntax:

SELECT column1, column2, ...
FROM table_name
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...;
  • ASC: Ascending order (default if not specified).
  • DESC: Descending order.

Examples:

  1. Order by a Single Column:

    If you want to order a list of employees by their last names:

    SELECT first_name, last_name 
    FROM employees 
    ORDER BY last_name;
    

    By default, the order will be ascending. If you want the order to be descending:

    SELECT first_name, last_name 
    FROM employees 
    ORDER BY last_name DESC;
    
  2. Order by Multiple Columns:

    To order by the department first and then by last name within each department:

    SELECT department, first_name, last_name 
    FROM employees 
    ORDER BY department, last_name;
    

    You can also mix the order directions:

    SELECT department, first_name, last_name 
    FROM employees 
    ORDER BY department ASC, last_name DESC;
    
  3. Order by Using Column Position:

    Sometimes, it might be convenient to order by the position of the column in the SELECT list rather than the column name:

    SELECT first_name, last_name 
    FROM employees 
    ORDER BY 2;  -- This orders by the second column (last_name) in ascending order
    
  4. Order by Aggregate Function Results:

    If you're using aggregate functions, you can order by the result of these functions:

    SELECT department, COUNT(*) as num_employees 
    FROM employees 
    GROUP BY department 
    ORDER BY num_employees DESC;
    

    This query retrieves the number of employees in each department, ordering the departments by their employee counts in descending order.

Important Notes:

  • The ORDER BY clause always comes after the WHERE and GROUP BY clauses in a SQL statement.

  • In databases that support the LIMIT or OFFSET-FETCH clauses for pagination, the ORDER BY clause typically comes before these pagination clauses.

  • It's always a good idea to provide an explicit order if the sequence of the results is important, as most RDBMS systems don't guarantee a specific order without an ORDER BY clause.

  • Be careful when using the column position approach, as changes to the SELECT list can inadvertently change the ordering of results if you're not careful. It's generally more readable and safer to use explicit column names.

  1. How to use ORDER BY in SQL:

    • Sorts the result set based on one or more columns.
    SELECT column1, column2
    FROM example_table
    ORDER BY column1;
    
  2. Sorting results in ascending and descending order:

    • Use ASC for ascending order (default) and DESC for descending order.
    SELECT column1, column2
    FROM example_table
    ORDER BY column1 DESC;
    
  3. ORDER BY with multiple columns in SQL:

    • Sorts by multiple columns in sequence.
    SELECT column1, column2, column3
    FROM example_table
    ORDER BY column1, column2;
    
  4. Sorting NULL values in ORDER BY:

    • Control the order of NULL values using NULLS FIRST or NULLS LAST.
    SELECT column1, column2
    FROM example_table
    ORDER BY column1 NULLS LAST;
    
  5. ORDER BY and column aliases in SQL:

    • Use column aliases in the ORDER BY clause.
    SELECT column1 AS alias1, column2 AS alias2
    FROM example_table
    ORDER BY alias1;
    
  6. ORDER BY with expressions and functions:

    • Sort based on expressions or function results.
    SELECT column1, column2
    FROM example_table
    ORDER BY LEN(column2), column1 DESC;
    
  7. ORDER BY and LIMIT for pagination in SQL:

    • Use ORDER BY in combination with LIMIT for result set pagination.
    SELECT column1, column2
    FROM example_table
    ORDER BY column1
    LIMIT 10;
    
  8. ORDER BY and FETCH FIRST in SQL:

    • Use ORDER BY with FETCH FIRST for result set limitation.
    SELECT column1, column2
    FROM example_table
    ORDER BY column1
    FETCH FIRST 10 ROWS ONLY;
    
  9. Sorting results by specific criteria in SQL:

    • Customize sorting based on specific criteria.
    SELECT column1, column2
    FROM example_table
    ORDER BY
        CASE WHEN column1 < 5 THEN 1
             WHEN column1 >= 5 AND column1 < 10 THEN 2
             ELSE 3
        END,
        column1 DESC;
    
  10. Sorting alphanumeric data in SQL:

    • Handle alphanumeric sorting using CAST or CONVERT.
    SELECT column1, column2
    FROM example_table
    ORDER BY CAST(column1 AS INT), column2;
    
  11. ORDER BY and GROUP BY in SQL:

    • Sort within groups when using GROUP BY.
    SELECT column1, COUNT(*)
    FROM example_table
    GROUP BY column1
    ORDER BY column1;
    
  12. ORDER BY and HAVING clause in SQL:

    • Sort results after filtering using the HAVING clause.
    SELECT column1, COUNT(*)
    FROM example_table
    GROUP BY column1
    HAVING COUNT(*) > 1
    ORDER BY COUNT(*) DESC;
    
  13. ORDER BY and subqueries in SQL:

    • Sort based on the results of a subquery.
    SELECT column1, column2
    FROM example_table
    ORDER BY (SELECT MAX(column3) FROM another_table WHERE column1 = example_table.column1);