Ass 04
Ass 04
Q2.
-- Create the Database
CREATE DATABASE EmployeeDB;
USE EmployeeDB;
SELECT w1.ID
FROM works w1
WHERE w1.salary > ALL (SELECT w2.salary FROM works w2
WHERE w2.company_name = 'Small Bank Corporation');
-- Query e: Find the name of each company that is located in
every city in which "Small Bank Corporation" is located.
SELECT c1.company_name
FROM company c1
WHERE NOT EXISTS (
SELECT c2.city
FROM company c2
WHERE c2.company_name = 'Small Bank Corporation'
AND c2.city NOT IN (
SELECT c3.city
FROM company c3
WHERE c3.company_name = c1.company_name
)
);
-- Query f: Find the name of the company that has the most
employees.
SELECT w.company_name
FROM works w
GROUP BY w.company_name
ORDER BY COUNT(w.ID) DESC
LIMIT 1;
-- Q3--
-- a. Modify the database so that the employee whose ID is
'12345' now lives in "Newtown".
UPDATE employee
SET city = 'Newtown'
WHERE ID = 12345;
-- b. Give each manager of "First Bank Corporation" a 10
percent raise unless the salary becomes greater than
$100000; in such cases, give only a 3 percent raise.
UPDATE works
SET salary = CASE
WHEN salary * 1.10 <= 100000 THEN salary * 1.10
ELSE salary * 1.03
END
WHERE company_name = 'First Bank Corporation'
AND ID IN (SELECT manager_id FROM manages);
-- Q5
-- Employee Database Queries (Figure 3.19)
USE EmployeeDB;
-- a. Find ID and name of each employee who lives in the
same city as the location of the company for which the
employee works:
SELECT E.ID, E.person_name
FROM employee E
JOIN works W ON E.ID = W.ID
JOIN company C ON W.company_name = C.company_name
WHERE E.city = C.city;
-- b. Find ID and name of each employee who lives in the
same city and on the same street as does her or his manager:
SELECT E.ID, E.person_name
FROM employee E
JOIN manages M ON E.ID = M.ID
JOIN employee EM ON M.manager_id = EM.ID
WHERE E.city = EM.city AND E.street = EM.street;
-- Q6 --
-- Consider the employee database of Figure 3.19.
b. Find the grade point average (GPA) for the above student, that is, the total all courses taken by
the student.
SELECT takes.ID,
(sum(course.credits*gp.points)/NULLIF(sum(course.credits), 0)) as
total_grade_points
FROM takes
JOIN course
ON takes.course_id = course.course_id
JOIN grade_points as gp;
ON gp.grade = takes.grade
WHERE takes.ID = '12345' ;
c. Find the ID and the grade-point average of each student
d. Now consider your answers to the earlier parts of this exercise under the assumption that some
grades might be null. Explain whether your solutions still work and if not provide the versions that
handle nulls properly.
GROUP BY takes.ID;