PostgreSQL

PostgreSQL Select Data

Retrieve data from a PostgreSQL table with the SELECT statement.

Before you begin: Connect to the target database in pgAdmin 4 and make sure the 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

  • SELECT chooses the columns to display.
  • FROM products identifies the table to read.
  • WHERE filters rows before they are returned.
  • ORDER BY controls the result order.
  • LIMIT restricts the number of rows in the result.
Next step: Practice combining SELECT with WHERE, ORDER BY, and LIMIT. Later, learn how JOIN combines data from multiple tables.

Further Reading

Continue learning with these related tutorials:

Continue learning