SQL - Query Block, Sub Query

> (Data|State) Management and Processing > (Data Type|Data Structure) > (Relation|Table) - Tabular data > Structured Query Language (SQL)

1 - About

A query can contain one or several subquery. A subquery is also known as query block.

Advertising

3 - Example

SELECT e.ename, e.sal      
  FROM (SELECT *
          FROM emp e
         WHERE e.sal > 300
           AND e.deptno IN (SELECT d.deptno
                              FROM dept d
                             WHERE d.dname IN ('SALES', 'ACCOUNTING'))) e

This query contain 3 query block :

  • last
SELECT d.deptno
          FROM dept d
         WHERE d.dname IN ('SALES', 'ACCOUNTING')
  • second :
SELECT *
          FROM emp e
         WHERE e.sal > 300
           AND e.deptno IN ('last query block')
  • main query block
SELECT e.ename, e.sal      
  FROM ('second query block')