Consider the following relations and
How many tuples does the result of the following SQL query contain?
SELECT A.Id
FROM A
WHERE A.Age > ALL (SELECT B.Age
FROM B
WHERE B.Name = ‘Arun’)
View worked solution
Answer: B
To find the number of tuples returned by the SQL query, let us evaluate it step-by-step:
Step 1: Evaluate the Subquery
The subquery is:
SELECT B.Age
FROM B
WHERE B.Name = 'Arun'
Looking at relation , there is no tuple where the Name attribute is 'Arun'. Therefore, this subquery returns an empty set ().
Step 2: Evaluate the Outer Query Condition
The outer query filters tuples from relation using the condition:
In SQL, the > ALL operator checks if a value is greater than every value in the subquery result. Mathematically, this is a universal quantification:
When the set is empty (), this condition is vacuously true for any value of A.Age. Thus, the WHERE clause evaluates to TRUE for every tuple in relation .
Step 3: Count the Resulting Tuples
Since the condition is true for all tuples in , the query selects the Id of every tuple in :
- For ,
- For ,
- For ,
Relation contains exactly tuples. Therefore, the result of the query contains tuples.
Free practice · Your answers on this page are not saved.
