CodeBlanks Logo Icon CodeBlanks

ASC and DESC

Learn ascending and descending ordering.

ASC and DESC

ASC sorts ascending, DESC descending. After ORDER BY you write the column name then the direction.

SELECT name, price
FROM products
ORDER BY price DESC;

This sorts products by price descending; the most expensive on top.

name, price is selected from products. The result is sorted by price in descending order.

ASC and DESC — details

ASC is the default so omitting it still sorts ascending. For text ASC means alphabetical, for numbers smallest first. DESC is the reverse.

To see the most expensive products at the top, you write ORDER BY price DESC.

Practice ASC and DESC

Select name and price from products ordered by price descending.

Starter code

query.sql
-- sort descending
SELECT name, price
FROM products
ORDER BY price

Sample solution

query.sql
SELECT name, price
FROM products
ORDER BY price DESC

Expected output

name   price
Çanta  25
Defter 12
Kalem  5

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Sort descending

Question
SELECT name FROM products
ORDER BY price ____;
Answer
SELECT name FROM products
ORDER BY price DESC;

Explanation: DESC sorts from largest to smallest; the default is ASC.

2 Which word sorts descending?

  • ASC
  • DESC Correct answer
  • DOWN
  • REVERSE

Explanation: DESC sorts in descending order.

3 ASC and DESC: match the terms

Term Meaning
ASC ascending
DESC descending
ORDER BY sort
LIMIT limit

All lessons in Beginner