PostgreSQL SELECT
Summary: in this tutorial, you are going to learn how to use the basic PostgreSQL SELECT statement to query data from a table.
Note that if you don’t know how to execute a query against the PostgreSQL database using the psql command-line tool or pgAdmin GUI tool, you can check the connection to the PostgreSQL database tutorial.
One of the most common tasks, when you work with the database, is to retrieve data from tables using the SELECT
statement.
The SELECT
statement is one of the most complex statements in PostgreSQL. It has many clauses that you can use to form a flexible query.
Due to its complexity, we will break it down into many shorter and easy-to-understand tutorials so that you can learn about each clause faster.
The SELECT
statement has the following clauses:
- Select distinct rows using
DISTINCT
operator. - Sort rows using
ORDER BY
clause. - Filter rows using
WHERE
clause. - Select a subset of rows from a table using
LIMIT
orFETCH
clause. - Group rows into groups using
GROUP BY
clause. - Filter groups using
HAVING
clause. - Join with other tables using joins such as
INNER JOIN
,LEFT JOIN
,FULL OUTER JOIN
,CROSS JOIN
clauses. - Perform set operations using
UNION
,INTERSECT
, andEXCEPT
.
In this tutorial, you are going to focus on the SELECT
and FROM
clauses.
PostgreSQL SELECT statement syntax
Let’s start with the basic form of the SELECT
statement that retrieves data from a single table.
The following illustrates the syntax of the SELECT
statement:
In this syntax:
- First, specify a select list that can be a column or a list of columns in a table from which you want to retrieve data. If you specify a list of columns, you need to place a comma (
,
) between two columns to separate them. If you want to select data from all the columns of the table, you can use an asterisk (*
) shorthand instead of specifying all the column names. The select list may also contain expressions or literal values. - Second, provide the name of the table from which you want to query data after the
FROM
keyword.
The FROM
clause is optional. If you are not querying data from any table, you can omit the FROM
clause in the SELECT
statement.
PostgreSQL evaluates the FROM
clause before the SELECT
clause in the SELECT
statement:
Note that the SQL keywords are case-insensitive. It means that SELECT
is equivalent to select
or Select
. By convention, we will use all the SQL keywords in uppercase to make the queries easier to read.
PostgreSQL SELECT examples
Let’s explore some examples of using the SELECT
statement.
We will use the following customer
table in the dvdrental
sampledatabase for the demonstration.
First, connect to the PostgreSQL server using the postgres
user:
You’ll be prompted to enter a password for the postgres
user. After entering the password correctly, you will be connected to the PostgreSQL server:
Second, switch the current database to dvdrental:
Third, execute the query in the following examples.
1) Using PostgreSQL SELECT statement to query data from one column example
This example uses the SELECT
statement to find the first names of all customers from the customer
table:
Here is the partial output:
Notice that we added a semicolon (;
) at the end of the SELECT
statement. The semicolon is not a part of the SQL statement; rather, it serves as a signal of PostgreSQL indicating the conclusion of an SQL statement. Additionally, semicolons are used to separate two or more SQL statements.
2) Using PostgreSQL SELECT statement to query data from multiple columns example
The following query uses the SELECT
statement to retrieve first name, last name, and email of customers from the customer
table:
Partial output:
The output shows three corresponding columns first_name, last_name, and email.
3) Using PostgreSQL SELECT statement to query data from all columns of a table example
The following query uses the SELECT *
statement to retrieve data from all columns of the customer
table:
Partial output:
In this example, we used an asterisk (*
) in the SELECT
clause, which serves as a shorthand for all columns.
Instead of listing all columns in the SELECT
clause individually, we can use the asterisk (*
) to make the query shorter.
However, using the asterisk (*
) in the SELECT
statement is considered a bad practice when you embed SQL statements in the application code, such as Python, Java, or PHP for the following reasons:
- Database performance. Suppose you have a table with many columns and substantial data, the
SELECT
statement with the asterisk (*
) shorthand will select data from all the columns of the table, potentially retrieving more data than required for the application. - Application performance. Retrieving unnecessary data from the database increases the traffic between the PostgreSQL server and the application server. Consequently, this can result in slower response times and reduced scalability for your applications.
For these reasons, it is recommended to explicitly specify the column names in the SELECT
clause whenever possible. This ensures that only the necessary data is retrieved from the database, contributing to more efficient and optimized queries.
The asterisk (*) shorthand should be reserved solely for the ad-hoc queries that examine data from the database.
4) Using PostgreSQL SELECT statement with expressions example
The following example uses the SELECT
statement to return the full names and emails of all customers from the customer
table:
Output:
In this example, we used the concatenation operator ||
to concatenate the first name, space, and last name of every customer.
Notice the first column of the output doesn’t have a name but ?column?
. To assign a name to a column temporarily in the query, you can use a column alias:
The AS keyword is optional. Therefore, you can use a shorter syntax:
For example, you can assign a column alias full_name to the first column of the query as follows:
Output:
5) Using PostgreSQL SELECT statement without a FROM clause
The FROM
clause of the SELECT
statement is optional. Therefore, you can omit it in the SELECT statement.
Typically, you use the SELECT
clause with a function to retrieve the function result. For example:
In this example, we use the NOW()
function in the SELECT
statement. It’ll return the current date and time of the PostgreSQL server.
Summary
- Use the
SELECT ... FROM
statement to retrieve data from a table. - PostgreSQL evaluates the
FROM
clause before theSELECT
clause. - Use a column alias to assign a temporary name to a column or an expression in a query.
- In PostgreSQL, the
FROM
clause is optional.