CS 338 Nested SQL Queries Bojana Bislimovska Spring 2017
Exercises 2. A database for an organization that shelters animals, and people can go and adopt animals that they shelter, has the following set of relations: Animals(ID: integer, Name: string, Owner: integer, DateAdmitted: date, Type:string) Adopter(PID: integer, Name: string, Address: string) Adoption(AnimalID:integer, PID:integer, AdoptDate: date, chipno: integer) where the type of each attribute is given following the attribute (e.g., ID: integer), and the primary keys are underlined. Furthermore, (a) Animals stores information about the animals. Each is given an ID, and their names (attribute Name) together with the PID of their owners (attribute Owner) if they have been adopted, and their date of admission is recorded (so Owner is a foreign key to Adopter.PID). Type refers to the type of animal (dog, cat, etc). Note that this is a historical table, so it keeps track of all admitted animals, even if some of them have been adopted. (b) Adopter is the relation that holds information about animal adopters. The PID of the person who has adopted an animal is recorded as well as his/her Name and Address. (c) Adoption.AnimalID refers to Animals.ID and has the same type. Similarly, Adoption.PID refers to Adopter. PID and has the same type. Attribute chipno stores the number on the microchip that is implanted on the animal for tracking.
Exercises Formulate the following queries in SQL: 1) For each animal type, list the animal type and total number of adoptions on 14 June 2015. 2) For each adopter who has made at least two adoptions, list their names and addresses.
Outline Nested Queries Correlated Nested Queries EXISTS and UNIQUE functions Exercises
Queries that require existing values in the DB to be fetched and then used in a comparison condition Select-from-where block inside another SQL query Example Nested Queries Give biographical information for directors of profitable movies SELECT name, birth, city FROM ( SELECT director FROM Film WHERE gross > budget) AS Profitable, Person WHERE director = name
Nested queries can also appear in the WHERE clause or in the SELECT clause Example Find the film with the highest budget Nested Queries SELECT * FROM Film WHERE budget = (SELECT MAX(budget) FROM Film);
Comparison operator IN Compares a value with a set of values and evaluates to TRUE if the value is one of the elements in the set Example Query 4: Make a list of all project numbers for projects that involve an employee whose last a e is S ith, either as a worker or as a a ager of the depart e t that co trols the project. SELECT DISTINCT Pnumber FROM PROJECT WHERE Pnumber IN (SELECT Pnumber FROM PROJECT, DEPARTMENT, EMPLOYEE WHERE Dnum=Dnumber AND OR Pnumber IN (SELECT Pno FROM WORKS_ON, EMPLOYEE Nested Queries Mgr_ss =Ss AND L a e= S ith ) WHERE Ess =Ss AND L a e= S ith );
Nested Queries Tuples of values can also be used in comparisons with IN Explicit set of values can also be used, instead of a nested query SELECT DISTINCT Essn FROM WORKS_ON WHERE Pno IN (1,2,3)
Nested Queries Other comparison operators can be used ANY (or SOME) operator returns TRUE if the value is equal to some value in the set Equivalent to IN >, >=, <,<=, <> can also be used with ANY (SOME) ALL operator every value in the set
Correlated Nested Queries Major research Correlated query when a nested query references some attribute of a relation declared in the outer query Evaluated once for each tuple Easiest to understand (and write correctly) if all column names are qualified by their relation names
The EXISTS and UNIQUE Functions in SQL Major research Boolean functions that return TRUE or FALSE Used in the WHERE clause condition EXISTS function checks if a result of a nested query is empty or not NOT EXISTS Example Retrieve the names of employees who have no dependents SELECT Fname, Lname FROM EMPLOYEE WHERE NOT EXISTS (SELECT * FROM DEPENDENT WHERE Ssn=Essn); UNIQUE function checks if there are duplicate tuples in the result of a query
Exercises 1. Consider the following relations: Emp(eno, ename, title, city) Proj(pno, pname, budget, city) Works(eno, pno, resp, dur) Pay(title, salary) where the primary keys are underlined, and Emp.title is a foreign key to Pay.title, Works.eno is a foreign key to Emp.eno, and Works.pno is a foreign key to Proj.pno. Write the following SQL query: 1) Retrieve the employee number and responsibility of all persons assigned to any project for which more than 2 persons share the same responsibility for that project.
Exercises 2. A database for an organization that shelters animals, and people can go and adopt animals that they shelter, has the following set of relations: Animals(ID: integer, Name: string, Owner: integer, DateAdmitted: date, Type:string) Adopter(PID: integer, Name: string, Address: string) Adoption(AnimalID:integer, PID:integer, AdoptDate: date, chipno: integer) where the type of each attribute is given following the attribute (e.g., ID: integer), and the primary keys are underlined. Furthermore, Owner is a foreign key to Adopter.PID; Adoption.AnimalID refers to Animals.ID; Adoption.PID refers to Adopter. PID and has the same type. Formulate the following query in SQL: 1) Find the names of adopters who have adopted every type of animal available at the animal shelter.