SQL - Query Block, Sub Query

About

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

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')

Powered by ComboStrap