Search⌘ K

Retrieving Data from Tables

Explore how to retrieve data from PostgreSQL tables using SQL SELECT queries. Learn to extract specific columns, filter rows with WHERE, return unique values with DISTINCT, sort results with ORDER BY, and group data using GROUP BY and HAVING clauses. This lesson equips you to perform fundamental data retrieval and aggregation operations essential for database querying.

The SELECT query

The basic unit of data storage in PostgreSQL is tables. A table consists of a collection of rows, and each row has a set of columns containing that row’s data. To get data out of a table, we use the SELECT statement.

The most basic form of the SELECT statement looks like this:

SELECT
*
FROM
<table_name>;
The most basic SELECT statement

It will select all of the columns in the table_name table. The wildcard character * represents all the columns.

We can specify the columns to be selected:

SELECT
<field_1>,
<field_2>,
...
<field_n>
FROM
<table_name>
WHERE
<condition>
The syntax of the SELECT statement

Here, <field_1>, <field_2>, ... represents the names of the columns we want to select and <table_name> is the name of the table from which we want to fetch data.

We can use the SELECT statement as below to retrieve data from a table, Employee, containing four columns, ...