1 of 26

CS-10337 – Applied Database Technologies� Lecture 8

By Prof. Rafael Orta

2 of 26

Wayground

3 of 26

Last class we covered

  • Lab #2
  • Data Modeling Part II
  • Midterm

4 of 26

Agenda

  • Subqueries

5 of 26

Subqueries – Single row subquery

A subquery (also called an inner query or nested query) is a query inside another query.

It’s like asking a question within a question — first you find one answer, and then you use that answer to solve the bigger question.

Example:

“Who earns more than the average salary?”

To answer that, you must first find the average salary, then use it in your outer query.

That’s exactly what a subquery does.

6 of 26

Subqueries – Single row subquery

7 of 26

Subqueries – Single row subquery

8 of 26

Types of Subqueries

9 of 26

Multi-row subquery

10 of 26

Multi-row Subquery

11 of 26

Correlated Subquery

The inner query uses e.department_id from the outer query.

It calculates the average salary for each department and compares each employee’s salary to their department’s average.

This runs once per row, not once for the whole table.

12 of 26

Correlated Subquery

13 of 26

Subquery in the from clause (inline view)

  • The subquery (named dept_avg) acts like a temporary table.
  • The outer query filters departments with high average salaries.

14 of 26

Subquery in the from clause (inline view)

15 of 26

Subquery in the select clause

16 of 26

Attendance

17 of 26

DEMO TIME

18 of 26

DEMO TIME

19 of 26

DEMO TIME

20 of 26

Supplemental Video(s)

21 of 26

Non-graded hands-on practice

Exercise 1 — Single-row subquery

Prompt: List course_code and title for all courses in the same department as “Dr. Elena Ruiz.”

Hint: First find Dr. Ruiz’s dept_id, then use it in the outer query.

Exercise 2 — Multi-row subquery (simpler version)

Prompt: Find the full_name of all students whose major department is one of the departments that offer courses worth more than 3 credits.

Hint: The inner query should find all departments that have any 4-credit course, then the outer query returns students majoring in those departments.

22 of 26

Non-graded hands-on practice

Exercise 1 — Single-row subquery

Prompt: List course_code and title for all courses in the same department as “Dr. Elena Ruiz.”

Hint: First find Dr. Ruiz’s dept_id, then use it in the outer query.

Exercise 2 — Multi-row subquery (simpler version)

Prompt: Find the full_name of all students whose major department is one of the departments that offer courses worth more than 3 credits.

Hint: The inner query should find all departments that have any 4-credit course, then the outer query returns students majoring in those departments.

23 of 26

Supplemental Reading

24 of 26

Knowledge Check

Which of the following statements about subqueries are true?�(Select all that apply)

�A. A subquery is a query inside another query.�B. Subqueries can return one or multiple results.�C. Subqueries cannot use aggregate functions.�D. Subqueries can be used in SELECT, WHERE, or FROM clauses.

Correct Answers: A, B, D

25 of 26

Reference Material used in this presentation

  • Murach’s MySQL 3rd Edition.
  • Fundamentals of Database Systems, 7th Edition by Elmasnri
  • Proprietary Material by the author.
  • Material from Professor Jack Mayers.
  • Material from Professor Phillip Quinn.

26 of 26