Lecture 3 - Nested Queries Flashcards

1
Q

What is a nested/inner query?

A

Nested query is a query within another (outer) query.

  • SELECT-FROM-WHERE block within another outer WHERE clause.
  • Nested query’s output is input to outer’s WHERE via: IN, ALL, EXISTS
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the two types of nested queries?

A
  • nested uncorrelated query
  • nested correlated query
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is IN?

A

check if something is within a set

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is ALL?

A

compares a value with all the values from the inner’s output set
using >, >=, <, <=, =, <>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is EXISTS?

A

Checks if resultant set of nested query is not empty.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a nested uncorrelated query?

A

first execute the nested query, and then
execute the outer query using inner’s output.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a nested correlated query?

A

for each tuple of the outer query, we execute the
nested query

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Within nested correlated queries we have ?

A

global and local scopes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Problem with exists?

A

just focus on the count of the nested query or set, but doesn’t care about the content (can be all null)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Example of a nested correlated query?

A

SELECT E.FNAME, E.LNAME
FROM EMPLOYEE AS E
WHERE E.SSN IN ( SELECT D.ESSN
FROM DEPENDENT AS D
WHERE E.FNAME = D.DEPENENT_NAME)

we are passing the employee to the inner query

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Example of a nested uncorrelated query?

A

SELECT FNAME
FROM EMPLOYEE
WHERE DNO IN ( SELECT DNUMBER
FROM DEPARTMENT
WHERE DNAME = ‘Research’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

STUDENT (Name, StudentID, Class)
COURSE (Name, CourseID, Credits, School)
GRADES (StudentID, CourseID, Grade)
/Grade: {‘A’, ‘B’, ‘C’, ‘D’, ‘E’}/
Task: Retrieve the names of all students who have a Grade of ‘A’ in
all of their courses ( ‘distinction’ students)

A

SELECT S.Name
FROM STUDENT S
WHERE NOT EXISTS
(SELECT * FROM GRADES G
WHERE G.StudentID = S.StudentID
AND G.Grade <> ‘A’
)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly