Skip to main content

GATE CSE 2016Databases › SQLVerified

GATE CSE 2016 PYQ: Consider the following database table named water_schemes: scheme_no district_name capacity 1 Ajmer 20 1…

Consider the following database table named water_schemes:
scheme_nodistrict_namecapacity1Ajmer201Bikaner102Bikaner103Bikaner201Churu102Churu201Dungargarh10Water_schemes\overset{\text{Water\_schemes}}{\begin{array}{|c|c|c|}\hline \textbf{scheme\_no}& \textbf{district\_name}& \textbf{capacity} \\\hline 1& \text{Ajmer}& 20 \\ \hline 1& \text{Bikaner}& 10 \\ \hline 2& \text{Bikaner}& 10 \\\hline 3& \text{Bikaner}&20 \\\hline 1& \text{Churu}& 10 \\\hline 2& \text{Churu}& 20 \\\hline 1& \text{Dungargarh}& 10 \\\hline \end{array}}

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

    Answer

    2

    Approach

    The query builds two common table expressions: first, total computes district-wise total capacity; second, $total_avg$ computes the average of those district totals. The final SELECT cross-joins the district totals with the single average value and keeps districts whose total capacity is at least that average.

    The district totals are 20, 40, 30, and 10. Their average is 25. The final query returns district names whose total capacity is at least 25, which are Bikaner and Churu, hence 2 tuples.

    Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.