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.

Topics

Other SQL Courses

Course

Introduction to SQL

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

Top 5 SQL Server Certifications: A Complete Guide

Unlock SQL Server certification success with our guide on paths, preparation with DataCamp, and the top certifications to enhance your career.
Matt Crabtree's photo

Matt Crabtree

8 min

PostgreSQL vs. MySQL: Choosing the Right Database for Your Project

Explore the key differences and similarities between PostgreSQL and MySQL to find the best database solution for your project's needs.
Jake Roach's photo

Jake Roach

8 min

SQL Developer Salaries: Expectations in 2024

In this article, we're going to learn what an SQL developer does, what factors impact SQL developer salaries, how the average SQL developer salary compares to the average salaries of other developer profiles, and how you can increase your salary as an SQL developer.
Elena Kosourova's photo

Elena Kosourova

7 min

Mastering SQL NOT EQUAL Operator: A Beginner's Guide

Unlock the power of SQL NOT EQUAL with our expert guide. Learn to refine data queries with practical examples and optimization tips for better analysis.
Abid Ali Awan's photo

Abid Ali Awan

5 min

SQL NOT IN Operator: A Comprehensive Guide for Beginners

Master SQL's NOT IN operator with this beginner's guide. Learn to filter data effectively, avoid common pitfalls, and explore efficient alternatives
Abid Ali Awan's photo

Abid Ali Awan

5 min

SQL CONTAINS: A Comprehensive Tutorial

Unlock the power of SQL CONTAINS for advanced text searches. Dive into logical operators, proximity searches, and wildcard uses for precise data analysis.
Abid Ali Awan's photo

Abid Ali Awan

5 min

See MoreSee More