Database: shop_membership

Question: How many branches where have more than average number of memberships are there?
SQL: SELECT count(*) FROM branch WHERE membership_amount  >  (SELECT avg(membership_amount) FROM branch)

Question: What are names for top three branches with most number of membership?
SQL: SELECT name FROM branch ORDER BY membership_amount DESC LIMIT 3

Question: List all open years when at least two shops are opened.
SQL: SELECT open_year FROM branch GROUP BY open_year HAVING count(*)  >=  2

Question: Show minimum and maximum amount of memberships for all branches opened in 2011 or located at city London.
SQL: SELECT min(membership_amount) ,  max(membership_amount) FROM branch WHERE open_year  =  2011 OR city  =  'London'

Question: Show the city and the number of branches opened before 2010 for each city.
SQL: SELECT city ,  count(*) FROM branch WHERE open_year  <  2010 GROUP BY city

Question: Show the membership level with most number of members.
SQL: SELECT LEVEL FROM member GROUP BY LEVEL ORDER BY count(*) DESC LIMIT 1

Question: Show all member names and registered branch names sorted by register year.
SQL: SELECT T3.name ,  T2.name FROM membership_register_branch AS T1 JOIN branch AS T2 ON T1.branch_id  =  T2.branch_id JOIN member AS T3 ON T1.member_id  =  T3.member_id ORDER BY T1.register_year

Question: Show all branch names with the number of members in each branch registered after 2015.
SQL: SELECT T2.name ,  count(*) FROM membership_register_branch AS T1 JOIN branch AS T2 ON T1.branch_id  =  T2.branch_id WHERE T1.register_year  >  2015 GROUP BY T2.branch_id

Question: Show member names without any registered branch.
SQL: SELECT name FROM member WHERE member_id NOT IN (SELECT member_id FROM membership_register_branch)

Question: List the branch name and city without any registered members.
SQL: SELECT name ,  city FROM branch WHERE branch_id NOT IN (SELECT branch_id FROM membership_register_branch)

Question: What is the name and open year for the branch with most number of memberships registered in 2016?
SQL: SELECT T2.name ,  T2.open_year FROM membership_register_branch AS T1 JOIN branch AS T2 ON T1.branch_id  =  T2.branch_id WHERE T1.register_year  =  2016 GROUP BY T2.branch_id ORDER BY count(*) DESC LIMIT 1

Question: Show the member name and hometown who registered a branch in 2016.
SQL: SELECT T2.name ,  T2.hometown FROM membership_register_branch AS T1 JOIN member AS T2 ON T1.member_id  =  T2.member_id WHERE T1.register_year  =  2016

Question: Show all city with a branch opened in 2001 and a branch with more than 100 membership.
SQL: SELECT city FROM branch WHERE open_year  =  2001 AND membership_amount  >  100

Question: Show all cities without a branch having more than 100 memberships.
SQL: SELECT city FROM branch EXCEPT SELECT city FROM branch WHERE membership_amount  >  100

Question: What is the sum of total pounds of purchase in year 2018 for all branches in London?
SQL: SELECT sum(total_pounds) FROM purchase AS T1 JOIN branch AS T2 ON T1.branch_id  =  T2.branch_id WHERE T2.city  =  'London' AND T1.year  =  2018