How to PostgreSQL- Insert & Select Query

In this tutorial, you will learn how to use INSERT  & SELECT Query to insert & retrieve data respectively from a PostgreSQL table.

 

PostgreSQL INSERT

The PostgreSQL INSERT statement is used to insert a single record or multiple records into a table. The basic syntax of INSERT INTO statement is as follows.

Syntax
    INSERT INTO table
    (column1, column2, ... )
    VALUES
    (expression1 | DEFAULT, expression2 | DEFAULT, ... ),
    (expression1 | DEFAULT, expression2 | DEFAULT, ... ),
    ...;

 

column1, column2, …: The columns in the table to insert values.
expression1, expression2, …: These are the An expression or value to assign to the corresponding column.
DEFAULT: All columns will be populated with their default values.

Example:

Select payroll table that we created on the last guide.

 

Example

postgresql Insert & Select

 

Following example inserts a row into the payroll table.

Insert Example
     INSERT INTO payroll (id, firstname, lastname, payment_date, designation, salary) 
     VALUES (1,'Anwar', 'Yakkiparamban', '2017-01-01', 'Manager', 2000 );

 

Output

Insert & Select Query

 

Following example inserts multiple rows into the payroll table.

Query
     INSERT INTO payroll (id, firstname, lastname, payment_date, designation, salary) 
     VALUES (1,'Habeeb', 'Rahman', '2017-01-01', 'Editor', 1500 ),
     (3, 'Muhsin', 'Yakkiparamban', '2017-01-01', 'Administrator', 1500);

 

Output

Insert & Select Query

 

PostgreSQL SELECT

you are going to learn how to use PostgreSQL SELECT statement to query data from a table. PostgreSQL SELECT statement is used to fetch the data from a database table which returns data in the form of the result table. The basic syntax of simple SELECT statement is as follows.

Syntax
     SELECT column1, column2, ... 
     FROM table_name;

 

column1, column2, …: columns in the table from which you want to query data.

To query data from all rows and all columns from the customer table, you use the following query:

Query
      SELECT * FROM table_name;

 

Example: 

Let’s take a look at an example using PostgreSQL SELECT statement to query the data from the payroll table.

Query
      SELECT * FROM payroll;

Above query retrieve data from all rows and all columns from the payroll table.

 

Output

Insert & Select Query

 

Following query retrieve all firstname and salary from the payroll table.

Query
      SELECT firstname, salary FROM payroll;

 

Output

Insert & Select Query

 

Anwar Yakkiparamban

Anwar Yakkiparamban is the founder of Lauyou Learning. Prior to Lauyou learning, Anwar worked at ARD Engineering & Development, Qatar. He holds bachelor degree in Electronics and Communication Engineering from Govt. Engineering College Idukki.

You may also like...