Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Sorting Data | Sorting Retrieved Data
Introduction to SQL
Section 2. Chapter 1
single

single

Sorting Data

Swipe to show menu

The data you receive can be sorted in a specific order. To do this, you first retrieve the data using the SELECT statement and then apply the ORDER BY clause. This clause takes the names of columns based on which the output will be sorted.

Here's the syntax:

SELECT columns
FROM table
ORDER BY column_name

The following example orders the result by the continent column:

123
SELECT continent FROM country ORDER BY continent;

By default, ORDER BY sorts in ascending order — numerically for numeric columns and alphabetically for string columns.

Also, the ORDER BY clause should be placed just before the LIMIT clause, if a LIMIT clause is included in the query.

Explanation: In the example, you can observe that the ORDER BY clause sorts the data based on the continent column.

Task

Swipe to start coding

Write an SQL query to select the capital column from the country table and sort the results alphabetically. Use the SELECT statement to choose the column, and the ORDER BY clause to sort the output. Only select the capital column.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 1
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

some-alt