Giter Club home page Giter Club logo

db-revision's Introduction

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
  )
);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.