Database: dog_kennels
Schema: CREATE TABLE `Breeds` ( `breed_code` VARCHAR(10) PRIMARY KEY , `breed_name` VARCHAR(80) ); CREATE TABLE `Charges` ( `charge_id` INTEGER PRIMARY KEY , `charge_type` VARCHAR(10), `charge_amount` DECIMAL(19,4) ); CREATE TABLE `Sizes` ( `size_code` VARCHAR(10) PRIMARY KEY , `size_description` VARCHAR(80) ); CREATE TABLE `Treatment_Types` ( `treatment_type_code` VARCHAR(10) PRIMARY KEY , `treatment_type_description` VARCHAR(80) ); CREATE TABLE `Owners` ( `owner_id` INTEGER PRIMARY KEY , `first_name` VARCHAR(50), `last_name` VARCHAR(50), `street` VARCHAR(50), `city` VARCHAR(50), `state` VARCHAR(20), `zip_code` VARCHAR(20), `email_address` VARCHAR(50), `home_phone` VARCHAR(20), `cell_number` VARCHAR(20) ); CREATE TABLE `Dogs` ( `dog_id` INTEGER PRIMARY KEY , `owner_id` INTEGER NOT NULL, `abandoned_yn` VARCHAR(1), `breed_code` VARCHAR(10) NOT NULL, `size_code` VARCHAR(10) NOT NULL, `name` VARCHAR(50), `age` VARCHAR(20), `date_of_birth` DATETIME, `gender` VARCHAR(1), `weight` VARCHAR(20), `date_arrived` DATETIME, `date_adopted` DATETIME, `date_departed` DATETIME, FOREIGN KEY (`breed_code` ) REFERENCES `Breeds`(`breed_code` ), FOREIGN KEY (`size_code` ) REFERENCES `Sizes`(`size_code` ), FOREIGN KEY (`owner_id` ) REFERENCES `Owners`(`owner_id` ), FOREIGN KEY (`owner_id` ) REFERENCES `Owners`(`owner_id` ) ); CREATE TABLE `Professionals` ( `professional_id` INTEGER PRIMARY KEY , `role_code` VARCHAR(10) NOT NULL, `first_name` VARCHAR(50), `street` VARCHAR(50), `city` VARCHAR(50), `state` VARCHAR(20), `zip_code` VARCHAR(20), `last_name` VARCHAR(50), `email_address` VARCHAR(50), `home_phone` VARCHAR(20), `cell_number` VARCHAR(20) ); CREATE TABLE `Treatments` ( `treatment_id` INTEGER PRIMARY KEY , `dog_id` INTEGER NOT NULL, `professional_id` INTEGER NOT NULL, `treatment_type_code` VARCHAR(10) NOT NULL, `date_of_treatment` DATETIME, `cost_of_treatment` DECIMAL(19,4), FOREIGN KEY (`treatment_type_code` ) REFERENCES `Treatment_Types`(`treatment_type_code` ), FOREIGN KEY (`professional_id` ) REFERENCES `Professionals`(`professional_id` ), FOREIGN KEY (`dog_id` ) REFERENCES `Dogs`(`dog_id` ) );

Question (hard): Which states have both owners and professionals living there?
SQL: SELECT state FROM Owners INTERSECT SELECT state FROM Professionals

Question (hard): Find the states where both owners and professionals live.
SQL: SELECT state FROM Owners INTERSECT SELECT state FROM Professionals

Question (hard): What is the average age of the dogs who have gone through any treatments?
SQL: SELECT avg(age) FROM Dogs WHERE dog_id IN ( SELECT dog_id FROM Treatments )

Question (hard): Find the average age of the dogs who went through treatments.
SQL: SELECT avg(age) FROM Dogs WHERE dog_id IN ( SELECT dog_id FROM Treatments )

Question (extra): Which professionals live in the state of Indiana or have done treatment on more than 2 treatments? List his or her id, last name and cell phone.
SQL: SELECT professional_id ,  last_name ,  cell_number FROM Professionals WHERE state  =  'Indiana' UNION SELECT T1.professional_id ,  T1.last_name ,  T1.cell_number FROM Professionals AS T1 JOIN Treatments AS T2 ON T1.professional_id  =  T2.professional_id GROUP BY T1.professional_id HAVING count(*)  >  2

Question (extra): Find the id, last name and cell phone of the professionals who live in the state of Indiana or have performed more than two treatments.
SQL: SELECT professional_id ,  last_name ,  cell_number FROM Professionals WHERE state  =  'Indiana' UNION SELECT T1.professional_id ,  T1.last_name ,  T1.cell_number FROM Professionals AS T1 JOIN Treatments AS T2 ON T1.professional_id  =  T2.professional_id GROUP BY T1.professional_id HAVING count(*)  >  2

Question (hard): Which dogs have not cost their owner more than 1000 for treatment ? List the dog names .
SQL: select name from dogs where dog_id not in ( select dog_id from treatments group by dog_id having sum(cost_of_treatment)  >  1000 )

Question (hard): What are the names of the dogs for which the owner has not spend more than 1000 for treatment ?
SQL: select name from dogs where dog_id not in ( select dog_id from treatments group by dog_id having sum(cost_of_treatment)  >  1000 )

Question (hard): Which first names are used for professionals or owners but are not used as dog names?
SQL: SELECT first_name FROM Professionals UNION SELECT first_name FROM Owners EXCEPT SELECT name FROM Dogs

Question (hard): Find the first names that are used for professionals or owners but are not used as dog names.
SQL: SELECT first_name FROM Professionals UNION SELECT first_name FROM Owners EXCEPT SELECT name FROM Dogs

Question (extra): Which professional did not operate any treatment on dogs? List the professional's id, role and email.
SQL: SELECT professional_id ,  role_code ,  email_address FROM Professionals EXCEPT SELECT T1.professional_id ,  T1.role_code ,  T1.email_address FROM Professionals AS T1 JOIN Treatments AS T2 ON T1.professional_id  =  T2.professional_id

Question (extra): Give me the id, role and email of the professionals who did not perform any treatment on dogs.
SQL: SELECT professional_id ,  role_code ,  email_address FROM Professionals EXCEPT SELECT T1.professional_id ,  T1.role_code ,  T1.email_address FROM Professionals AS T1 JOIN Treatments AS T2 ON T1.professional_id  =  T2.professional_id

Question (extra): Which owner owns the most dogs? List the owner id, first name and last name.
SQL: SELECT T1.owner_id ,  T2.first_name ,  T2.last_name FROM Dogs AS T1 JOIN Owners AS T2 ON T1.owner_id  =  T2.owner_id GROUP BY T1.owner_id ORDER BY count(*) DESC LIMIT 1

Question (extra): Return the owner id, first name and last name of the owner who has the most dogs.
SQL: SELECT T1.owner_id ,  T2.first_name ,  T2.last_name FROM Dogs AS T1 JOIN Owners AS T2 ON T1.owner_id  =  T2.owner_id GROUP BY T1.owner_id ORDER BY count(*) DESC LIMIT 1

Question (medium): Which professionals have done at least two treatments? List the professional's id, role, and first name.
SQL: SELECT T1.professional_id ,  T1.role_code ,  T1.first_name FROM Professionals AS T1 JOIN Treatments AS T2 ON T1.professional_id  =  T2.professional_id GROUP BY T1.professional_id HAVING count(*)  >=  2

Question (medium): What are the id, role, and first name of the professionals who have performed two or more treatments?
SQL: SELECT T1.professional_id ,  T1.role_code ,  T1.first_name FROM Professionals AS T1 JOIN Treatments AS T2 ON T1.professional_id  =  T2.professional_id GROUP BY T1.professional_id HAVING count(*)  >=  2

Question (extra): What is the name of the breed with the most dogs?
SQL: SELECT T1.breed_name FROM Breeds AS T1 JOIN Dogs AS T2 ON T1.breed_code  =  T2.breed_code GROUP BY T1.breed_name ORDER BY count(*) DESC LIMIT 1

Question (extra): Which breed do the most dogs have? Give me the breed name.
SQL: SELECT T1.breed_name FROM Breeds AS T1 JOIN Dogs AS T2 ON T1.breed_code  =  T2.breed_code GROUP BY T1.breed_name ORDER BY count(*) DESC LIMIT 1

Question (extra): Which owner has paid for the most treatments on his or her dogs? List the owner id and last name.
SQL: SELECT T1.owner_id ,  T1.last_name FROM Owners AS T1 JOIN Dogs AS T2 ON T1.owner_id  =  T2.owner_id JOIN Treatments AS T3 ON T2.dog_id  =  T3.dog_id GROUP BY T1.owner_id ORDER BY count(*) DESC LIMIT 1

Question (extra): Tell me the owner id and last name of the owner who spent the most on treatments of his or her dogs.
SQL: SELECT T1.owner_id ,  T1.last_name FROM Owners AS T1 JOIN Dogs AS T2 ON T1.owner_id  =  T2.owner_id JOIN Treatments AS T3 ON T2.dog_id  =  T3.dog_id GROUP BY T1.owner_id ORDER BY count(*) DESC LIMIT 1

Question (extra): What is the description of the treatment type that costs the least money in total?
SQL: SELECT T1.treatment_type_description FROM Treatment_types AS T1 JOIN Treatments AS T2 ON T1.treatment_type_code  =  T2.treatment_type_code GROUP BY T1.treatment_type_code ORDER BY sum(cost_of_treatment) ASC LIMIT 1

Question (extra): Give me the description of the treatment type whose total cost is the lowest.
SQL: SELECT T1.treatment_type_description FROM Treatment_types AS T1 JOIN Treatments AS T2 ON T1.treatment_type_code  =  T2.treatment_type_code GROUP BY T1.treatment_type_code ORDER BY sum(cost_of_treatment) ASC LIMIT 1

Question (extra): Which owner has paid the largest amount of money in total for their dogs? Show the owner id and zip code.
SQL: SELECT T1.owner_id ,  T1.zip_code FROM Owners AS T1 JOIN Dogs AS T2 ON T1.owner_id  =  T2.owner_id JOIN Treatments AS T3 ON T2.dog_id  =  T3.dog_id GROUP BY T1.owner_id ORDER BY sum(T3.cost_of_treatment) DESC LIMIT 1

Question (extra): Find the owner id and zip code of the owner who spent the most money in total for his or her dogs.
SQL: SELECT T1.owner_id ,  T1.zip_code FROM Owners AS T1 JOIN Dogs AS T2 ON T1.owner_id  =  T2.owner_id JOIN Treatments AS T3 ON T2.dog_id  =  T3.dog_id GROUP BY T1.owner_id ORDER BY sum(T3.cost_of_treatment) DESC LIMIT 1

Question (medium): Which professionals have done at least two types of treatments? List the professional id and cell phone.
SQL: SELECT T1.professional_id ,  T1.cell_number FROM Professionals AS T1 JOIN Treatments AS T2 ON T1.professional_id  =  T2.professional_id GROUP BY T1.professional_id HAVING count(*)  >=  2

Question (medium): Find the id and cell phone of the professionals who operate two or more types of treatments.
SQL: SELECT T1.professional_id ,  T1.cell_number FROM Professionals AS T1 JOIN Treatments AS T2 ON T1.professional_id  =  T2.professional_id GROUP BY T1.professional_id HAVING count(*)  >=  2

Question (extra): What are the first name and last name of the professionals who have done treatment with cost below average?
SQL: SELECT DISTINCT T1.first_name ,  T1.last_name FROM Professionals AS T1 JOIN Treatments AS T2 WHERE cost_of_treatment  <  ( SELECT avg(cost_of_treatment) FROM Treatments )

Question (extra): Which professionals have operated a treatment that costs less than the average? Give me theor first names and last names.
SQL: SELECT DISTINCT T1.first_name ,  T1.last_name FROM Professionals AS T1 JOIN Treatments AS T2 WHERE cost_of_treatment  <  ( SELECT avg(cost_of_treatment) FROM Treatments )

Question (medium): List the date of each treatment, together with the first name of the professional who operated it.
SQL: SELECT T1.date_of_treatment ,  T2.first_name FROM Treatments AS T1 JOIN Professionals AS T2 ON T1.professional_id  =  T2.professional_id

Question (medium): What are the date and the operating professional's first name of each treatment?
SQL: SELECT T1.date_of_treatment ,  T2.first_name FROM Treatments AS T1 JOIN Professionals AS T2 ON T1.professional_id  =  T2.professional_id

Question (medium): List the cost of each treatment and the corresponding treatment type description.
SQL: SELECT T1.cost_of_treatment ,  T2.treatment_type_description FROM Treatments AS T1 JOIN treatment_types AS T2 ON T1.treatment_type_code  =  T2.treatment_type_code

Question (medium): What are the cost and treatment type description of each treatment?
SQL: SELECT T1.cost_of_treatment ,  T2.treatment_type_description FROM Treatments AS T1 JOIN treatment_types AS T2 ON T1.treatment_type_code  =  T2.treatment_type_code

Question (medium): List each owner's first name, last name, and the size of his for her dog.
SQL: SELECT T1.first_name ,  T1.last_name ,  T2.size_code FROM Owners AS T1 JOIN Dogs AS T2 ON T1.owner_id  =  T2.owner_id

Question (medium): What are each owner's first name, last name, and the size of their dog?
SQL: SELECT T1.first_name ,  T1.last_name ,  T2.size_code FROM Owners AS T1 JOIN Dogs AS T2 ON T1.owner_id  =  T2.owner_id

Question (medium): List pairs of the owner's first name and the dogs's name.
SQL: SELECT T1.first_name ,  T2.name FROM Owners AS T1 JOIN Dogs AS T2 ON T1.owner_id  =  T2.owner_id

Question (medium): What are each owner's first name and their dogs's name?
SQL: SELECT T1.first_name ,  T2.name FROM Owners AS T1 JOIN Dogs AS T2 ON T1.owner_id  =  T2.owner_id

Question (extra): List the names of the dogs of the rarest breed and the treatment dates of them.
SQL: SELECT T1.name ,  T2.date_of_treatment FROM Dogs AS T1 JOIN Treatments AS T2 ON T1.dog_id  =  T2.dog_id WHERE T1.breed_code  =  ( SELECT breed_code FROM Dogs GROUP BY breed_code ORDER BY count(*) ASC LIMIT 1 )

Question (extra): Which dogs are of the rarest breed? Show their names and treatment dates.
SQL: SELECT T1.name ,  T2.date_of_treatment FROM Dogs AS T1 JOIN Treatments AS T2 ON T1.dog_id  =  T2.dog_id WHERE T1.breed_code  =  ( SELECT breed_code FROM Dogs GROUP BY breed_code ORDER BY count(*) ASC LIMIT 1 )

Question (medium): Which dogs are owned by someone who lives in Virginia? List the owner's first name and the dog's name.
SQL: SELECT T1.first_name ,  T2.name FROM Owners AS T1 JOIN Dogs AS T2 ON T1.owner_id  =  T2.owner_id WHERE T1.state  =  'Virginia'

Question (medium): Find the first names of owners living in Virginia and the names of dogs they own.
SQL: SELECT T1.first_name ,  T2.name FROM Owners AS T1 JOIN Dogs AS T2 ON T1.owner_id  =  T2.owner_id WHERE T1.state  =  'Virginia'

Question (medium): What are the arriving date and the departing date of the dogs who have gone through a treatment?
SQL: SELECT DISTINCT T1.date_arrived ,  T1.date_departed FROM Dogs AS T1 JOIN Treatments AS T2 ON T1.dog_id  =  T2.dog_id

Question (medium): Find the arriving date and the departing date of the dogs that received a treatment.
SQL: SELECT DISTINCT T1.date_arrived ,  T1.date_departed FROM Dogs AS T1 JOIN Treatments AS T2 ON T1.dog_id  =  T2.dog_id

Question (extra): List the last name of the owner owning the youngest dog.
SQL: SELECT T1.last_name FROM Owners AS T1 JOIN Dogs AS T2 ON T1.owner_id  =  T2.owner_id WHERE T2.age  =  ( SELECT max(age) FROM Dogs )

Question (extra): Who owns the youngest dog? Give me his or her last name.
SQL: SELECT T1.last_name FROM Owners AS T1 JOIN Dogs AS T2 ON T1.owner_id  =  T2.owner_id WHERE T2.age  =  ( SELECT max(age) FROM Dogs )

Question (medium): List the emails of the professionals who live in the state of Hawaii or the state of Wisconsin.
SQL: SELECT email_address FROM Professionals WHERE state  =  'Hawaii' OR state  =  'Wisconsin'

Question (medium): What are the emails of the professionals living in either the state of Hawaii or the state of Wisconsin?
SQL: SELECT email_address FROM Professionals WHERE state  =  'Hawaii' OR state  =  'Wisconsin'

Question (medium): What are the arriving date and the departing date of all the dogs?
SQL: SELECT date_arrived ,  date_departed FROM Dogs

Question (medium): List the arrival date and the departure date for all the dogs.
SQL: SELECT date_arrived ,  date_departed FROM Dogs

Question (easy): How many dogs went through any treatments?
SQL: SELECT count(DISTINCT dog_id) FROM Treatments

Question (easy): Count the number of dogs that went through a treatment.
SQL: SELECT count(DISTINCT dog_id) FROM Treatments

Question (easy): How many professionals have performed any treatment to dogs?
SQL: SELECT count(DISTINCT professional_id) FROM Treatments

Question (easy): Find the number of professionals who have ever treated dogs.
SQL: SELECT count(DISTINCT professional_id) FROM Treatments

Question (medium): Which professionals live in a city containing the substring 'West'? List his or her role, street, city and state.
SQL: SELECT role_code ,  street ,  city ,  state FROM professionals WHERE city LIKE '%West%'

Question (medium): Find the role, street, city and state of the professionals living in a city that contains the substring 'West'.
SQL: SELECT role_code ,  street ,  city ,  state FROM professionals WHERE city LIKE '%West%'

Question (medium): Which owners live in the state whose name contains the substring 'North'? List his first name, last name and email.
SQL: SELECT first_name ,  last_name ,  email_address FROM Owners WHERE state LIKE '%North%'

Question (medium): Return the first name, last name and email of the owners living in a state whose name contains the substring 'North'.
SQL: SELECT first_name ,  last_name ,  email_address FROM Owners WHERE state LIKE '%North%'

Question (hard): How many dogs have an age below the average?
SQL: SELECT count(*) FROM Dogs WHERE age  <  ( SELECT avg(age) FROM Dogs )

Question (hard): Count the number of dogs of an age below the average.
SQL: SELECT count(*) FROM Dogs WHERE age  <  ( SELECT avg(age) FROM Dogs )

Question (medium): How much does the most recent treatment cost?
SQL: SELECT cost_of_treatment FROM Treatments ORDER BY date_of_treatment DESC LIMIT 1

Question (medium): Show me the cost of the most recently performed treatment.
SQL: SELECT cost_of_treatment FROM Treatments ORDER BY date_of_treatment DESC LIMIT 1

Question (extra): How many dogs have not gone through any treatment?
SQL: SELECT count(*) FROM Dogs WHERE dog_id NOT IN ( SELECT dog_id FROM Treatments )

Question (extra): Tell me the number of dogs that have not received any treatment .
SQL: select count(*) from dogs where dog_id not in ( select dog_id from treatments )

Question (extra): How many owners temporarily do not have any dogs?
SQL: SELECT count(*) FROM Owners WHERE owner_id NOT IN ( SELECT owner_id FROM Dogs )

Question (extra): Find the number of owners who do not own any dogs at this moment.
SQL: SELECT count(*) FROM Owners WHERE owner_id NOT IN ( SELECT owner_id FROM Dogs )

Question (extra): How many professionals did not operate any treatment on dogs?
SQL: SELECT count(*) FROM Professionals WHERE professional_id NOT IN ( SELECT professional_id FROM Treatments )

Question (extra): Find the number of professionals who have not treated any dogs.
SQL: SELECT count(*) FROM Professionals WHERE professional_id NOT IN ( SELECT professional_id FROM Treatments )

Question (medium): List the dog name, age and weight of the dogs who have been abandoned? 1 stands for yes, and 0 stands for no.
SQL: SELECT name ,  age ,  weight FROM Dogs WHERE abandoned_yn  =  1

Question (medium): What are the dog name, age and weight of the dogs that were abandoned? Note that 1 stands for yes, and 0 stands for no in the tables.
SQL: SELECT name ,  age ,  weight FROM Dogs WHERE abandoned_yn  =  1

Question (easy): What is the average age of all the dogs?
SQL: SELECT avg(age) FROM Dogs

Question (easy): Compute the average age of all the dogs.
SQL: SELECT avg(age) FROM Dogs

Question (easy): What is the age of the oldest dog?
SQL: SELECT max(age) FROM Dogs

Question (easy): Tell me the age of the oldest dog.
SQL: SELECT max(age) FROM Dogs

Question (medium): How much does each charge type costs? List both charge type and amount.
SQL: SELECT charge_type ,  charge_amount FROM Charges

Question (medium): List each charge type and its amount.
SQL: SELECT charge_type ,  charge_amount FROM Charges

Question (easy): How much does the most expensive charge type costs?
SQL: SELECT max(charge_amount) FROM Charges

Question (easy): What is the charge amount of the most expensive charge type?
SQL: SELECT max(charge_amount) FROM Charges

Question (medium): List the email, cell phone and home phone of all the professionals.
SQL: SELECT email_address ,  cell_number ,  home_phone FROM professionals

Question (medium): What are the email, cell phone and home phone of each professional?
SQL: SELECT email_address ,  cell_number ,  home_phone FROM professionals

Question (medium): What are all the possible breed type and size type combinations?
SQL: SELECT DISTINCT breed_code ,  size_code FROM dogs

Question (medium): Find the distinct breed type and size type combinations for dogs.
SQL: SELECT DISTINCT breed_code ,  size_code FROM dogs

Question (medium): List the first name of all the professionals along with the description of the treatment they have done.
SQL: SELECT DISTINCT T1.first_name ,  T3.treatment_type_description FROM professionals AS T1 JOIN Treatments AS T2 ON T1.professional_id  =  T2.professional_id JOIN Treatment_types AS T3 ON T2.treatment_type_code  =  T3.treatment_type_code

Question (medium): What are each professional's first name and description of the treatment they have performed?
SQL: SELECT DISTINCT T1.first_name ,  T3.treatment_type_description FROM professionals AS T1 JOIN Treatments AS T2 ON T1.professional_id  =  T2.professional_id JOIN Treatment_types AS T3 ON T2.treatment_type_code  =  T3.treatment_type_code