PostgreSQL
PostgreSQL Select Data
Retrieve data from a PostgreSQL table with the SELECT statement.
products table exists. You can create it with the
Create Table guide and add rows with the
Insert Data guide.
Select data
To retrieve data from a database, use the SELECT statement. A basic
query has this structure:
SELECT column_name FROM table_name;
SELECT identifies the information to read, and FROM
identifies the table that contains it. By default, the query returns every row
that matches the statement.
Fetch specific columns
Specify the column names when you want to choose which columns to return. For example, this query returns the product name and year:
SELECT name, year
FROM products;
The result contains only the name and year columns.
You can select multiple columns by separating their names with commas. The
column names must exist in the table.
Return all columns
Use an asterisk (*) instead of column names to return every column
in the table:
SELECT * FROM products;
This query returns all columns and all rows from products. For the
table created in the previous lesson, the output includes name,
model, and year. Selecting specific columns is usually
clearer when an application needs only part of the table.
Run either query in pgAdmin 4's Query Tool and view the results in the Data Output panel.
Filter rows with WHERE
Add a WHERE clause when you want only rows that match a condition:
SELECT name, model, year
FROM products
WHERE year = 2025;
This query returns products released in 2025. Text conditions use single
quotation marks, for example WHERE name = 'Laptop'. Common
comparison operators include =, >,
<, >=, and <=.
Sort and limit results
Use ORDER BY to sort results and LIMIT to return only a specified number of rows:
SELECT name, model, year
FROM products
ORDER BY year DESC
LIMIT 5;
DESC sorts from highest to lowest. Use ASC for lowest
to highest; ascending order is the default. This example displays the five
newest products.
Query explained
SELECTchooses the columns to display.FROM productsidentifies the table to read.WHEREfilters rows before they are returned.ORDER BYcontrols the result order.LIMITrestricts the number of rows in the result.
SELECT with
WHERE, ORDER BY, and LIMIT. Later, learn
how JOIN combines data from multiple tables.
Further Reading
Continue learning with these related tutorials:
- PostgreSQL Introduction
- PostgreSQL pgAdmin 4
- PostgreSQL Create Table
- PostgreSQL Insert Data
- PostgreSQL Select Data
- PostgreSQL ADD COLUMN
- PostgreSQL UPDATE
- PostgreSQL ALTER COLUMN
- PostgreSQL DROP COLUMN
- PostgreSQL DELETE
- PostgreSQL DROP TABLE
- PostgreSQL Operators
- PostgreSQL SELECT
- PostgreSQL SELECT DISTINCT
- PostgreSQL WHERE
- PostgreSQL ORDER BY
- PostgreSQL LIMIT
- PostgreSQL MIN and MAX
- PostgreSQL COUNT
- PostgreSQL SUM
- PostgreSQL AVG
- PostgreSQL LIKE
- PostgreSQL IN
- PostgreSQL BETWEEN
- PostgreSQL AS
- PostgreSQL Joins
- PostgreSQL INNER JOIN
- PostgreSQL LEFT JOIN
- PostgreSQL RIGHT JOIN
- PostgreSQL FULL JOIN
- PostgreSQL CROSS JOIN
- PostgreSQL UNION
- PostgreSQL GROUP BY
- PostgreSQL HAVING
- PostgreSQL EXISTS
- PostgreSQL ANY
- PostgreSQL ALL
- PostgreSQL CASE
Continue learning
