PostgreSQL - SELECT



PostgreSQL SELECT statement is used to fetch the data from a database table that returns data in the form of result table. These result tables are called result-sets.

Syntax

Following is the basic syntax of SELECT statement is as follows −

SELECT column1, column2, columnN FROM table_name;

Here,

  • column1, column2...are the fields of a table, whose values you want to fetch. If you want to fetch all the fields available in the field then you can use the following syntax −
SELECT * FROM table_name;

Example

Consider the table COMPANY having records as follows −

idnameageaddresssalary
1Paul32California20000
2Allen25Texas15000
3Teddy23Norway20000
4Mark25Rich-Mond65000
5David27Texas85000
6Kim22South-Hall45000
7James24Houston10000
(7 rows)

The following is an example, which would fetch ID, Name and Salary fields of the customers available in CUSTOMERS table −

testdb=# SELECT ID, NAME, SALARY FROM COMPANY ;

This would produce the following result −

idnamesalary
1Paul20000
2Allen15000
3Teddy20000
4Mark65000
5David85000
6Kim45000
7James10000
(7 rows)

If you want to fetch all the fields of CUSTOMERS table, then use the following query −

testdb=# SELECT * FROM COMPANY;

This would produce the following result −

idnameageaddresssalary
1Paul32California20000
2Allen25Texas15000
3Teddy23Norway20000
4Mark25Rich-Mond65000
5David27Texas85000
6Kim22South-Hall45000
7James24Houston10000
(7 rows)
Advertisements