PostgreSQL Subquery

Subquery:

              A Subquery is a query within a query. You can create subqueries within your SQL statements.
These subqueries can reside in the WHERE clause, the FROM clause, or the SELECT clause.
Subqueries can be used with the SELECT, INSERT, UPDATE and DELETE statements along with the operators like =, <, >, >=, <=, IN, etc.

Types of Subqueries

Single Row Sub Query: 

Sub query which returns single row output. They mark the usage of single row comparison operators, when used in WHERE conditions.

Example:
pearl=# select name from siva where age = (select age from siva where age=21);
 name
-------
 muthu
(1 row)

Multiple row sub query: 

Sub query returning multiple row output. They make use of multiple row comparison operators like IN, ANY, ALL. There can be sub queries returning multiple columns also.

Usage of Multiple Row operators

    [> ALL] More than the highest value returned by the subquery

    [< ALL] Less than the lowest value returned by the subquery

    [< ANY] Less than the highest value returned by the subquery

    [> ANY] More than the lowest value returned by the subquery

    [= ANY] Equal to any value returned by the subquery (same as IN)

Examples:

pearl=# select name from siva where age >all (select age from siva where age>=26);
 name
------
(0 rows)

pearl=# select name from siva where age <all (select age from siva where age>=26);
 name
-------
 muthu
(1 row)

pearl=# select name from siva where age <any (select age from siva where age>=26);
 name
-------
 muthu
 siva
(2 rows)

pearl=# select name from siva where age >any (select age from siva where age>=26);
   name 
----------
 thanigai
(1 row)

pearl=# select name from siva where age in (select age from siva where age>=26);
   name 
----------
 siva
 thanigai
(2 rows)

pearl=# select name from siva where age =any (select age from siva where age>=26);
   name 
----------
 siva
 thanigai
(2 rows)

Correlated Sub Query: (Use Multiple Tables)

Correlated subqueries depend on data provided by the outer query.This type of subquery also includes subqueries
that use the EXISTS operator to test the existence of data rows satisfying specified criteria.

Example:
pearl=# select a.id,a.name from siva a where id in (select b.id from muthu b where place ='theni');
 id | name
----+-------
  1 | muthu
(1 row)

pearl=# select a.id,a.name,a.salary from siva a where id = (select b.id from muthu b where place ='theni');
 id | name  | salary
----+-------+--------
  1 | muthu |   2000
(1 row)

pearl=# select a.id,a.name,a.salary from siva a where id in (select b.id from muthu b where place ='theni');
 id | name  | salary
----+-------+--------
  1 | muthu |   2000
(1 row)

pearl=# select a.id,a.name,a.salary from siva a where id >all (select b.id from muthu b where place ='theni');
 id |   name   | salary
----+----------+--------
  2 | siva     |   4000
  3 | thanigai |   5000
(2 rows)

pearl=# select a.id,a.name,a.salary from siva a where id <all (select b.id from muthu b where place ='theni');
 id | name | salary
----+------+--------
(0 rows)

pearl=# select a.id,a.name,a.salary from siva a where id >any (select b.id from muthu b where place ='theni');
 id |   name   | salary
----+----------+--------
  2 | siva     |   4000
  3 | thanigai |   5000
(2 rows)

pearl=# select a.id,a.name,a.salary from siva a where id <any (select b.id from muthu b where place ='theni');
 id | name | salary
----+------+--------
(0 rows)

subquery with EXISTS:

Example:
pearl=# select a.id,a.name from siva a where exists (select from muthu b where a.id=b.id);
 id | name
----+-------
  1 | muthu
(1 row)

Subqueries with the INSERT Statement:

Example:
pearl=# select * from siva;
 id |   name   | age | salary
----+----------+-----+--------
  1 | muthu    |  21 |   2000
  2 | siva     |  26 |   4000
  3 | thanigai |  27 |   5000
(3 rows)
pearl=# create table mms(id int);
CREATE TABLE
pearl=# insert into mms select id from siva  where id in(select id from siva);
INSERT 0 3
pearl=# select * from mms;
 id
----
  1
  2
  3
(3 rows)

Subqueries with the UPDATE Statement:

Example:
pearl=# select * from siva;
 id |   name   | age | salary
----+----------+-----+--------
  1 | muthu    |  21 |   2000
  2 | siva     |  26 |   4000
  3 | thanigai |  27 |   5000
(3 rows)

pearl=# update siva set salary = salary * 2 where age in (select age from siva where age >=26);
UPDATE 2
pearl=# select * from siva;
 id |   name   | age | salary
----+----------+-----+--------
  1 | muthu    |  21 |   2000
  2 | siva     |  26 |   8000
  3 | thanigai |  27 |  10000
(3 rows)

Subqueries with the DELETE Statement:

Example:
pearl=# delete from siva where age in (select age from siva where age >26);
DELETE 1
pearl=# select * from siva;
 id | name  | age | salary
----+-------+-----+--------
  1 | muthu |  21 |   2000
  2 | siva  |  26 |   8000
(2 rows)

Comments

Popular posts from this blog

PostgreSQL pg_pool-II Installation and Configuration

PostgreSQL Migration Using MTK

PostgreSQL Pages and Tuples