0% found this document useful (0 votes)
841 views5 pages

Assignment 2 Key 8.15

The document provides sample SQL queries and their solutions to retrieve information from various databases like employee, project, flight, and library databases. Some key queries include: 1. Finding names and addresses of employees in the Research department. 2. Listing project details for projects located in Stafford. 3. Finding names of employees working on all projects in department 5. 4. Listing projects involving employee Smith. 5. Retrieving names of employees with two or more dependents. The document also discusses how to write SQL queries to retrieve aggregate information like average salaries, count of books owned by branches, flights between cities, and more. Various joins, filters, aggregations are demonstrated.

Uploaded by

coyotesmith2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
841 views5 pages

Assignment 2 Key 8.15

The document provides sample SQL queries and their solutions to retrieve information from various databases like employee, project, flight, and library databases. Some key queries include: 1. Finding names and addresses of employees in the Research department. 2. Listing project details for projects located in Stafford. 3. Finding names of employees working on all projects in department 5. 4. Listing projects involving employee Smith. 5. Retrieving names of employees with two or more dependents. The document also discusses how to write SQL queries to retrieve aggregate information like average salaries, count of books owned by branches, flights between cities, and more. Various joins, filters, aggregations are demonstrated.

Uploaded by

coyotesmith2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Assignment 2 Key

8.15 Show the result of each of the sample queries in Section 8.5 as it would apply
to the database state in Figure 5.6

Q1. Find the name and address of all employees who work for the 'Research' department.

Q2. For every project located in 'Stafford', list the project number, the controlling department
number, and the department manager's last name, address, and birth date.

Q3. Find the names of all employees who work on all the projects controlled by department number
5.

Q4. Make a list of project numbers for projects that involve an employee whose last name is 'Smith'
as a worker or as a manager of the department that controls the project.

Q5. List the names of all employees with two or more dependents.

Q6. List the names of employees who have no dependents.


Q7. List the names of managers who have at least one dependent.

8.16
a. Retrieve the names of all employees in department 5 who work more than 10 hours per week on
the ProductX project.

WORK_X ← (σ PNAME='ProductX' (PROJECT)) ⋈PNUMBER=PNO (WORKS_ON)

WORK_10 ← (EMPLOYEE) ⋈SSN=ESSN (σ HOURS>10 (WORK_X))

RESULT ← π FNAME, LNAME (σ DNO=5 (WORK_10))

b. List the names of all employees who have a dependent with the same first name as themselves.

π FNAME, LNAME ((EMPLOYEE) ⋈SSN=ESSN AND FNAME=DEPENDENT_NAME (DEPENDENT))

c. Find the names of all employees who are directly supervised by ‘Franklin Wong’.

π FNAME, LNAME ((EMPLOYEE) ⋈ SUPERSSN=SSN (π SSN (σ FNAME='Franklin' AND LNAME='Wong' (EMPLOYEE))))

d. For each project, list the project name and the total hours per week (by all employees) spent on
that project.

PROJ_HOURS (PNO, TOTAL_HRS) ← PNO ℑ SUM(HOURS) (WORKS_ON)

RESULT ← π PNAME, TOTAL_HRS ((PROJ_HOURS) ⋈PNO=PNUMBER (PROJECT))

* You can use operations like count, sum, average, max, min etc. Usage: G ℑ Aggregate-function(A), you can
see an example in above solution.
e. Retrieve the names of all employees who work on every project.

PE (PNO, SSN) ← π PNO, ESSN (WORKS_ON)

ALL_PROJECTS(PNO) ← π PNUMBER (PROJECT)

EMPS_ALL_PROJS ← PE ÷ ALL_PROJECTS

RESULT ← π FNAME, LNAME (EMPLOYEE * EMP_ALL_PROJS)

f. Retrieve the names of all employees who do not work on any project.

NON_WORKING ← (π SSN (EMPLOYEE)) – (π ESSN (WORKS_ON))

RESULT ← π FNAME, LNAME (EMPLOYEE * NON_WORKING)

g. For each department, retrieve the department name and the average salary of all employees
working in that department.

π DNAME, AVG ((DNAME ℑ AVG (SALARY) (EMPLOYEE)) ⋈DNO=DNUMBER (DEPARTMENT))

h. Retrieve the average salary of all female employees.

ℑ AVG (SALARY) (σ SEX=’F’ (EMPLOYEE)))


i. Find the names and addresses of all employees who work on at least one project located in Houston
but whose department has no location in Houston.

HOUSTON_PROJS(SSN) ← πESSN (WORKS_ON ⋈PNO=PNUMBER (σPLOCATION='Houston' (PROJECT)))

NO_HOUST_DEPS ← πDNUMBER (DEPARTMENT) - πDNUMBER (σDLOCATION='Houston' (DEPARTMENT))

NO_HOUST_EMP_DEPS ← π SSN (EMPLOYEE ⋈DNO=DNUMBER (NO_HOUST_DEPS))

RESULT ← π FNAME, LNAME, ADDRESS (EMPLOYEE * (HOUSTON_PROJS - NO_HOUST_EMP_DEPS))

j. List the last names of all department managers who have no dependents.

π FNAME, LNAME (EMPLOYEE * (π MGRSSN (DEPARTMENT) - π ESSN (DEPENDENT)))

8.17
a. For each flight, list the flight number, the departure airport for the first leg of the flight, and the
arrival airport for the last leg of the flight.

DEP <- π Flight_number, Dep_airport_code(Flight_number ℑ MIN(Leg_number)(FLIGHT_LEG)) * FLIGHT_LEG

ARR <- π Flight_number, Arr_airport_code(Flight_number ℑ MAX(Leg_number)(FLIGHT_LEG)) * FLIGHT_LEG

RESULT <- DEP * ARR

b. List the flight numbers and weekdays of all flights or flight legs that depart from Houston
Intercontinental Airport (airport code ‘iah’) and arrive in Los Angeles International Airport
(airport code ‘lax’).

π Flight_number, Weekdays ((σDep_airport_code= ‘iah’(FLIGHT_LEG) * σ Arr_airport_code= ‘lax’(FLIGHT_LEG)) *FLIGHT)

c. List the flight number, departure airport code, scheduled departure time, arrival airport code,
scheduled arrival time, and weekdays of all flights or flight legs that depart from some airport in
the city of Houston and arrive at some airport in the city of Los Angeles.

HOUSTON_LA <- σDep_airport_code= ‘iah’(FLIGHT_LEG) * σ Arr_airport_code= ‘lax’(FLIGHT_LEG)

RESULT <- πFlight_number, Dep_airport_code, Sched_dep_time, Arr_airport_code, Sched_arrival_time, Weekday (HOUSTON_LA * FLIGHT)

d. List all fare information for flight number ‘co197’.

σ Flight_number = 'co197' (FARE)


e. Retrieve the number of available seats for flight number ‘co197’ on ‘2009-10-09’.

π Number_of_available_seats (σ Flight_number='CO197' AND Date='1999-10-09'(LEG_INSTANCE))

8.18
a. How many copies of the book titled The Lost Tribe are owned by the library branch whose name
is ‘Sharpstown’?

πNo_of_copies (Book_Copies ∗ (πBranch_id(σBranch_name=’Sharpstown’(Library_Branch))) ∗ πBook_id (σTitle=’The Lost Tribe’(Book)))

b. How many copies of the book titled The Lost Tribe are owned by each library branch?

πBranchID, No_of_copies ((σTitle='The Lost Tribe'(BOOK)) * BOOKCOPIES

c. Retrieve the names of all borrowers who do not have any books checked out.

πName (Borrower ∗ (πCard_no (Borrower) – πCard_no (Book Loans)))

d. For each book that is loaned out from the Sharpstown branch and whose Due_date is today,
retrieve the book title, the borrower’s name, and the borrower’s address.

ST <-- π BranchId (σ BranchName='Sharpstown'(LIBRARY_BRANCH))


BORROWED_ST <-π BookId, CardNo ((σDueDate=today (BOOKLOANS)) * ST)
RESULT <-- π Title, Name, Address (BOOK * BORROWER * BORROWED_ST))

e. For each library branch, retrieve the branch name and the total number of books loaned out
from that branch.

TEMP (BranchId, Total) <-BranchId ℑ COUNT (BookId, CardNo) (BOOK_LOANS)


RESULT <-πBranchName, Total (TEMP * LIBRARY_BRANCH))

f. Retrieve the names, addresses, and number of books checked out for all borrowers who have
more than five books checked out.

TEMP (CardNo, NumofBooks) <- CardNo ℑ COUNT(BookId) (BOOK_LOANS)


RESULT <-πName, Address, NumofBooks ((σNumofBooks > 5(TEMP)) * BORROWER)

g. For each book authored (or coauthored) by Stephen King, retrieve the title and the number of
copies owned by the library branch whose name is Central.

STEPHEN (BookId, Title) <- (σ AuthorName='Stephen King' (BOOK_AUTHORS)) * BOOK


CENTRAL(BranchId) <- σ BranchName='Central' (LIBRARY_BRANCH)
RESULT <-- π Title, No_of_copies (STEPHEN * BOOKCOPIES * CENTRAL)

You might also like