When viewing rows for our data base, we often want to sort the data for some meaningful analysis. To do this, we can use the ORDER BY
clause. In this article, we will learn how to use ORDER BY in Postgres.
For our setup, we will use docker compose to create a Postgres database and to connect phpmyadmin. Start by copying the following into a docker compose file called docker-compose.yml
version: '3'
services:
db:
image: 'postgres:latest'
ports:
- 5432:5432
environment:
POSTGRES_USER: username
POSTGRES_PASSWORD: password
POSTGRES_DB: default_database
volumes:
- psqldata:/var/lib/postgresql
phpmyadmin:
image: phpmyadmin/phpmyadmin
links:
- db
environment:
PMA_HOST: db
PMA_PORT: 3306
PMA_ARBITRARY: 1
restart: always
ports:
- 8081:80
volumes:
psqldata:
We can run this file, we can use docker-compose up
. One this is done, open up phpmyadmin by going to http://localhost:8081.
You can then login by leaving the host empty and using the following credentials.
POSTGRES_USER: username
POSTGRES_PASSWORD: password
In this article, we will need some data to work with. If you don't understand these commands, don't worry, we will cover them in later articles.
We will be using the sample db provided here: https://dev.mysql.com/doc/sakila/en/. However, we will only enter what we need rather than import the whole db.
Next, let's create an actor
table.
CREATE TABLE actor (
actor_id smallint,
first_name VARCHAR(45) NOT NULL,
last_name VARCHAR(45) NOT NULL,
last_update TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (actor_id)
) ;
And finally, let's enter a few rows.
INSERT INTO actor VALUES
(1,'PENELOPE','GUINESS','2006-02-15 04:34:33'),
(2,'NICK','WAHLBERG','2006-02-15 04:34:33'),
(3,'ED','CHASE','2006-02-15 04:34:33'),
(4,'JENNIFER','DAVIS','2006-02-15 04:34:33'),
(5,'JOHNNY','LOLLOBRIGIDA','2006-02-15 04:34:33'),
(6,'BETTE','NICHOLSON','2006-02-15 04:34:33'),
(7,'GRACE','MOSTEL','2006-02-15 04:34:33'),
(8,'MATTHEW','JOHANSSON','2006-02-15 04:34:33')
The basic sql structure as the following pattern.
SELECT * from actor order by actor_id
actor_id | first_name | last_name | last_update |
---|---|---|---|
2 | NICK | WAHLBERG | 2006-02-15 04:34:33 |
3 | ED | CHASE | 2006-02-15 04:34:33 |
6 | BETTE | NICHOLSON | 2006-02-15 04:34:33 |
7 | GRACE | MOSTEL | 2006-02-15 04:34:33 |
8 | MATTHEW | JOHANSSON | 2006-02-15 04:34:33 |
Running this command will return all actors, sorted by the id. Since the id column is a number, we will get a numeric sort.
For a contrasting example, we can sort by name, which will give us an alpha numeric sort.
SELECT * from actor order by first_name
actor_id | first_name | last_name | last_update |
---|---|---|---|
6 | BETTE | NICHOLSON | 2006-02-15 04:34:33 |
3 | ED | CHASE | 2006-02-15 04:34:33 |
7 | GRACE | MOSTEL | 2006-02-15 04:34:33 |
8 | MATTHEW | JOHANSSON | 2006-02-15 04:34:33 |
2 | NICK | WAHLBERG | 2006-02-15 04:34:33 |
We are not limited to single column sorts. We can use multiple sorts to create a then by
sort. For example, the following will sort by first_name then by last_name.
SELECT * FROM actor ORDER BY first_name, last_name
actor_id | first_name | last_name | last_update |
---|---|---|---|
6 | BETTE | NICHOLSON | 2006-02-15 04:34:33 |
3 | ED | CHASE | 2006-02-15 04:34:33 |
7 | GRACE | MOSTEL | 2006-02-15 04:34:33 |
8 | MATTHEW | JOHANSSON | 2006-02-15 04:34:33 |
2 | NICK | WAHLBERG | 2006-02-15 04:34:33 |
By default, SQL will wort our data by Ascending order. We can change this using the ASC
and DESC
keywords.
For example, we can sort by first_name descending using the following.
SELECT *
FROM actor
ORDER BY first_name DESC
actor_id | first_name | last_name | last_update |
---|---|---|---|
2 | NICK | WAHLBERG | 2006-02-15 04:34:33 |
8 | MATTHEW | JOHANSSON | 2006-02-15 04:34:33 |
7 | GRACE | MOSTEL | 2006-02-15 04:34:33 |
3 | ED | CHASE | 2006-02-15 04:34:33 |
6 | BETTE | NICHOLSON | 2006-02-15 04:34:33 |
We can also combine the keywords with multiple sort.
SELECT *
FROM actor
ORDER BY first_name DESC, last_name ASC
actor_id | first_name | last_name | last_update |
---|---|---|---|
2 | NICK | WAHLBERG | 2006-02-15 04:34:33 |
8 | MATTHEW | JOHANSSON | 2006-02-15 04:34:33 |
7 | GRACE | MOSTEL | 2006-02-15 04:34:33 |
3 | ED | CHASE | 2006-02-15 04:34:33 |
6 | BETTE | NICHOLSON | 2006-02-15 04:34:33 |