Skip to main content
HomeAbout SQLLearn SQL

Using ORDER BY Keyword in SQL

In this tutorial, you will learn how to use and apply the ORDER BY keyword in SQL.
Feb 2019  · 4 min read

This tutorial assumes that you are already familiar with the basics of SQL and this includes CRUD queries (Create, Read, Update and Delete). If you want a refresher on the basics of SQL following resources might come in handy -

During your data analysis in SQL, you will need to sort your results with respect to the values of one or more columns of the table(s) you are analyzing. In SQL, this is done using the order by keyword in SQL. This tutorial is going to introduce you to the order by keyword by walking you through real examples in PostgreSQL. The second link as given above covers a decent introduction to PostgreSQL. Make sure you check it out if you are not familiar with using PostgreSQL.

Let's first create a table named countries in PostgreSQL. The table will contain the following columns and data-types -

  • country_code (character)
  • country_name (character)
  • continent (character)
  • region (character)
  • independence_year (integer)
  • local_name (character)
  • gov_form (character)
  • capital (character)

Let's quickly create a table realizing these specifications. Following SQL query would do this for you -

CREATE TABLE countries(
 country_code varchar(50) primary key,
 country_name varchar(50),
 continent varchar(50),
 region varchar(50),
 independence_year smallint,
 local_name varchar(50),
 gov_form varchar(50),
 capital varchar(50)
);

You will need a decent number of records in the table countries in order to understand the full potential of the order by keyword. You can import this .csv file into the countries table you just created.

(If you want to know how to import a .csv file into PostgreSQL you can follow this link)

After you import the .csv file into PostgreSQL, just run a select query on the countries table, and you should get something like the following -

Select query results

You now have a database ready on which you can experiment with the order by keyword. Let's start by the following query which will sort the countries with respect to the names of the countries. Country names are stored into the country_name column.

select * from countries order by country_name;

The output is -

Order by country name table

By default, order by sorts the values in ascending order. In this case, order by is sorting the country names in alphabetical order.

We can also sort the columns in descending manner by providing the DESC keyword -

select * from countries order by country_name desc;

The result returns as -

Order by by country_name desc

The countries table contain a column called independence_year. Let's see the details of the countries that got their independence in between 1850 and 1900 and let's also sort them alphabetically -

select * from countries where independence_year >= 1800 and independence_year <= 1900 order by country_name;

The results would look like -

Sort countries that got independence in between 1850 and 1900 table result

Let's now sort the details of the countries with respect to the capitals of the countries in descending order -

select * from countries order by capital desc;

And you get -

Order by with respect to the capitals in descending order table result

Look the first couple of values in the capital column. They are null values, and alphabetically they appear towards the very end.

You can also specify particular columns names as well in conjugation with order by.

select country_code, country_name, capital from countries order by capital;

The results would be - specify particular names in conjugation with order by table result

That is it for this tutorial. In this tutorial, you learned to use the order by keyword in SQL to sort column values. If you want to strengthen your SQL knowledge, you check this DataCamp course - Joining Data in SQL.

Other SQL Courses

Introduction to SQL

Beginner
2 hr
242.4K
Learn how to create and query relational databases using SQL in just two hours.
See DetailsRight Arrow
Start Course
See MoreRight Arrow
Related

What is SQL Used For? 7 Top SQL Uses

Discover the uses of SQL in industries and specific jobs. Plus, learn why the SQL language is so versatile and in demand.
Natassha Selvaraj's photo

Natassha Selvaraj

11 min

A duck codes in DuckDB

DuckDB makes SQL a first-class citizen on DataCamp Workspace

In this blog post, we list out all recent improvements that make it seamless and efficient to query data with SQL, all without leaving the tool.
Filip Schouwenaars's photo

Filip Schouwenaars

The 80 Top SQL Interview Questions and Answers for Beginners & Intermediate Practitioners

This article provides a comprehensive overview of 80 essential SQL questions and answers for job hunters, hiring managers, and recruiters, covering both general topics and technical questions.
Elena Kosourova's photo

Elena Kosourova

33 min

SQL Window Functions Cheat Sheet

With this SQL Window Functions cheat sheet, you'll have a handy reference guide to the various types of window functions in SQL.
Richie Cotton's photo

Richie Cotton

10 min

How to Use SQL in pandas Using pandasql Queries

Unleash the power of SQL within pandas and learn when and how to use SQL queries in pandas using the pandasql library for seamless integration.
Elena Kosourova's photo

Elena Kosourova

8 min

How to Practice SQL Using any Dataset with Workspace

Learn how DataCamp Workspace optimizes the experience of working with Jupyter notebooks and SQL. Discover how to effortlessly write SQL queries, connect to databases, analyze CSV files, and leverage the power of AI assistance
Richie Cotton's photo

Richie Cotton

9 min

See MoreSee More