The SELECT statement in PostgreSQL is one of the most powerful and flexible query statements available. It allows you to retrieve data from one or more tables in various ways by using different clauses.
Here, we will explore the basic usage of the PostgreSQL SELECT statement to query data from a database table.
Clauses used with SELECT Statement
The SELECT statement can be enhanced with several clauses to increase its flexibility and functionality:
- DISTINCT operator: It is used to select distinct rows from a table.
- ORDER BY clause: It is used to sort table rows.
- WHERE clause: It is used to filter rows from a table. >LIMIT clause: It is used to select a subset of rows from the table.
- FETCH clause: It is also used to select a subset of rows from the table.
- GROUP BY clause: It is used to group different rows into a single group.
- HAVING clause: It is used to filter rows from a table with specified attributes or features.
- FROM clause: It is used to specify a column in a table.
- Joins: It is used to join two or more tables together using joins such as ‘INNER JOIN’, ‘LEFT JOIN‘, ‘FULL OUTER JOIN‘, and ‘CROSS JOIN’ clauses.
- Set operators: These operators such as UNION, INTERSECT, and EXCEPT are used to manipulate the different sets of data.
Basic Syntax of SELECT Statement
For the sake of simplicity we will be looking into the use of SELECT statement with FROM clause in our sample DVD rental database. The syntax for using the SELECT statement is as follows:
SELECT select_list
FROM table_name;
Now, let’s evaluate the SELECT statement in more detail,
Firstly, we need to specify a particular column or a list of columns from where the data is to be retrieved. Commas are used to separate multiple columns, if a list of column is selected as below:
SELECT select_list1, select_list2, select_list3 FROM table_name;
For selecting all columns from the database, use
asterisk(‘*’)
as below:
SELECT * FROM table_name;
Secondly, we need to specify the name of the table from which data is to be retrieved, after the FROM keyword.
PostgreSQL SELECT Statement Examples
Now let us look into a few examples of using PostgreSQL SELECT statement in our sample database. For the sake of this article we will be using the sample DVD rental database, which is explained here and can be downloaded by clicking on this link.
Example 1: Querying Data from One Column
Using SELECT statement to query data from one column.
Query:
SELECT first_name
FROM customer;
Output:

Explanation: The query returns a list of first names from the ‘customer' table.
Example 2: Querying Data from Multiple Columns
Using SELECT statement to query data from multiple columns.
Query:
SELECT first_name, email
FROM customer;
Output:

Explanation: The query returns a list of first names and email addresses from the ‘customer' table.
Example 3: Querying All Columns of a Table
Using SELECT statement to query data in all columns of a table.
Query:
SELECT * FROM customer;
Output:

Explanation: The query returns all columns from the ‘customer' table.
Example 4: Using SELECT Statement with Expressions
Using SELECT statement with expressions.
Query:
SELECT first_name || ' ' || last_name AS full_name, email
FROM customer;
Output:

Explanation: The query returns the full names and email addresses of customers by concatenating the ‘first_name' and ‘last_name' columns.
Advanced Usage of SELECT Statement
1. Using DISTINCT to Remove Duplicates
Retrieve distinct rental rates from the film table.
Query:
SELECT DISTINCT rental_rate
FROM film;
2. Sorting Results with ORDER BY
Retrieve and sort films by their title.
Query:
SELECT title, rental_rate
FROM film
ORDER BY title;
3. Filtering Results with WHERE
Retrieve customers from a specific city.
Query:
SELECT first_name, last_name, email
FROM customer
WHERE city = 'Los Angeles';
4. Limiting Results with LIMIT and FETCH
Retrieve the first 10 customers.
Query:
SELECT *
FROM customer
LIMIT 10;
Or using FETCH.
Query:
SELECT *
FROM customer
FETCH FIRST 10 ROWS ONLY;
5. Grouping Results with GROUP BY
Retrieve the count of customers by city.
Query:
SELECT city, COUNT(*)
FROM customer
GROUP BY city;
6. Filtering Groups with HAVING
Retrieve cities with more than 5 customers.
Query:
SELECT city, COUNT(*)
FROM customer
GROUP BY city
HAVING COUNT(*) > 5;
7. Joining Tables with JOIN
Retrieve rental information along with customer details.
Query:
SELECT c.first_name, c.last_name, r.rental_date
FROM customer c
INNER JOIN rental r ON c.customer_id = r.customer_id;
8. Combining Results with Set Operators
Retrieve all unique cities from both customer and store tables.
Query:
SELECT city
FROM customer
UNION
SELECT city
FROM store;
Conclusion
In conclusion, the PostgreSQL SELECT statement is a versatile and essential tool for querying data from databases. By understanding and utilizing its various clauses, you can perform complex queries efficiently. Whether you are retrieving specific columns, filtering data, or joining tables, the SELECT statement provides the flexibility needed for effective data management.