SP 23 24 Lecture11 IDB
SP 23 24 Lecture11 IDB
• Subquery
• Guidelines for using Subquery
• Types of Subquery
• Single-row Subquery
Using a Subquery to Solve a
Problem
Main Query
Subquery
?
“What is Jones’ salary?”
Subquery
• The subquery (inner query) executes once before the main query.
• The result of the subquery is used by the main query (outer query).
• Subqueries are useful when a query is based on unknown values.
SELECT select_list
FROM table
WHERE expr operator
(SELECT select_list
FROM table);
Using a Subquery
ENAME
ENAME
----------
----------
KING
KING
FORD
FORD
SCOTT
SCOTT
Guidelines for using Subquery
• Single-row subquery
Main query
returns
Subquery CLERK
• M u lti p le -ro w su b q u e ry
• Multiple-row subquery
Main query
Subquery
returns CLERK
• M u lti p le -co lu m n su b q u e ry
MANAGER
• Multiple-column subquery
Main query
returns
Subquery CLERK 7900
MANAGER 7698
Single-Row Subquery
Operator Meaning
= Equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
<> Not equal to
Executing Single-Row Subquery
ENAME
ENAME JOB
JOB
----------
---------- ---------
---------
MILLER
MILLER CLERK
CLERK
Using Group Function in a Subquery
ENAME
ENAME JOB
JOB SAL
SAL
----------
---------- ---------
--------- ---------
---------
SMITH
SMITH CLERK
CLERK 800
800
Having Clause with Subquery
no
no rows
rows selected
selected
Will this statement work?
1. https://www.db-book.com/db6/slide-dir/index.html
2. https://docs.oracle.com/en/database/oracle/oracle-database/20/sqlrf/SQL-Sta
ndards.html#GUID-BCCCFF75-D2A4-43AD-8CAF-C3C97D92AC63
3. https://www.slideshare.net/HaaMeemMohiyuddin1/data-knowledge-and-infor
mation
4. https://www.slideshare.net/tabinhasan/from-data-to-wisdom
5. https://www.slideshare.net/thinnaphat.bo/