SQL Basics

Chapter 3 - Sorting

  • SQL statements are made up of clauses
  • Some clauses are required; others are optional
  • A clause usually consists of a keyword and an associated value
  • For example, the SELECT statements used here include a required FROM clause
  • ORDER BY is also a clause of the SELECT statement (optional)
  • It sorts the result
  • ORDER BY comes last in these SELECT statements, except for row-limiting clauses such as LIMIT or FETCH
  • You can also sort by a column that is not included in the SELECT result
SELECT   <column>
FROM     <table>
ORDER BY <column>	

Select the product names from the Products table and sort them (9)

-- Oracle / MySQL / SQL Server
SELECT   prod_name
FROM     Products
ORDER BY prod_name;
prod_name
12 inch teddy bear
18 inch teddy bear
8 inch teddy bear
Bird bean bag toy
Fish bean bag toy
King doll
Queen doll
Rabbit bean bag toy
Raggedy Ann
SELECT   <column1>,
         <column2>,
         <column3>
FROM     <table>
ORDER BY <column1>,
         <column2>;

Select the product ID, price and name from Products, and sort by price and name

-- Oracle / MySQL / SQL Server
SELECT   prod_id,
         prod_price,
         prod_name
FROM     Products
ORDER BY prod_price,
         prod_name;
prod_idprod_priceprod_name
BNBG023.49Bird bean bag toy
BNBG013.49Fish bean bag toy
BNBG033.49Rabbit bean bag toy
RGAN014.99Raggedy Ann
BR015.998 inch teddy bear
BR028.9912 inch teddy bear
RYL019.49King doll
RYL029.49Queen doll
BR0311.9918 inch teddy bear

The rows are sorted by price first, then by name. If every price is unique, sorting by name has no effect.

  • Specify one or more numbers in ORDER BY to sort by the position of a column in the SELECT statement
  • You cannot use a position number for a column that is not included in SELECT
SELECT	 <column1>,
         <column2>,
         <column3>
FROM     <table>
ORDER BY 1, 3;

Select the product ID, price and name from Products, and sort by column positions 2 and 3

-- Oracle / MySQL / SQL Server
SELECT	 prod_id,
         prod_price,
         prod_name
FROM     Products
ORDER BY 2,3;
prod_idprod_priceprod_name
BNBG023.49Bird bean bag toy
BNBG013.49Fish bean bag toy
BNBG033.49Rabbit bean bag toy
RGAN014.99Raggedy Ann
BR015.998 inch teddy bear
BR028.9912 inch teddy bear
RYL019.49King doll
RYL029.49Queen doll
BR0311.9918 inch teddy bear

Be careful when changing the order of columns in the SELECT statement

  • By default, ORDER BY sorts from A to Z (Ascending)
  • To sort from Z to A, add the DESC keyword (descending)
SELECT   <column1>,
         <column2>,
         <column3>
FROM     <table>
ORDER BY <column1> DESC,
         <column2>;

Select the product ID, price and name from Products, and sort by price and name in descending order

-- Oracle / MySQL / SQL Server
SELECT	  prod_id,
          prod_price,
          prod_name
FROM      Products
ORDER BY  prod_price DESC,
          prod_name DESC;
prod_idprod_priceprod_name
BR0311.9918 inch teddy bear
RYL029.49Queen doll
RYL019.49King doll
BR028.9912 inch teddy bear
BR015.998 inch teddy bear
RGAN014.99Raggedy Ann
BNBG033.49Rabbit bean bag toy
BNBG013.49Fish bean bag toy
BNBG023.49Bird bean bag toy

If both price and name should be descending, specify DESC twice

Whether A and a are treated as equal when sorting depends on the database and its DBMS

Question SQL
1 - What do I want to see? SELECT
2 - Where do I get it from? FROM
3 - What do I want to sort by? ORDER BY
  1. Select cust_name from Customers and sort by cust_name (5)


  1. Select everything from OrderItems and sort by order_num and item_price (18)


  1. Select everything from OrderItems and sort by quantity in descending order (18)


  1. Select the vendor names and countries from the Vendors table (6)


  1. Select the product ID and quantity from OrderItems, and sort by quantity (18)