Question

Find the total number of members who have made at least one booking.
Schema reminder
DB schema

Expected Results

count
30

Your Answer Hint Help Save Run Query

 

Answers and Discussion Show

select count(distinct memid) from cd.bookings          

Your first instinct may be to go for a subquery here. Something like the below:

select count(*) from 
	(select distinct memid from cd.bookings) as mems

This does work perfectly well, but we can simplify a touch with the help of a little extra knowledge in the form of COUNT DISTINCT. This does what you might expect, counting the distinct values in the passed column.

Take a look at COUNT DISTINCT

Keyboard shortcuts:


Other hints: