Skip to main content
HomeTutorialsSQL

Using ORDER BY Keyword in SQL

In this tutorial, you will learn how to use and apply the ORDER BY keyword in SQL.
Feb 27, 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.

Topics

Other SQL Courses

Course

Introduction to SQL

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

tutorial

Introduction to the ALTER TABLE Statement in SQL

In this tutorial, you will learn how to use and apply the ALTER TABLE statement in SQL.
Sayak Paul's photo

Sayak Paul

7 min

tutorial

SQL Database Overview Tutorial

In this tutorial, you'll learn about databases in SQL.
DataCamp Team's photo

DataCamp Team

3 min

tutorial

Beginner's Guide to PostgreSQL

In this tutorial, you will learn how to write simple SQL queries in PostgreSQL.
Sayak Paul's photo

Sayak Paul

13 min

tutorial

Introduction to the Where Clause in SQL

In this tutorial, you will be introduced to filtering rows in SQL using the where clause.
Sayak Paul's photo

Sayak Paul

7 min

tutorial

Insert Into SQL Tutorial

SQL's "INSERT INTO" statement can be used to add rows of data to a table in the database.
DataCamp Team's photo

DataCamp Team

3 min

tutorial

Introduction to SQL Joins

In this tutorial, you'll learn about the mechanics of joins in SQL and its different types.
Sayak Paul's photo

Sayak Paul

9 min

See MoreSee More