ASS9
ASS9
Consider the following relations and Draw the ER, EER Diagram, Relational Model
and write the SQL statement for the following queries:
INSERT ALL
INTO WORKS VALUES ('ARKA', 'Google', 100000)
INTO WORKS VALUES ('JOHN', 'Microsoft', 150000)
INTO WORKS VALUES ('MOHIT', 'Amazon', 250000)
INTO WORKS VALUES ('XYZ', 'Axis Bank', 200000)
INTO WORKS VALUES ('ABC', 'Axis Bank', 280000)
SELECT * FROM DUAL;
INSERT ALL
INTO COMPANY VALUES ('Google', 'Mountain View')
INTO COMPANY VALUES ('Microsoft', 'Redmond')
INTO COMPANY VALUES ('Amazon', 'Seattle')
INTO COMPANY VALUES ('Axis Bank', 'Mumbai')
INTO COMPANY VALUES ('Walmart', 'Bentonville')
SELECT * FROM DUAL;
INSERT ALL
INTO MANAGES VALUES ('JOHN', 'ARKA')
INTO MANAGES VALUES ('MOHIT', 'ARKA')
INTO MANAGES VALUES ('ABC', 'MOHIT')
INTO MANAGES VALUES ('XYZ', 'JOHN')
INTO MANAGES VALUES ('ABC', 'JOHN')
SELECT * FROM DUAL;
a)Find the names of all employees who work for Axis Bank.
SELECT PERSONNAME FROM WORKS WHERE COMPANYNAME = 'AXIS
BANK';
b)Find the names and cities of residence of all employees who work for Axis
Bank.
SELECT E.PERSONNAME , E.CITY FROM EMPLOYEE E JOIN WORKS W ON
W.PERSONNAME = E.PERSONNAME WHERE W.COMPANYNAME = 'Axis Bank';
c)Find the names, street addresses, and cities of residence of all employees
who work for Axis Bank and earn more than Rs.30000 per annum.
SELECT E.PERSONNAME , E.STREET , E.CITY FROM EMPLOYEE E JOIN
WORKS W ON W.PERSONNAME = E.PERSONNAME WHERE
W.COMPANYNAME = 'Axis Bank' AND W.SALARY > 30000;
d)Find all employees who live in the same city as the company for which they
work is located.
SELECT E.PERSONNAME FROM EMPLOYEE E JOIN WORKS W ON
W.PERSONNAME = E.PERSONNAME JOIN COMPANY C ON
W.COMPANYNAME = C.COMPANYNAME WHERE C.CITY = E.CITY;
e)Find all employees who live in the same city and on the same street as their
managers.
SELECT E.PERSONNAME FROM EMPLOYEE E
WHERE E.CITY IN (SELECT CITY FROM EMPLOYEE WHERE PERSONNAME IN
(SELECT DISTINCT MANAGERNAME FROM MANAGES))
AND E.STREET IN (SELECT STREET FROM EMPLOYEE WHERE
PERSONNAME IN (SELECT DISTINCT MANAGERNAME FROM MANAGES))
AND E.PERSONNAME NOT IN (SELECT DISTINCT MANAGERNAME FROM
MANAGES);
f)Find all employees in the database who do not work for Axis Bank.
SELECT E.PERSONNAME
FROM EMPLOYEE E
JOIN WORKS W ON E.PERSONNAME = W.PERSONNAME
WHERE W.COMPANYNAME <> 'Axis Bank';
g)Find all employees who earn more than every employee of Axis Bank.
SELECT PERSONNAME FROM WORKS WHERE SALARY > (SELECT
MAX(SALARY) FROM WORKS WHERE COMPANYNAME = 'Axis Bank');
h)Assume that the companies may be located in several cities. Find all
companies located in every city iin which Axis Bank is located.
SELECT DISTINCT COMPANYNAME FROM COMPANY WHERE CITY IN
(SELECT CITY FROM COMPANY WHERE COMPANYNAME = 'Axis Bank') AND
COMPANYNAME <> 'Axis Bank';
i)Find all employees who earn more than the average salary of all employees
of their company.
CREATE TABLE TMPSAL AS SELECT AVG(SALARY) AS AVGSAL ,
COMPANYNAME FROM WORKS GROUP BY COMPANYNAME;
SELECT W.PERSONNAME , W.COMPANYNAME , W.SALARY
FROM WORKS W
JOIN TMPSAL T ON W.COMPANYNAME = T.COMPANYNAME
WHERE W.SALARY > T.AVGSAL;
p) Give all managers in the database a 10 percent raise, unless the salary
would be greater than Rs.300000.In such cases, give only a 3 percent raise.
UPDATE WORKS SET SALARY =
CASE
WHEN SALARY*1.1 <= 300000 THEN SALARY*1.1
ELSE SALARY*1.03
END
WHERE PERSONNAME IN (SELECT DISTINCT MANAGERNAME FROM
MANAGES);
q) Delete all tuples in the works relation for employees of Axis Bank.
DELETE FROM WORKS WHERE COMPANYNAME = 'Axis Bank';