Yandex

ORDER BY Clause
Sorting Results in SQL Queries



Introduction

When you run a SQL query, the results may seem... a bit random. But what if you want to see students in alphabetical order, or arrange marks from highest to lowest? That’s where ORDER BY steps in — a simple yet powerful clause that helps you control the order of your output.

Basic Syntax

SELECT column1, column2, ...
FROM table_name
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...;

Sample Table – Students

We’ll work with the following data:

CREATE TABLE students (
  roll_no INT PRIMARY KEY,
  name VARCHAR(50),
  class VARCHAR(10),
  age INT,
  city VARCHAR(30)
);

INSERT INTO students VALUES
(1, 'Aarav Sharma', '10A', 15, 'Delhi'),
(2, 'Diya Iyer', '9B', 14, 'Chennai'),
(3, 'Rohit Menon', '10A', 15, 'Kochi'),
(4, 'Sneha Patil', '8C', 13, 'Pune'),
(5, 'Mehul Agarwal', '9B', 14, 'Delhi');

1. ORDER BY in Ascending Order (Default)

List students by age in ascending order:

SELECT name, age FROM students
ORDER BY age;
name          | age
---------------+-----
Sneha Patil   | 13
Diya Iyer     | 14
Mehul Agarwal | 14
Aarav Sharma  | 15
Rohit Menon   | 15

2. ORDER BY in Descending Order

Now let’s flip the order and sort by age descending:

SELECT name, age FROM students
ORDER BY age DESC;
name          | age
---------------+-----
Aarav Sharma  | 15
Rohit Menon   | 15
Diya Iyer     | 14
Mehul Agarwal | 14
Sneha Patil   | 13

3. ORDER BY Alphabetically

Sort student names in A-Z order:

SELECT name, class FROM students
ORDER BY name;

4. Sorting by Multiple Columns

Let’s say you want to sort by class first, and then by name within each class:

SELECT name, class FROM students
ORDER BY class, name;
name           | class
----------------+-------
Sneha Patil    | 8C
Diya Iyer      | 9B
Mehul Agarwal  | 9B
Aarav Sharma   | 10A
Rohit Menon    | 10A

5. Mixing ASC and DESC

You can sort one column ascending and another descending. For example, sort by class (ascending), but age (descending):

SELECT name, class, age FROM students
ORDER BY class ASC, age DESC;

6. ORDER BY with WHERE

You can combine ORDER BY with WHERE to filter and then sort:

SELECT name, age FROM students
WHERE age >= 14
ORDER BY name;

7. ORDER BY Column Number (Not Recommended)

You can technically sort using column numbers from the SELECT clause, but it’s risky and non-descriptive:

SELECT name, age FROM students
ORDER BY 2 DESC;

Better to use explicit column names for clarity.

Best Practices

  • Always specify column names in ORDER BY for readability.
  • Use multiple columns to define detailed sorting logic.
  • ASC is the default — you can skip it unless needed for clarity.

Summary

The ORDER BY clause brings structure and sense to your result sets. Whether you're ranking top students, grouping city-wise attendance, or alphabetizing names for reports — this clause transforms chaos into clarity.

What’s Next?

Now that your data is sorted and structured, it's time to explore the LIMIT clause — a way to fetch only the top results from a large dataset.

QUIZ

Question 1:What is the purpose of the ORDER BY clause in SQL?

Question 2:By default, ORDER BY sorts values in descending order.

Question 3:Which of the following SQL queries use ORDER BY correctly?

Question 4:What will the following query return?
SELECT name, marks FROM students ORDER BY marks DESC;

Question 5:You can sort results by more than one column using ORDER BY.

Question 6:Which of the following are correct ORDER BY usages?



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M