Menu Close

PostgreSQL SELECT Statement

PostgreSQL SELECT Statement

In this tutorial, you will learn about how to use PostgreSQL SELECT statement to query data from a table. In the previous PostgreSQL tutorial, we have seen How to use the PostgreSQL CREATE TABLE statement to create a new table inside the database.

Select Statement in PostgreSQL

The select command comes under SQL DQL ( Data Query Language ) Command. PostgreSQL provides us with a command named SELECT that is used to fetch the required data from the database.

Syntax

The following syntax will only work when you want to select only particular columns from the table.

SELECT column1, column2, column3 FROM table_name WHERE search_condition;

In the above SELECT statement column1, column2 and column3 represent the name of the columns where you want to retrieve the data from.
If you want to retrieve all the fields from the table, you can use the following syntax.

SELECT * FROM table_name WHERE search_condition;

PostgresQL SELECT Statement Example

In this example, we will use the students table. We have inserted rows in the previous tutorial.

  • Using SELECT statement we will query data from Only two columns of students table.

Example

SELECT first_name, last_name FROM students;

Output


 first_name | last_name
------------+-----------
 Vishvajit  | Rao
 Anshika    | kumari
 Mariya     | Singh
 Harsh      | Jain
 Priya      | Kumari
 Banti      | Rao
 Ayush      | Rao
  • Using SELECT statement we are going to query data from all columns of students table.
SELECT * FROM students;

Output

 st_id | first_name | last_name | age
-------+------------+-----------+-----
     1 | Vishvajit  | Rao       |  22
     3 | Anshika    | kumari    |  22
     5 | Mariya     | Singh     |  22
     4 | Harsh      | Jain      |  25
     6 | Priya      | Kumari    |  23
     7 | Banti      | Rao       |  20
     2 | Ayush      | Rao       |  24

In the above example, we used an asterisk ( * ) in the SELECT clause. This is the best way to select all the columns from the table. Suppose you have a large table with many columns, The SELECT Statement with an asterisk ( * ) will receive all the columns data from a table.

  • Using SELECT statement with expression Example

Example

SELECT first_name || '*' || last_name AS FullName, age FROM students;

Output

    FullName    | age
----------------+-----
 Vishvajit Rao  |  22
 Anshika kumari |  22
 Mariya Singh   |  22
 Harsh Jain     |  25
 Priya Kumari   |  23
 Banti Rao      |  20
 Ayush Rao      |  24
  • Select Statement with condition: Here we are going to search all the students records whose id lies between 2 to 6 inlcuding 2 and 6.
SELECT * FROM students WHERE st_id in (2,3,4,5,6);

Output

 st_id | first_name | last_name | age
-------+------------+-----------+-----
     2 | Ayush      | Rao       |  24
     3 | Anshika    | kumari    |  22
     4 | Harsh      | Jain      |  25
     5 | Mariya     | Singh     |  22
     6 | Priya      | Kumari    |  23

Conclusion

So, In this article, we have seen all how to use PostgreSQL select statement to select one or more records from the database table. It allows us to select records whose full fill the specific condition.

It is one of the most useful commands that use most of the time by a developer or a database administrator. I hope this article will have helped you, if you like this article, please share and keep visiting for further PostgreSQL tutorials.

Thanks for your valuable timeā€¦

PostgreSQL INSERT Statement

Related Posts