Skip to main content

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
173.2K
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

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

COALESCE SQL Function

COALESCE() is one of the handiest functions in SQL. Read this tutorial to learn how to master it.
Travis Tang 's photo

Travis Tang

FORMAT() SQL FUNCTION

FORMAT() is one of the most commonly used functions in SQL. Learn its main applications in this tutorial.
Travis Tang 's photo

Travis Tang

INSERT INTO SQL FUNCTION

INSERT INTO lets you add data to your tables. Learn how to use it in this tutorial.
Travis Tang 's photo

Travis Tang

COUNT() SQL FUNCTION

COUNT() lets you count the number of rows that match certain conditions. Learn how to use it in this tutorial.
Travis Tang 's photo

Travis Tang

See MoreSee More