SQL ORDER BY Clause — Sorting Results in SQL Queries

Sort Results

A school prize list sorts marks from high to low, but two students have the same mark. Their order needs a clear rule.

ORDER BY defines the ordering of a query result. Multiple sort expressions provide rules for breaking ties.

  1. Explain and apply

    Sort a class report and use a second ordering field to resolve ties.

  2. Work it out

    Sort the three students by marks descending and then roll number ascending.

  3. Avoid mistakes

    Use a stable unique tie-breaker when LIMIT or pagination depends on reproducible results.

Sort Results: the request and its result

  1. School need

    A school prize list sorts marks from high to low, but two students have the same mark. Their order needs a clear rule.

  2. Database operation

    ORDER BY defines the ordering of a query result. Multiple sort expressions provide rules for breaking ties.

  3. Check the outcome

    The first sort expression ties Aarav and Sneha at 85. Their roll numbers 1 and 3 settle that tie before the 70-mark row.

A conceptual model. The query examples below show the actual syntax, input data and results.

Understand what the database does

DESC puts larger non-null numeric values first; ASC reverses that direction. A later ordering expression is used when earlier values tie. Sorting the result does not permanently rearrange a table into a guaranteed order for future queries.

Related concepts: Limit Result Rows and Window Functions.

Basic Syntax

SQL · portable concepts; preserved examples identify their engine

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

Sample Table – Students

SQL · portable concepts; preserved examples identify their engine

Let’s use a simple students table as our base. We shall use this table in the following examples.

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');
students
roll_nonameclassagecity
1Aarav Sharma10A15Delhi
2Diya Iyer9B14Chennai
3Rohit Menon10A15Kochi
4Sneha Patil8C13Pune
5Mehul Agarwal9B14Delhi

1. ORDER BY in Ascending Order (Default)

Input definitions and data for this example. The shown statements establish the base fixture. Where this section changes an existing object, its required object definition is included below; use the stated before/after table to follow a sequence of changes.

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');

SQL · portable concepts; preserved examples identify their engine

Use case: A school administrator wants to view the list of all students sorted by their age to identify the youngest and oldest students.

students
roll_nonameclassagecity
1Aarav Sharma10A15Delhi
2Diya Iyer9B14Chennai
3Rohit Menon10A15Kochi
4Sneha Patil8C13Pune
5Mehul Agarwal9B14Delhi

SQL query description: SQL query to select the name and age columns from the students table and sort the result in ascending order based on the age column.

SELECT name, age FROM students
ORDER BY age;

Result description: Result contains the names and ages of all students, sorted from the youngest to the oldest.

Query Result
name age
Sneha Patil13
Diya Iyer14
Mehul Agarwal14
Aarav Sharma15
Rohit Menon15
FocusSelected or comparedAdded or changedRemoved or failing

The surrounding query explains the specific highlighted values. Highlights annotate this example; they do not change the stored table.

2. ORDER BY in Descending Order

Input definitions and data for this example. The shown statements establish the base fixture. Where this section changes an existing object, its required object definition is included below; use the stated before/after table to follow a sequence of changes.

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');

SQL · portable concepts; preserved examples identify their engine

Use case: A school administrator wants to view a list of all students sorted by age in descending order to identify the oldest students first.

students
roll_nonameclassagecity
1Aarav Sharma10A15Delhi
2Diya Iyer9B14Chennai
3Rohit Menon10A15Kochi
4Sneha Patil8C13Pune
5Mehul Agarwal9B14Delhi

SQL query description: SQL query to select the name and age columns from the students table and sort the result by age in descending order.

SELECT name, age FROM students
ORDER BY age DESC;

Result description: Result contains the names and ages of all students, arranged from the oldest to the youngest.

Query Result
name age
Aarav Sharma15
Rohit Menon15
Diya Iyer14
Mehul Agarwal14
Sneha Patil13
FocusSelected or comparedAdded or changedRemoved or failing

The surrounding query explains the specific highlighted values. Highlights annotate this example; they do not change the stored table.

3. ORDER BY Alphabetically

Input definitions and data for this example. The shown statements establish the base fixture. Where this section changes an existing object, its required object definition is included below; use the stated before/after table to follow a sequence of changes.

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');

SQL · portable concepts; preserved examples identify their engine

Use case: A school administrator wants to generate an alphabetically sorted list of all students to create an organized student directory.

students
roll_nonameclassagecity
1Aarav Sharma10A15Delhi
2Diya Iyer9B14Chennai
3Rohit Menon10A15Kochi
4Sneha Patil8C13Pune
5Mehul Agarwal9B14Delhi

SQL query description: SQL query to select the name and class columns from the students table and sort the results by the name column in ascending order.

SELECT name, class FROM students
ORDER BY name;

Result description: Result contains the names and class details of all students, sorted alphabetically by name.

Query Result
name class
Aarav Sharma10A
Diya Iyer9B
Mehul Agarwal9B
Rohit Menon10A
Sneha Patil8C
FocusSelected or comparedAdded or changedRemoved or failing

The surrounding query explains the specific highlighted values. Highlights annotate this example; they do not change the stored table.

4. Sorting by Multiple Columns

Input definitions and data for this example. The shown statements establish the base fixture. Where this section changes an existing object, its required object definition is included below; use the stated before/after table to follow a sequence of changes.

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');

SQL · portable concepts; preserved examples identify their engine

Use case: A school administrator wants to generate a sorted list of all students, grouped by class and alphabetically ordered by name within each class, for reporting or display purposes.

students
roll_nonameclassagecity
1Aarav Sharma10A15Delhi
2Diya Iyer9B14Chennai
3Rohit Menon10A15Kochi
4Sneha Patil8C13Pune
5Mehul Agarwal9B14Delhi

SQL query description: SQL query to select the name and class columns from the students table and sort the results first by class and then by name in ascending order.

SELECT name, class FROM students
ORDER BY class, name;

Result description: Result contains the names and class details of all students, sorted by class and then by student name within each class.

Query Result
name class
Aarav Sharma10A
Rohit Menon10A
Sneha Patil8C
Diya Iyer9B
Mehul Agarwal9B
FocusSelected or comparedAdded or changedRemoved or failing

The surrounding query explains the specific highlighted values. Highlights annotate this example; they do not change the stored table.

5. Mixing ASC and DESC

Input definitions and data for this example. The shown statements establish the base fixture. Where this section changes an existing object, its required object definition is included below; use the stated before/after table to follow a sequence of changes.

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');

SQL · portable concepts; preserved examples identify their engine

Use case: A school administrator wants to generate a sorted list of all students, grouped by class in ascending order and within each class sorted by age in descending order, to identify the oldest students in each class.

students
roll_nonameclassagecity
1Aarav Sharma10A15Delhi
2Diya Iyer9B14Chennai
3Rohit Menon10A15Kochi
4Sneha Patil8C13Pune
5Mehul Agarwal9B14Delhi

SQL query description: SQL query to select the name, class, and age columns from the students table, and sort the results first by class in ascending order and then by age in descending order.

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

Result description: Result contains the names, classes, and ages of all students, ordered by class (A–Z) and within each class, by age from oldest to youngest.

Query Result
name class age
Aarav Sharma10A15
Rohit Menon10A15
Sneha Patil8C13
Diya Iyer9B14
Mehul Agarwal9B14
FocusSelected or comparedAdded or changedRemoved or failing

The surrounding query explains the specific highlighted values. Highlights annotate this example; they do not change the stored table.

6. ORDER BY with WHERE

Input definitions and data for this example. The shown statements establish the base fixture. Where this section changes an existing object, its required object definition is included below; use the stated before/after table to follow a sequence of changes.

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');

SQL · portable concepts; preserved examples identify their engine

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

Use case: A school administrator wants to generate a list of students who are 14 years old or older, sorted alphabetically by name, to plan age-specific programs or activities.

students
roll_nonameclassagecity
1Aarav Sharma10A15Delhi
2Diya Iyer9B14Chennai
3Rohit Menon10A15Kochi
4Sneha Patil8C13Pune
5Mehul Agarwal9B14Delhi

SQL query description: SQL query to select the name and age columns from the students table where the age is greater than or equal to 14, and then sort the results in ascending order by name.

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

Result description: Result contains the names and ages of students who are at least 14 years old, displayed in alphabetical order by name.

Query Result
name age
Aarav Sharma15
Diya Iyer14
Mehul Agarwal14
Rohit Menon15
FocusSelected or comparedAdded or changedRemoved or failing

The surrounding query explains the specific highlighted values. Highlights annotate this example; they do not change the stored table.

Best Practices

Query discussed in this section:

SELECT name, age FROM students
ORDER BY 2 DESC;
  • 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.

Important differences and mistakes

Use a stable unique tie-breaker when LIMIT or pagination depends on reproducible results. NULL ordering and text collation vary by engine. Do not assume that an index, insertion order or a previously sorted query guarantees this query’s order.

Try it with the school data

Sort the three students by marks descending and then roll number ascending.

This practice uses a separate, fresh fixture named practice_students. It does not continue the changing table state of the earlier examples.

Practice input: practice_students
roll_nonameclassmarkscity
1Aarav10A85Pune
2Diya9B70Chennai
3Sneha10A85Pune

SQL · create only these practice tables in a disposable database:

CREATE TABLE practice_students (roll_no INTEGER PRIMARY KEY, name VARCHAR(50), class VARCHAR(10), marks INTEGER, city VARCHAR(30));
INSERT INTO practice_students VALUES
(1, 'Aarav', '10A', 85, 'Pune'),
(2, 'Diya', '9B', 70, 'Chennai'),
(3, 'Sneha', '10A', 85, 'Pune');

Worked answer and reasoning

This practice uses a separate, fresh fixture named practice_students. It does not continue the changing table state of the earlier examples.

Practice input: practice_students
roll_nonameclassmarkscity
1Aarav10A85Pune
2Diya9B70Chennai
3Sneha10A85Pune

SQL · create only these practice tables in a disposable database:

CREATE TABLE practice_students (roll_no INTEGER PRIMARY KEY, name VARCHAR(50), class VARCHAR(10), marks INTEGER, city VARCHAR(30));
INSERT INTO practice_students VALUES
(1, 'Aarav', '10A', 85, 'Pune'),
(2, 'Diya', '9B', 70, 'Chennai'),
(3, 'Sneha', '10A', 85, 'Pune');

SQL · this practice variation runs with SQLite 3; the preserved examples retain their stated dialect

SELECT name, marks FROM practice_students ORDER BY marks DESC, roll_no ASC;
Expected query result
namemarks
Aarav85
Sneha85
Diya70

The first sort expression ties Aarav and Sneha at 85. Their roll numbers 1 and 3 settle that tie before the 70-mark row.

Check your understanding

Why add roll_no after ORDER BY marks DESC?
Choose one answer. You can try again.

Answer and explanation

To define the order between students whose marks tie.

The first sort expression ties Aarav and Sneha at 85. Their roll numbers 1 and 3 settle that tie before the 70-mark row.

Use the idea independently

ORDER BY defines the ordering of a query result. Multiple sort expressions provide rules for breaking ties.

The first sort expression ties Aarav and Sneha at 85. Their roll numbers 1 and 3 settle that tie before the 70-mark row.

Continue your understanding with Limit Result Rows and Window Functions.

Source for the engine-specific behavior: MySQL 8.4 reference documentation.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: SQL GROUP BY Clause — Summarizing Data →