0% found this document useful (0 votes)
14 views15 pages

SP 23 24 Lecture11 IDB

The document outlines a lecture on subqueries in SQL, covering their definition, types, and guidelines for use. It includes examples of single-row and multiple-row subqueries, as well as the use of group functions and the HAVING clause. Additionally, it provides references for further reading on database management and SQL standards.

Uploaded by

aiubmehedi20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views15 pages

SP 23 24 Lecture11 IDB

The document outlines a lecture on subqueries in SQL, covering their definition, types, and guidelines for use. It includes examples of single-row and multiple-row subqueries, as well as the use of group functions and the HAVING clause. Additionally, it provides references for further reading on database management and SQL standards.

Uploaded by

aiubmehedi20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Subquery: Part 01

Course Code: CSC 2108 Course Title: Introduction To Database

Department of Computer Science


Faculty of Science and Technology

Lecture No: 11 Week No: 06 Semester: TBA


Lecturer: Name & email
Lecture Outline

• Subquery
• Guidelines for using Subquery
• Types of Subquery
• Single-row Subquery
Using a Subquery to Solve a
Problem

•“Who has a salary greater than Jones’?”

Main Query

“Which employees have a salary greater than


? Jones’ salary?”

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

SQL> SELECT ename


2 FROM emp 2975
3 WHERE sal >
4 (SELECT sal
5 FROM emp
6 WHERE empno=7566);

ENAME
ENAME
----------
----------
KING
KING
FORD
FORD
SCOTT
SCOTT
Guidelines for using Subquery

•Enclose subqueries in parentheses.


•Place subqueries on the right side of the comparison operator.
•Use single-row operators with single-row subqueries.
•Use multiple-row operators with multiple-row subqueries.
Types of 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

• Return only one row


• Use single-row comparison operators

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

SQL> SELECT ename, job


2 FROM emp
3 WHERE job = CLERK
4 (SELECT job
5 FROM emp
6 WHERE empno = 7369)
7 AND sal > 1100
8 (SELECT sal
9 FROM emp
10 WHERE empno = 7876);

ENAME
ENAME JOB
JOB
----------
---------- ---------
---------
MILLER
MILLER CLERK
CLERK
Using Group Function in a Subquery

SQL> SELECT ename, job, sal


800
2 FROM emp
3 WHERE sal =
4 (SELECT MIN(sal)
5 FROM emp);

ENAME
ENAME JOB
JOB SAL
SAL
----------
---------- ---------
--------- ---------
---------
SMITH
SMITH CLERK
CLERK 800
800
Having Clause with Subquery

• The Oracle Server executes subqueries first.


• The Oracle Server returns results into the HAVING clause of the main query.

SQL> SELECT deptno, MIN(sal)


2 FROM emp
3 GROUP BY deptno 800
4 HAVING MIN(sal) >
5 (SELECT MIN(sal)
6 FROM emp
7 WHERE deptno = 20);
What is wrong with this statement?

SQL> SELECT empno, ename


2 FROM emp
3 WHERE sal =
4 (SELECT MIN(sal)
5 FROM emp
h
6 r wit GROUP BY
r ato
deptno);
ope ry
r o w q ue
ing le-
ERROR: su b
SORA-01427:
ERROR:
le - r owsingle-row subquery returns more than
m ultip
ORA-01427: single-row subquery returns more than
one
one row
row

no
no rows
rows selected
selected
Will this statement work?

SQL> SELECT ename, job


2 FROM emp
3 WHERE job =
4 (SELECT job
5 FROM emp
6 WHERE ename='SMYTHE');
lues
o va
ns n
no
no rows
rows selected ur
ret
selected
uery
ubq
S
Books

1. Modern Database Management (Sixth Edition) by Fred R. McFadden, Jeffrey A.


Hoffer, Mary B. Prescott
2. Database System Concepts (Fifth Edition) by Henry F. Korth, S. Sudarshan, A.
Silberschatz
3. Oracle-database-10g-sql-fundamentals-1-student-guide-volume-1
4. SQL and Relational Theory: How to Write Accurate SQL Code by C.J. Date
5. Database Systems: A Practical Approach to Design, Implementation and
Management (4th Edition) by Thomas M. Connolly, Carolyn E. Begg
6. Fundamentals of Database Systems, 5th Edition by RamezElmasri, Shamkant B.
Navathe
7. Database Design and Relational Theory: Normal Forms and All That Jazz by C. J. Date
8. An Introduction to Database Systems 8th Edition, by C.J. Date
References

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/

You might also like