williamchenjun / DB-Revision

Revision for KCL Database winter exams. Includes SQL queries, database normalisation and data warehouse design.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Database Sample Questions

Disclaimer: The problems are taken from a mock exam provided by the course. I do not take credit for them.
KCL Module Ref.: 7CCSMDDW.

Q1. Given the relation $R(A,B,C,D,E,F,G,H)$ and the set of functional dependencies

$$\{ A \rightarrow BD,\; DH \rightarrow CF,\; C \rightarrow B,\; BF \rightarrow H,\; H \rightarrow G \}$$

is the functional dependency $AF \rightarrow G$ implied?

Solution: The answer is YES. We can see that

$$\begin{align*} \because A &\rightarrow BD \implies \begin{cases}A \rightarrow B\\A \rightarrow D\end{cases}\\[1em] AF &\rightarrow BF \implies AF \rightarrow H \implies AF \rightarrow G. \end{align*}$$

(The first step is given by the splitting rule. The second step is respectively given by the axiom of augmentation and transitivity).

We are given the following database schema:

  • employee(empid, lname, location, salary, manager)
  • project(projectID, projectName, projectLeader, budget)
  • projectemployees(contractID, empID(FK), projectID(FK), contract_length)

Q2. List the last name of the employees based in London who have the lowest salary. Note that more than one employee might have the lowest salary if they have the same salary.

Solution:

SELECT lname
FROM employee
WHERE salary = (
  SELECT MIN(salary)
  FROM employee
  WHERE location = "London"
);

Q3. Which employees are working on the most projects?

Solution:

SELECT empID, COUNT(projectID) AS NumberOfProjects
FROM projectemployees
GROUP BY empID
HAVING COUNT(projectID) = (
  SELECT MAX(P) 
  FROM (
    SELECT empID, COUNT(projectID) AS P
    FROM projectemployees 
    GROUP BY empID
  )
);

We could also make it better by performing an inner join with the employee table to access more information about the employee:

SELECT employees.empID, employees.lname, employees.fname, COUNT(projectemployees.projectID) AS NumberOfProjects
FROM projectemployees INNER JOIN employees ON projectemployees.empID = employees.empID
GROUP BY projectemployees.empID
HAVING COUNT(projectemployees.projectID) = (
  SELECT MAX(P) 
  FROM (
    SELECT empID, COUNT(projectID) AS P
    FROM projectemployees 
    GROUP BY empID
  )
);

About

Revision for KCL Database winter exams. Includes SQL queries, database normalisation and data warehouse design.

License:GNU General Public License v3.0