Fetching Data in PostgreSQL with the SELECT Command

🌟 Hello!! Today, we’re going to focus on the SELECT command in PostgreSQL, the SQL command that lets you retrieve data from your tables. By the end of this tutorial, you’ll know the basics and some advanced techniques for data retrieval. Let’s get started!

What is SELECT?

In PostgreSQL, the SELECT command is used to fetch one or more rows from a table. This is the SQL command you’ll likely use most often, as querying data is the essence of database interaction.

Basic Syntax

The most straightforward use of SELECT looks something like this:

SELECT column1, column2 FROM table_name;

If you want to select all columns, you can use the wildcard *:

SELECT * FROM table_name;

Example: Querying Data from the tv_series Table

Recall the tv_series table we’ve been working with, filled with fictional data about various TV shows. Let’s explore how to query it.

Selecting Specific Columns

To fetch only the names and genres of TV series, you’d do:

SELECT name, genre FROM tv_series;
name genre
Breaking Bad Crime
Stranger Things Science Fiction
The Crown Historical Drama
Game of Thrones Fantasy
Friends Comedy
The Mandalorian Science Fiction
The Office Comedy

Selecting All Columns

If you want all the data from the table, go for:

SELECT * FROM tv_series;
id name genre seasons is_running
1 Breaking Bad Crime 5 false
2 Stranger Things Science Fiction 4 true
3 The Crown Historical Drama 4 true
4 Game of Thrones Fantasy 8 false
5 Friends Comedy 10 false
6 The Mandalorian Science Fiction 2 true
7 The Office Comedy 9 false

Using WHERE to Filter Data

Want to find all TV series that are still running? You’ll use the WHERE clause:

SELECT name, genre FROM tv_series WHERE is_running = true;
name genre
Stranger Things Science Fiction
The Crown Historical Drama
The Mandalorian Science Fiction

Common Flags and Usage Patterns


You can sort the results using ORDER BY followed by the column name:

SELECT * FROM tv_series ORDER BY name ASC;


Want to limit the number of results? Use the LIMIT keyword:

SELECT * FROM tv_series LIMIT 3;

Aggregate Functions

You can perform operations like counting, averaging, or summing up the data. For example, to count all TV series:

SELECT COUNT(*) FROM tv_series;

Common Errors and Solutions

Error Message:

ERROR: column "xyz" does not exist


You’ve requested a column that doesn’t exist in the table.


  • Double-check your column names for typos.
  • Make sure you’re querying the correct table.

Error Message:

ERROR: syntax error at or near "FROM"


There’s likely a syntax error in your query, probably before the FROM keyword.


  • Review your SQL statement for any syntax mistakes or misplaced keywords.

Error Message:

ERROR: permission denied for table tv_series


Your user role doesn’t have the necessary permissions to query the table.


  • Check your role’s permissions.
  • If you’re the database administrator, you can grant the necessary permissions to your role.

And there you have it! You’re now well-equipped to use the SELECT command in PostgreSQL to query data, filter it, sort it, and much more. Happy data fetching! 🎣

Leave a Reply