Thursday, April 28, 2016

ORDER BY CLAUSE


This SQL tutorial explains how to use the SQL ORDER BY clause with syntax and examples. Click the "Try It" button next to an example to test the ORDER BY clause for yourself in our SQL Editor.

DESCRIPTION

The SQL ORDER BY clause is used to sort the records in the result set for a SELECT statement.

SYNTAX

The syntax for the SQL ORDER BY clause is:
SELECT expressions
FROM tables
[WHERE conditions]
ORDER BY expression [ ASC | DESC ];

Parameters or Arguments

expressions
The columns or calculations that you wish to retrieve.
tables
The tables that you wish to retrieve records from. There must be at least one table listed in the FROM clause.
WHERE conditions
Optional. Conditions that must be met for the records to be selected.
ASC
Optional. ASC sorts the result set in ascending order by expression. This is default behavior, if no modifier is provider.
DESC
Optional. DESC sorts the result set in descending order by expression.

NOTE

  • If the ASC or DESC modifier is not provided in the ORDER BY clause, the results will be sorted by expression in ascending order. This is equivalent to ORDER BY expression ASC.

DDL/DML FOR EXAMPLES

If you want to follow along with this tutorial, get the DDL to create the tables and the DML to populate the data. Then try the examples in your own database!

EXAMPLE - SORTING WITHOUT USING ASC/DESC ATTRIBUTE

The SQL ORDER BY clause can be used without specifying the ASC or DESC value. When this attribute is omitted from the SQL ORDER BY clause, the sort order is defaulted to ASC or ascending order. Let's explore this further.
In this example, we have a table called customers with the following data:
customer_idlast_namefirst_namefavorite_website
4000JacksonJoetechonthenet.com
5000SmithJanedigminecraft.com
6000FergusonSamanthabigactivities.com
7000ReynoldsAllencheckyourmath.com
8000AndersonPaigeNULL
9000JohnsonDerektechonthenet.com
Enter the following SQL statement:
Try It
SELECT *
FROM customers
ORDER BY last_name;
These are the results that you should see:
customer_idlast_namefirst_namefavorite_website
8000AndersonPaigeNULL
6000FergusonSamanthabigactivities.com
4000JacksonJoetechonthenet.com
9000JohnsonDerektechonthenet.com
7000ReynoldsAllencheckyourmath.com
5000SmithJanedigminecraft.com
This example would return all records from the customers sorted by the last_name field in ascending order and would be equivalent to the following SQL ORDER BY clause:
Try It
SELECT *
FROM customers
ORDER BY last_name ASC;
Most programmers omit the ASC attribute if sorting in ascending order.

EXAMPLE - SORTING IN DESCENDING ORDER

When sorting your result set in descending order, you use the DESC attribute in your ORDER BY clause. Let's take a closer look.
In this example, we have a table called suppliers with the following data:
supplier_idsupplier_namecity
100MicrosoftRedmond
200RIMWaterloo
300OracleRedwood City
400GoogleMountain View
500IntelSanta Clara
600SamsungSeoul
Enter the following SQL statement:
Try It
SELECT *
FROM suppliers
WHERE supplier_id > 200
ORDER BY supplier_id DESC;
These are the results that you should see:
supplier_idsupplier_namecity
600SamsungSeoul
500IntelSanta Clara
400GoogleMountain View
300OracleRedwood City
This example would sort the result set by the supplier_id field in descending order.

EXAMPLE - SORTING BY RELATIVE POSITION

You can also use the SQL ORDER BY clause to sort by relative position in the result set, where the first field in the result set is 1, the second field is 2, the third field is 3, and so on.
In this example, we have a table called products with the following data:
product_idproduct_namecategory_id
1Pear50
2Banana50
3Orange50
4Apple50
5Bread75
6Sliced Ham25
7KleenexNULL
Now enter the following SQL statement:
Try It
SELECT product_id, product_name
FROM products
WHERE product_name <> 'Bread'
ORDER BY 1 DESC;
These are the results that you should see:
product_idproduct_name
7Kleenex
6Sliced Ham
4Apple
3Orange
2Banana
1Pear
This example would sort the results by the product_id field in descending order, since the product_id field is in position #1 in the result set and would be equivalent to the following SQL ORDER BY clause:
Try It
SELECT product_id, product_name
FROM products
WHERE product_name <> 'Bread'
ORDER BY product_id DESC;

EXAMPLE - USING BOTH ASC AND DESC ATTRIBUTES

When sorting your result set using the SQL ORDER BY clause, you can use the ASC and DESC attributes in a single SELECT statement.
In this example, let's use the same products table as the previous example:
product_idproduct_namecategory_id
1Pear50
2Banana50
3Orange50
4Apple50
5Bread75
6Sliced Ham25
7KleenexNULL
Now enter the following SQL statement:
Try It
SELECT *
FROM products
WHERE product_id <> 7
ORDER BY category_id DESC, product_name ASC;
These are the results that you should see:
product_idproduct_namecategory_id
5Bread75
4Apple50
2Banana50
3Orange50
1Pear50
6Sliced Ham25
This example would return the records sorted by the category_id field in descending order, with a secondary sort byproduct_name in ascending orde

No comments:

Post a Comment