Consider the following database table named water_schemes:
The number of tuples returned by the following SQL query is _________.
with total (name, capacity) as
select district_name, sum (capacity)
from water_schemes
group by district_name
with total_avg (capacity) as
select avg (capacity)
from total
select name
from total, total_avg
where total.capacity ≥ total_avg.capacity
View worked solution
Answer: 2
To find the number of tuples returned by the given SQL query, we can execute it step-by-step:
Step 1: Evaluate the first CTE (total)
The first CTE groups the water_schemes table by district_name and calculates the sum of capacity for each district:
Step 2: Evaluate the second CTE (total_avg)
The second CTE calculates the average of the capacities obtained in the total relation:
So, total_avg contains a single value: 25.
Step 3: Evaluate the main query
The main query performs a cross join between total and total_avg, filtering for rows where total.capacity ≥ total_avg.capacity (i.e., ):
- Ajmer: ✗ (False)
- Bikaner: ✓ (True)
- Churu: ✓ (True)
- Dungargarh: ✗ (False)
The query returns the names of the districts that satisfy this condition: Bikaner and Churu.
Thus, the number of tuples returned is 2.
Free practice · Your answers on this page are not saved.
