Question

Produce a list of facilities with more than 1000 slots booked. Produce an output table consisting of facility id and hours, sorted by facility id.
Schema reminder
DB schema

Expected Results

facid Total Slots
0 1320
1 1278
2 1209
4 1404
6 1104

Your Answer Hint Help Save Run Query

 

Answers and Discussion Show

select facid, sum(slots) as "Total Slots"
        from cd.bookings
        group by facid
        having sum(slots) > 1000
        order by facid          

It turns out that there's actually an SQL keyword designed to help with the filtering of output from aggregate functions. This keyword is HAVING.

The behaviour of HAVING is easily confused with that of WHERE. The best way to think about it is that in the context of a query with an aggregate function, WHERE is used to filter what data gets input into the aggregate function, while HAVING is used to filter the data once it is output from the function. Try experimenting to explore this difference!

Try investigating the HAVING clause.

Keyboard shortcuts:


Other hints: