We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

ORDER BY

SQL also offers us the ability to sort the results of a query using ORDER BY. By default, the ORDER BY keyword sorts records by the given field in ascending order, or ASC for short. However, ORDER BY does support descending order as well with the keyword DESC.

Examples

This query returns the name, price, and quantity fields from the products table sorted by price in ascending order:

SELECT name, price, quantity FROM products
ORDER BY price;

This query returns the name, price, and quantity of the products ordered by quantity in descending order:

SELECT name, price, quantity FROM products
ORDER BY quantity DESC;

Assignment

Write a query that lists all the records in the transactions table where:

  • amount is BETWEEN 10 and 80 dollars.
  • The results are sorted by amount in descending order.

Tip

Remember, BETWEEN is inclusive on both ends. The condition WHERE quantity BETWEEN 5 AND 10, for example, would include records where quantity is 5, 10, or anywhere between.