Geek Slack

Getting Started with SQL Server
    About Lesson


    SQL ORDER BY Keyword


    SQL ORDER BY Keyword

    The ORDER BY keyword is used to sort the result set of a SQL query in ascending or descending order based on specified columns.

    Basic ORDER BY Clause:

    SELECT *
    FROM customers
    ORDER BY last_name;

    This query selects all columns from the “customers” table and orders the result set by the “last_name” column in ascending order.

    ORDER BY with DESC Keyword:

    SELECT *
    FROM products
    ORDER BY price DESC;

    This query selects all columns from the “products” table and orders the result set by the “price” column in descending order.

    ORDER BY Multiple Columns:

    SELECT *
    FROM orders
    ORDER BY order_date DESC, total_amount DESC;

    This query selects all columns from the “orders” table and orders the result set first by the “order_date” column in descending order and then by the “total_amount” column in descending order.

    The ORDER BY clause is commonly used in SQL queries to sort the results and present them in a specified order.