You are currently viewing SQL 1.6 SQL ORDER BY

SQL 1.6 SQL ORDER BY

SQL ORDER BY Tutorial

SQL ORDER BY tutorial! In this tutorial, we will cover the basics of using the ORDER BY clause in SQL. ORDER BY is used to sort the result set of a SQL query based on one or more columns. This can be very useful when you want to organize your data in a specific order before presenting it to the user.

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, ...: Columns that you want to sort by.
  • table_name: The name of the table from which you are retrieving data.
  • [ASC|DESC]: Optional. ASC for ascending order (default), DESC for descending order.

Examples

Let’s dive into some examples to understand ORDER BY better.

Example 1: Sorting by a Single Column

Suppose we have a table called employees with columns employee_id, first_name, last_name, and salary. We want to retrieve the employees’ names and salaries sorted by salary in descending order.

  1. Create a table employees:
CREATE TABLE employees (
    employee_id INT,
    first_name VARCHAR(50),
    last_name VARCHAR(50),
    salary DECIMAL(10, 2)
);

INSERT INTO employees (employee_id, first_name, last_name, salary)
VALUES
    (1, 'John', 'Doe', 50000.00),
    (2, 'Jane', 'Smith', 60000.00),
    (3, 'Michael', 'Johnson', 45000.00),
    (4, 'Emily', 'Davis', 70000.00),
    (5, 'Robert', 'Wilson', 55000.00);
  1. Retrieve data sorted by salary in descending order:
SELECT first_name, last_name, salary
FROM employees
ORDER BY salary DESC;

Output:

first_name | last_name | salary
-----------|-----------|--------
Emily      | Davis     | 70000.00
Jane       | Smith     | 60000.00
Robert     | Wilson    | 55000.00
John       | Doe       | 50000.00
Michael    | Johnson   | 45000.00

In this example, we sorted the result set by the salary column in descending order.

Example 2: Sorting by Multiple Columns

Now let’s say we want to sort the employees by their last names in ascending order, and for employees with the same last name, we want to sort by their first names.

  1. Retrieve data sorted by last name (ascending) and then by first name (ascending):
SELECT first_name, last_name
FROM employees
ORDER BY last_name ASC, first_name ASC;

Output:

first_name | last_name
-----------|----------
John       | Doe
Emily      | Davis
Michael    | Johnson
Jane       | Smith
Robert     | Wilson

In this example, the result set is first sorted by last_name in ascending order. For employees with the same last_name, it sorts by first_name in ascending order.

Conclusion

The ORDER BY clause is a powerful tool in SQL that allows you to sort the result set of your queries based on one or more columns. By understanding its syntax and examples, you can effectively organize and present your data in the desired order. Practice using ORDER BY with different scenarios to become more comfortable with its usage in SQL queries.

Leave a Reply