PostgreSQL SELECT

How the PostgreSQL SELECT statement works: querying columns, expressions, aliases, computed columns, and eliminating duplicates with DISTINCT.

10 min read · Last updated: March 2026 · Back to overview

Quick Answer

SELECT retrieves rows from one or more tables or views. Specify the columns you want after SELECT, the source after FROM, and optionally filter with WHERE, sort with ORDER BY, and limit rows with LIMIT.

Spin up a Postgres database in 20 seconds with Vela.

Try Vela Sandbox

Introduction to PostgreSQL SELECT

The SELECT statement is the most frequently used SQL command. It retrieves rows from tables, views, or the result of expressions.

Basic syntax:

SELECT select_list FROM table_name;

Selecting specific columns

List the columns you want after SELECT, separated by commas. Using explicit column names instead of * is a best practice — it makes queries self-documenting and avoids issues when table structure changes.

SELECT first_name, last_name, email FROM customer;

Selecting all columns

SELECT * FROM customer;

Use * for ad-hoc queries and exploration. Avoid it in application code — schema changes can silently break queries or return unexpected data.

Column aliases with AS

Rename columns in the result set using AS. The alias is available in ORDER BY but not in WHERE (because WHERE is evaluated before SELECT).

SELECT
  first_name || ' ' || last_name AS full_name,
  email
FROM customer
ORDER BY full_name;

Expressions and computed columns

SELECT
  product_name,
  price,
  quantity,
  price * quantity AS total_value
FROM order_items
ORDER BY total_value DESC;

SELECT with WHERE, ORDER BY, and LIMIT

SELECT
  order_id,
  customer_id,
  total_amount,
  placed_at
FROM orders
WHERE placed_at >= now() - interval '30 days'
  AND total_amount > 50
ORDER BY placed_at DESC
LIMIT 20;

DISTINCT: removing duplicate rows

-- Unique cities customers are from
SELECT DISTINCT city FROM customer ORDER BY city;

PostgreSQL also supports DISTINCT ON — a powerful extension that returns one row per group based on a specified column:

-- Most recent order per customer
SELECT DISTINCT ON (customer_id)
  customer_id, order_id, placed_at
FROM orders
ORDER BY customer_id, placed_at DESC;

Production tips

  • Always list columns explicitly in application queries — SELECT * will break silently if columns are added or reordered.
  • Use column aliases to match your application's expected field names, reducing transformation code in the application layer.
  • Qualify column names with table aliases (e.g. o.order_id) in multi-table queries to avoid ambiguity.
  • Use EXPLAIN to check how PostgreSQL plans your query before deploying to production.

Reference: PostgreSQL documentation — SELECT.

Continue in Querying Data: Column Aliases.

Related in this section: Column Aliases · ORDER BY · SELECT DISTINCT

Frequently Asked Questions

What is the basic syntax of a PostgreSQL SELECT statement?

The minimum SELECT statement is: SELECT column1, column2 FROM table_name; You can also use SELECT * to retrieve all columns, though listing columns explicitly is better for production code. Add WHERE to filter rows, ORDER BY to sort, LIMIT to cap the result count, and JOIN to combine multiple tables.

What is the order of execution in a PostgreSQL SELECT query?

PostgreSQL evaluates SELECT clauses in this logical order: FROM (identify tables/joins) → WHERE (filter rows) → GROUP BY (group rows) → HAVING (filter groups) → SELECT (compute output columns) → DISTINCT (remove duplicates) → ORDER BY (sort) → LIMIT/OFFSET (paginate). This means you cannot use a SELECT alias in a WHERE clause, but you can use it in ORDER BY.

How do I select distinct rows in PostgreSQL?

Use SELECT DISTINCT to remove duplicate rows from the result set. PostgreSQL compares all selected columns to determine duplicates. For finer control — keeping one row per group based on one column while sorting by another — use SELECT DISTINCT ON (column). DISTINCT ON is a PostgreSQL-specific extension not available in all databases.

Can I use expressions and functions in SELECT?

Yes. The SELECT list can contain column references, arithmetic expressions (price * quantity AS total), string functions (first_name || ' ' || last_name AS full_name), date functions (date_trunc('month', created_at)), and aggregate functions (COUNT(*), SUM(amount)). Any valid PostgreSQL expression is allowed.