PostgreSQL Fetch Data (SELECT)
SELECT is the statement you will write most often. It reads data and returns it as a result set, which is itself a little table. SELECT never changes the stored data, so it is always safe to experiment with.
Selecting columns
List the columns you want after SELECT, separated by commas, and name the table after FROM:
SELECT first_name, last_name, city
FROM customers; first_name | last_name | city
------------+-----------+-----------
Anna | Kowalski | Warsaw
Ben | Carter | Leeds
Chloé | Dubois | Lyon
David | Kim | Seoul
Emma | Johansson | Stockholm
Farid | Haddad | Beirut
Gina | Rossi | Milan
Hugo | Silva | Porto
Isla | Murray | Glasgow
Jamal | Wright | Chicago
Keiko | Sato | Osaka
Liam | O'Brien | Dublin
Mia | Fischer | Berlin
Noah | Brown | Toronto
Olivia | Martin | Leeds
(15 rows)
The columns come back in the order you listed them. Without an ORDER BY clause, the order of the rows is not guaranteed; here they happen to come back in insertion order, but never rely on that in real applications.
Selecting every column
* means "all columns, in table order":
SELECT * FROM employees; id | name | job_title | manager_id | salary | hired
----+----------------+---------------------+------------+---------+------------
1 | Rachel Adams | Managing Director | | 9500.00 | 2018-01-08
2 | Victor Chen | Store Manager | 1 | 6200.00 | 2018-06-11
3 | Nadia Hussein | Head of Online | 1 | 6400.00 | 2019-03-04
4 | Paul Becker | Bookseller | 2 | 3100.00 | 2020-09-14
5 | Lucy Evans | Bookseller | 2 | 3000.00 | 2022-02-01
6 | Omar Farouk | Web Developer | 3 | 4800.00 | 2021-05-17
7 | Sara Lindqvist | Support Agent | 3 | 3200.00 | 2023-11-20
8 | Tim Novak | Warehouse Assistant | 2 | 2900.00 | 2024-04-02
(8 rows)
Tip:
SELECT *is great for exploring. In application code, list the columns explicitly: the query documents itself, sends less data, and doesn't change shape when someone adds a column.
Computed columns
A select list can contain expressions, not just column names. You can do arithmetic, call functions and join strings with ||. Use AS to give a computed column a readable name:
SELECT title,
price,
stock,
price * stock AS stock_value,
upper(title) AS shouting,
length(title) AS title_length
FROM books
WHERE category_id = 4; title | price | stock | stock_value | shouting | title_length
---------------------------+-------+-------+-------------+---------------------------+--------------
Learning SQL the Calm Way | 39.90 | 9 | 359.10 | LEARNING SQL THE CALM WAY | 25
Postgres in Practice | 44.00 | 4 | 176.00 | POSTGRES IN PRACTICE | 20
Rust for Curious Minds | 42.50 | 6 | 255.00 | RUST FOR CURIOUS MINDS | 22
Algorithms on a Napkin | 35.00 | 11 | 385.00 | ALGORITHMS ON A NAPKIN | 22
(4 rows)
Filtering and sorting (a preview)
Most real queries also say which rows they want (WHERE) and in which order (ORDER BY). Both get their own lessons; here is a taste:
SELECT first_name || ' ' || last_name AS customer, joined
FROM customers
WHERE country = 'UK'
ORDER BY joined DESC; customer | joined
---------------+------------
Olivia Martin | 2024-07-04
Isla Murray | 2023-09-09
Ben Carter | 2023-02-03
(3 rows)
SELECT without a table
FROM is optional in PostgreSQL. A SELECT without it evaluates expressions once, which turns Postgres into a handy calculator:
SELECT 2 ^ 10 AS power, sqrt(144) AS root, round(22 / 7.0, 3) AS almost_pi, 'tad' || 'books' AS word; power | root | almost_pi | word
-------+------+-----------+----------
1024 | 12 | 3.143 | tadbooks
(1 row)
The logical order of a query
Clauses must be written in this order, which is also roughly the order PostgreSQL evaluates them:
| Written order | What it does |
|---|---|
SELECT |
which columns or expressions to return |
FROM |
which table(s) to read |
WHERE |
keep only matching rows |
GROUP BY / HAVING |
group rows and filter groups |
ORDER BY |
sort the result |
LIMIT / OFFSET |
return only part of the result |
Test yourself
What does SELECT * FROM books; return?
- Only the first column
- All columns of all rows
- The number of rows
- The table definition
Which operator joins two strings in PostgreSQL?
- +
- &
- ||
- .
Without ORDER BY, in which order are rows returned?
- Alphabetical
- By primary key
- Insertion order, guaranteed
- Unspecified: the database may return any order