Database: local_govt_and_lot

Question: How many residents does each property have? List property id and resident count.
SQL: SELECT T1.property_id ,  count(*) FROM properties AS T1 JOIN residents AS T2 ON T1.property_id  =  T2.property_id GROUP BY T1.property_id

Question: What is the distinct service types that are provided by the organization which has detail 'Denesik and Sons Party'?
SQL: SELECT DISTINCT T1.service_type_code FROM services AS T1 JOIN organizations AS T2 ON T1.organization_id  =  T2.organization_id WHERE T2.organization_details  =  'Denesik and Sons Party'

Question: How many services has each resident requested? List the resident id, details, and the count in descending order of the count.
SQL: SELECT t1.resident_id ,  t1.other_details ,  count(*) FROM residents AS t1 JOIN residents_services AS t2 ON t1.resident_id  =  t2.resident_id GROUP BY t1.resident_id ORDER BY count(*) DESC

Question: What is the maximum number that a certain service is provided? List the service id, details and number.
SQL: SELECT T1.service_id ,  T1.service_details ,  count(*) FROM Services AS T1 JOIN Residents_Services AS T2 ON T1.service_id  =  T2.service_id GROUP BY T1.service_id ORDER BY count(*) DESC LIMIT 1

Question: What are the id and details of the customers who have at least 3 events?
SQL: SELECT T1.customer_id ,  T1.customer_details FROM Customers AS T1 JOIN Customer_Events AS T2 ON T1.customer_id  =  T2.customer_id GROUP BY T1.customer_id HAVING count(*)  >=  3

Question: Which events have the number of notes between one and three? List the event id and the property id.
SQL: SELECT T1.Customer_Event_ID  ,  T1.property_id FROM Customer_Events AS T1 JOIN Customer_Event_Notes AS T2 ON T1.Customer_Event_ID  =  T2.Customer_Event_ID GROUP BY T1.customer_event_id HAVING count(*) BETWEEN 1 AND 3

Question: What are the distinct id and type of the thing that has the status 'Close' or has a status record before the date '2017-06-19 02:59:21'
SQL: SELECT DISTINCT T2.thing_id ,  T2.Type_of_Thing_Code FROM Timed_Status_of_Things AS T1 JOIN Things AS T2 ON T1.thing_id  =  T2.thing_id WHERE T1.Status_of_Thing_Code  =  'Close' OR T1.Date_and_Date  <  '2017-06-19 02:59:21'

Question: How many distinct locations have the things with service detail 'Unsatisfied' been located in?
SQL: SELECT count(DISTINCT T2.Location_Code) FROM Things AS T1 JOIN Timed_Locations_of_Things AS T2 ON T1.thing_id  =  T2.thing_id WHERE T1.service_details  =  'Unsatisfied'

Question: Which organizations are not a parent organization of others? List the organization id.
SQL: SELECT organization_id FROM organizations EXCEPT SELECT parent_organization_id FROM organizations

Question: What are the resident details containing the substring 'Miss'?
SQL: SELECT other_details FROM Residents WHERE other_details LIKE '%Miss%'