Database: museum_visit
# museum ( museum_id , name , num_of_staff , open_year )
# visitor ( id , name , level_of_membership , age )
# visit ( museum_id , visitor_id , num_of_ticket , total_spent )
# visit.visitor_id = visitor.id
# visit.museum_id = museum.museum_id

Question: How many visitors below age 30 are there?
SQL: select count ( * ) from visitor where age < 30

Question: Find the names of the visitors whose membership level is higher than 4, and order the results by the level from high to low.
SQL: select name from visitor where level_of_membership > 4 order by level_of_membership desc

Question: What is the average age of the visitors whose membership level is not higher than 4?
SQL: select avg ( age ) from visitor where level_of_membership <= 4

Question: Find the name and membership level of the visitors whose membership level is higher than 4, and sort by their age from old to young.
SQL: select name , level_of_membership from visitor where level_of_membership > 4 order by age desc

Question: Find the id and name of the museum that has the most staff members?
SQL: select museum_id , name from museum order by num_of_staff desc limit 1

Question: Find the average number of staff working for the museums that were open before 2009.
SQL: select avg ( num_of_staff ) from museum where open_year < 2009

Question: What are the opening year and staff number of the museum named Plaza Museum?
SQL: select num_of_staff , open_year from museum where name = 'Plaza Museum'

Question: find the names of museums which have more staff than the minimum staff number of all museums opened after 2010.
SQL: select name from museum where num_of_staff > ( select min ( num_of_staff ) from museum where open_year > 2010 )

Question: find the id, name and age for visitors who visited some museums more than once.
SQL: select visitor.id , visitor.name , visitor.age from visitor join visit on visitor.id = visit.visitor_id group by visitor.id having count ( * ) > 1

Question: What are the id, name and membership level of visitors who have spent the largest amount of money in total in all museum tickets?
SQL: select visit.visitor_id , visitor.name , visitor.level_of_membership from visitor join visit on visitor.id = visit.visitor_id group by visit.visitor_id order by sum ( visit.total_spent ) desc limit 1

Question: What are the id and name of the museum visited most times?
SQL: select visit.museum_id , museum.name from museum join visit on museum.museum_id = visit.museum_id group by visit.museum_id order by count ( * ) desc limit 1

Question: What is the name of the museum that had no visitor yet?
SQL: select name from museum where museum_id not in ( select museum_id from visit )

Question: Find the name and age of the visitor who bought the most tickets at once.
SQL: select visitor.name , visitor.age from visitor join visit on visitor.id = visit.visitor_id order by visit.num_of_ticket desc limit 1

Question: What are the average and maximum number of tickets bought in all visits?
SQL: select avg ( num_of_ticket ) , max ( num_of_ticket ) from visit

Question: What is the total ticket expense of the visitors whose membership level is 1?
SQL: select sum ( visit.total_spent ) from visitor join visit on visitor.id = visit.visitor_id where visitor.level_of_membership = 1

Question: What is the name of the visitor who visited both a museum opened before 2009 and a museum opened after 2011?
SQL: select visitor.name from visitor join visit on visitor.id = visit.visitor_id join museum on museum.museum_id = visit.museum_id where museum.open_year < 2009 intersect select visitor.name from visitor join visit on visitor.id = visit.visitor_id join museum on museum.museum_id = visit.museum_id where museum.open_year > 2011

Question: Find the number of visitors who did not visit any museum opened after 2010.
SQL: select count ( * ) from visitor where id not in ( select visit.visitor_id from museum join visit on museum.museum_id = visit.museum_id where museum.open_year > 2010 )

Question: How many museums were opened after 2013 or before 2008?
SQL: select count ( * ) from museum where open_year > 2013 or open_year < 2008