PostgreSQL offers a very useful query named SELECT that retrieves the record of an individual or multiple tables. The SELECTstatement fetches the record from a targeted table of a database and returns the resultant table. On successful execution, the SELECTstatement returns a resultant table entitled as result table or result set.
A wide range of clauses can be used with the Postgres SELECT statement. For example, WHERE, ORDER BY, HAVING, INNER JOIN, CROSS JOIN, UNION, and so on. All these clauses serve different functionalities and provide more ease/flexibility.
In this write-up, we will discuss the basics of SELECT query; however, we will explain the working of each clause in the later tutorials. So, let’s get started!
How Does the SELECT Statement Work in PostgreSQL?
In PostgreSQL, you can fetch the record of an individual, multiple, or all column using the SELECT statement.
How to Select the Data From a Single Column in PostgreSQL?
The syntax given below shows how to fetch the record of a single column in PostgreSQL:
SELECT col_name FROM tab_name;
Here is the step-by-step description of the above-given syntax:
- SELECT is a statement used to select/target an individual or a list of columns.
- col_name is the column to be fetched/selected.
- FROM is a clause that determines which table to select.
- tab_name is the targeted table.
Example: How to Select a column’s record in PostgreSQL?
Follow the below-given step-wise instructions to select a single column in PostgreSQL:
Step 1: Select a Database
Open pgAdmin, and select the desired database from the available databases:
We selected the “example” database.
Step 2: Select a Table
Explore the tables available within the “example” database and select the desired table under the “Schemas” section:
The above snippet shows that there are seven tables available within the example database. We selected the “team_info” table.
Step 3: Check the Available Columns
Click on the selected table and select the “Columns” section to see all the columns of that table:
The above snippet shows that the “team_info” table has three columns i.e. “team_id”, “team_name”, and “team_ranking”.
Step 4: Fetch the Column’s Data
Suppose we need to fetch the data of the “team_name” column. To do this, right-click on the “Columns” section and select the “Query Tool”:
Now, execute the SELECT query to fetch the data of the “team_name” column:
The output shows that the “SELECT” query succeeded in fetching the data of the “team_name” column.
How to Select the Data From Multiple Columns in PostgreSQL?
Let’s have a look at the below-given syntax to understand how to select more than one column in PostgreSQL:
SELECT col_1, col_2, col_3, ..., col_N FROM tab_name;
Here, col_1, col_2, col_3, ..., col_N are the columns to be fetched.
Example: How to Fetch More Than One Column in PostgreSQL?
Suppose we have to fetch the record of the “team_name” and “team_ranking” columns. To do this, we will run the “SELECT” command using the comma-separated syntax:
SELECT team_name, team_ranking FROM team_info;
The output verified that the SELECT query successfully fetched the record of the “team_name” and “team_ranking” columns.
How to Select the Data From ALL Columns in PostgreSQL?
Follow the below syntax to fetch the record of all columns in PostgreSQL:
SELECT * FROM tab_name;
Specifying an asterisk * with the SELECT statement will provide the data of all columns.
Example: How to Fetch All Columns Data in PostgreSQL?
Run the below-given query to fetch all the columns of a table:
SELECT * FROM team_info;
The output shows that this time SELECT query returned all the columns of the selected table.
The SELECT statement is used to fetch a single or more than one column of a table. On successful execution, the SELECT statement returns a resultant table entitled as result table or result set. In PostgreSQL, asterisk * is used with the SELECT statement to fetch the data of all the columns. This write-up explains the working of the SELECT query in PostgreSQL using some relevant examples.