1. Single-Row Subquery
Course Code: CSC 2108
Dept. of Computer Science
Faculty of Science and Technology
Lecturer No: 11 Week No: 06 Semester: TBA
Lecturer: Name & email
Course Title: Introduction to Database
2. Lecture Outline
After completing this lesson, you should be able to do
the following:
1. Describe the types of problems that subqueries can
solve
2. Define subqueries
3. List the types of subqueries
4. Write single-row subqueries
3. Using a Subquery
to Solve a Problem
“Who has a salary greater than Jones’?”
“Which employees have a salary greater
than Jones’ salary?”
Main Query
?
“What is Jones’ salary?”
?
Subquery
4. Subqueries
The subquery (inner query) executes once before the
main query.
The result of the subquery is used by the main query
(outer query).
SELECT select_list
FROM table
WHERE expr operator
(SELECT select_list
FROM table);
5. 2975
SQL> SELECT ename
2 FROM emp
3 WHERE sal >
4 (SELECT sal
5 FROM emp
6 WHERE empno=7566);
Using a Subquery
ENAME
----------
KING
FORD
SCOTT
6. Guidelines for Using Subqueries
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.
7. Types of Subqueries
Single-row subquery
Main query
Subquery
returns
CLERK
• Multiple-row subquery
CLERK
MANAGER
Main query
Subquery
returns
• Multiple-column subquery
CLERK 7900
MANAGER 7698
Main query
Subquery
returns
9. Single-Row Subqueries
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
10. Executing Single-Row Subqueries
CLERK
1100
ENAME JOB
---------- ---------
MILLER CLERK
SQL> SELECT ename, job
2 FROM emp
3 WHERE job =
4 (SELECT job
5 FROM emp
6 WHERE empno = 7369)
7 AND sal >
8 (SELECT sal
9 FROM emp
10 WHERE empno = 7876);
11. Using Group Functions
in a Subquery
800
ENAME JOB SAL
---------- --------- ---------
SMITH CLERK 800
SQL> SELECT ename, job, sal
2 FROM emp
3 WHERE sal =
4 (SELECT MIN(sal)
5 FROM emp);
12. HAVING Clause with Subqueries
The Oracle Server executes subqueries first.
The Oracle Server returns results into the HAVING
clause of the main query.
800
SQL> SELECT deptno, MIN(sal)
2 FROM emp
3 GROUP BY deptno
4 HAVING MIN(sal) >
5 (SELECT MIN(sal)
6 FROM emp
7 WHERE deptno = 20);
13. What IsWrong
withThis Statement?
ERROR:
ORA-01427: single-row subquery returns more than
one row
no rows selected
SQL> SELECT empno, ename
2 FROM emp
3 WHERE sal =
4 (SELECT MIN(sal)
5 FROM emp
6 GROUP BY deptno);
14. WillThis StatementWork?
no rows selected
SQL> SELECT ename, job
2 FROM emp
3 WHERE job =
4 (SELECT job
5 FROM emp
6 WHERE ename='SMYTHE');
15. 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