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

Certification available

Introduction to SQL

BeginnerSkill Level
2 hr
466.2K
Learn how to create and query relational databases using SQL in just two hours.
See DetailsRight Arrow
Start Course
Certification available

Intermediate SQL

BeginnerSkill Level
4 hr
151.8K
Accompanied at every step with hands-on practice queries, this course teaches you everything you need to know to analyze data using your own SQL code today!
See MoreRight Arrow
Related

10 Portfolio-Ready SQL Projects for All Levels

Select your first—or next—SQL project to practice your current SQL skills, develop new ones, and create an outstanding professional portfolio.
Elena Kosourova's photo

Elena Kosourova

11 min

SQL vs NoSQL Databases: Key Differences and Practical Insights

Discover how to decide between SQL and NoSQL databases in data science and application development. Learn their strengths, use cases, and industry applications.

Kevin Babitz

15 min

MySQL Basics Cheat Sheet

With this MySQL basics cheat sheet, you'll have a handy reference guide to basic querying tables, filtering data, and aggregating data
Richie Cotton's photo

Richie Cotton

6 min

PostgreSQL Basics Cheat Sheet

With this PostgreSQL cheat sheet, you'll have a handy reference guide to basic querying tables, filtering data, and aggregating data
Richie Cotton's photo

Richie Cotton

6 min

MySQL Tutorial: A Comprehensive Guide for Beginners

Discover what MySQL is and how to get started in one of the most popular database management systems.
Javier Canales Luna's photo

Javier Canales Luna

15 min

SQL Server Tutorial: Unlock the Power of Data Management

Explore data management with our SQL Server Tutorial. From basics to advanced usage, enhance your skills and navigate SQL Server with confidence.

Kevin Babitz

13 min

See MoreSee More