All posts
PostgreSQLAugust 29, 20267 min read

How to Select Data in PostgreSQL: A Beginner's Guide

Learn how to select data in PostgreSQL with the SELECT statement. A beginner-friendly guide to WHERE, ORDER BY, LIMIT, and COUNT with copy-paste examples.

F
Fepiq Team
Fepiq

If you searched for "how to select data in PostgreSQL," here is the short answer: you use a SELECT statement. It looks like SELECT column_name FROM table_name;, and it tells PostgreSQL which columns you want and which table to get them from. In this guide, you will learn exactly how SELECT works, one small step at a time, with copy-pasteable examples you can run yourself.

This is a true beginner guide. We assume you have never written SQL before. If you have not created a table yet, it may help to read our guide on creating a table in PostgreSQL and inserting data first, but you can also follow along here using the examples as-is.

What Is a SELECT Query?

A database table stores data in rows and columns, similar to a spreadsheet. A SELECT query is a command you type into PostgreSQL that asks it to read some of that data and show it back to you. SELECT does not change anything in the table. It only reads and displays data, which makes it a very safe command to practice with.

Think of it like asking a librarian a question: "Show me the titles and authors of every book published after 2020." SELECT is how you ask that question in a language PostgreSQL understands.

The Basic SELECT Syntax

Imagine a table named customers with columns for id, first_name, email, country, and signup_date. The simplest possible query looks like this:

sql
SELECT * FROM customers;

Here is what each part means. SELECT is the keyword that starts every read query. The asterisk (*) is a shortcut that means "all columns." FROM customers tells PostgreSQL which table to read from. The semicolon (;) at the end tells PostgreSQL the command is finished. Run this, and PostgreSQL returns every row and every column in the customers table.

Selecting Only the Columns You Need

Using * is fine for exploring a small table, but in real projects you usually only want specific columns. You can list them by name, separated by commas:

sql
SELECT first_name, email FROM customers;

This query returns only the first_name and email columns for every row, instead of every column. Being specific like this makes your results easier to read and, on large tables, faster to run because PostgreSQL has less data to send back.

Filtering Rows with WHERE

So far, every example returns all rows in the table. Usually you only want rows that match a certain condition. That is what the WHERE clause is for:

sql
SELECT first_name, email
FROM customers
WHERE country = 'Canada';

WHERE country = 'Canada' tells PostgreSQL to only include rows where the country column is exactly 'Canada'. Notice the text value is wrapped in single quotes; PostgreSQL requires that for text, but not for numbers. You can also use other comparisons such as > (greater than), < (less than), != (not equal to), and AND / OR to combine multiple conditions.

Sorting Results with ORDER BY

By default, PostgreSQL does not guarantee any particular order for the rows it returns. To control the order, add ORDER BY:

sql
SELECT first_name, email
FROM customers
ORDER BY first_name ASC;

ORDER BY first_name ASC sorts the results alphabetically by first name, from A to Z. ASC means "ascending" and is actually the default, so you could leave it out. To sort from Z to A, or from newest to oldest for a date column, use DESC ("descending") instead.

Limiting How Many Rows You Get Back

Some tables have millions of rows. Often you just want to peek at a few of them, like the most recent sign-ups. LIMIT does that:

sql
SELECT first_name, email, signup_date
FROM customers
ORDER BY signup_date DESC
LIMIT 5;

This query sorts customers by signup_date with the newest first (DESC), then LIMIT 5 tells PostgreSQL to only return the first 5 rows of that sorted result. This pattern, ORDER BY plus LIMIT, is one of the most common ways beginners and experts alike explore a table quickly.

Counting Rows with COUNT()

Sometimes you do not need the actual data, just a number, such as "how many customers do we have from Canada?" For that, use the COUNT() function:

sql
SELECT COUNT(*) FROM customers WHERE country = 'Canada';

COUNT(*) counts the number of rows that match the query, instead of returning the rows themselves. Combined with WHERE, this is a fast way to answer simple questions about your data without scrolling through a long list.

Putting It All Together

You can combine everything you have learned into one query. Here is an example that finds the 3 most recently signed-up customers from Canada:

sql
SELECT first_name, email, signup_date
FROM customers
WHERE country = 'Canada'
ORDER BY signup_date DESC
LIMIT 3;

Reading it top to bottom: pick the first_name, email, and signup_date columns, from the customers table, but only rows where country is Canada, sorted with the newest signup first, and only show the top 3 results. This order (SELECT, FROM, WHERE, ORDER BY, LIMIT) is exactly the order PostgreSQL expects these clauses to appear in.

ClausePurposeExample
SELECTChoose which columns to returnSELECT first_name, email
FROMChoose which table to read fromFROM customers
WHEREFilter which rows to includeWHERE country = 'Canada'
ORDER BYSort the resultsORDER BY signup_date DESC
LIMITCap the number of rows returnedLIMIT 3

Common Mistakes Beginners Make

  • Forgetting the semicolon (;) at the end of a query, which can make psql wait for more input.
  • Using double quotes instead of single quotes around text values, like "Canada" instead of 'Canada'. In PostgreSQL, single quotes are for text values and double quotes are for column or table names.
  • Forgetting that text comparisons are case-sensitive by default, so 'canada' will not match 'Canada'.
  • Writing clauses in the wrong order, such as putting ORDER BY before WHERE. Remember: SELECT, FROM, WHERE, ORDER BY, LIMIT.
  • Running SELECT * on a huge table just to check one column, which is slower than naming the column you actually need.

Where to Practice

The best way to learn SELECT is to run these queries yourself against real data. If you have not created a table yet, our guide on creating a table in PostgreSQL walks you through it, and our guide on inserting data shows you how to add rows you can then practice selecting.

Not sure how to get data into your table in the first place? Learn how to insert rows in PostgreSQL first.

Read the Insert Data Guide

Frequently asked questions

What does SELECT do in PostgreSQL?+

SELECT is a SQL command that reads and returns data from one or more tables without changing anything in the database. You use it to choose which columns and rows you want to see, and it is the most commonly used SQL command overall.

How do I select specific columns instead of all columns?+

List the column names you want after SELECT, separated by commas, instead of using the asterisk (*). For example, SELECT first_name, email FROM customers; returns only those two columns instead of every column in the table.

How do I filter results in a SELECT query?+

Add a WHERE clause after the table name, followed by a condition, such as WHERE country = 'Canada'. Only rows that match the condition are included in the results. You can combine multiple conditions with AND and OR.

What is the difference between WHERE and ORDER BY?+

WHERE decides which rows are included in the results based on a condition, while ORDER BY decides what order the included rows are displayed in. You typically write WHERE before ORDER BY in the same query, and you can use both together.

Can I limit how many rows a SELECT query returns?+

Yes. Add LIMIT followed by a number at the end of your query, such as LIMIT 5, and PostgreSQL will only return that many rows. This is useful for previewing large tables without loading every row at once.

SELECT is the foundation of nearly everything else you will do in SQL, from simple reports to complex joins across multiple tables. Once you are comfortable with SELECT, WHERE, ORDER BY, and LIMIT, you already have the tools to answer most everyday questions about your data.

Building a product that needs a real database behind it? We design and build custom web apps on PostgreSQL from the ground up.

Talk to Fepiq

Get new posts in your inbox

Occasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.

Keep reading

Related posts

All posts

Let's build something

Ready to ship your next product with Fepiq?

Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.