PostgreSQL – SELECT Statement

SELECT statement is used to retrieve data from the database.

In it’s basic form, a select statement looks as shown below:

Here is an example of select statement used to retrieve the current date from the database.

SELECT current_date;

You can perform simple arithmetic operations in the database and use select statement to view the output:

SELECT 3232+5629+37589;

In Relational databases, data is stored in tables. SELECT statement is used to retrieve data that is stored in the database tables.

Example:

Consider an employee table as shown below:

idfirst_namelast_namedepartmentsalary
101bryanwalkersales50,000
102alexcarpenteraccounts55,000
103samwhitesales60,000
104johnbarrysales54,000
105billwoodaccounts65,000
106roberthopkinssupport70,000
107davidleesupport72,000
108jacksearssupport65,000
109tomyoungsales55,000
employee

Below is the SQL query to return all the data from employee table. ‘*’ is short to return all the columns of a table without listing all the column names.

SELECT * FROM employee;

id first_name last_name department salary
--- --------- --------- ---------- -------
101 bryan      walker    sales      50000
102 alex       carpenter accounts   55000
.
.
.

To return only the selected columns from a table, you need to list all the column names in your select statement. Example, below is the query to return just the id, first name and last name columns from the employee table.

SELECT id, first_name, last_name FROM employee;

id   first_name   last_name
---  ----------   ----------
101   bryan        walker
102   alex         carpenter
.
.
.