1.
SQL Queries
Aim:
Create an employee database and give an SQL query for the following.
Employee (empname, street, city)
Works (empname, compname, salary)
Company (compname, city)
Manager (empname, managername)
a) Find the names of all employees in Database who lives in same cities as the
companies for which they work.
b) Find the name, street address, and cities of residence of all employees who work
for FBC and earn more than $10,000.
c) Find all employees who live in the same cities, as do their managers.
d) Find all employees who earn more than every employee of SBC.
e) Find the company that has most employees.
f) Find the Company that has the smallest payroll.
g) Modify the database so that jones now lives in Newtown.
h) Give all employees of FBC a 10% raise.
i) Delete all tuples in Works relation for employees of SBC.
j) Give all managers of FBC a 10% raise unless the salary becomes greater than
$100,000 in such cases give only a 3% raise.
CREATE TABLE Work2(empname varchar(20), compname varchar(20), salary number(6) )
CREATE TABLE Company3(compname varchar(20), city varchar(20) )
CREATE TABLE Manager4(empname varchar(20), manager4name varchar(20))
INSERT ALL
INTO Employee0 VALUES ('Madhavan','Nehru street','Newtown')
INTO Employee0 VALUES ('Vimal','Bose street','Skytree')
INTO Employee0 VALUES ('Prem','Bharthi street','Newtown')
INTO Employee0 VALUES ('Jones','Krishna street','Newyork')
INTO Employee0 VALUES ('Gokul','Gandhi street','Newtown')
SELECT * FROM dual
INSERT ALL
INTO Work2 VALUES ('Gokul','FBC',15000)
INTO Work2 VALUES ('Madhavan','SBC',25000)
INTO Work2 VALUES ('Vimal','FBC',19500)
INTO Work2 VALUES ('Prem','FBC',30000)
INTO Work2 VALUES ('Jones','SBC',9000)
select * from dual
INSERT INTO Company3 VALUES ('FBC','Newtown')
INSERT INTO Company3 VALUES ('SBC','Skytree')
INSERT INTO Manager4 VALUES ('Gokul','Prem')
INSERT INTO Manager4 VALUES ('Vimal','Prem')
INSERT INTO Manager4 VALUES ('Jones','Madhavan')
DISPLAYING EMPLOYEE0
SELECT * FROM EMPLOYEE0
DISPLAYING WORK2
SELECT * FROM WORK2
DISPLAYING COMPANY3
SELECT * FROM COMPANY3
DISPLAYING MANAGER4
SELECT * FROM MANAGER4
A) Find the names of all employees in Database who lives in same cities as the
companies for which they work.
QUERY:
SELECT e.empname FROM Employee0 e, Work2 w, Company3 c WHERE e.empname=w.empname
AND w.compname=c.compname AND e.city=c.city
OUTPUT:
b) Find the name, street address, and cities of residence of all employees who work
for FBC and earn more than $10,000.
QUERY:
SELECT e.* FROM employee0 e, Work2 w WHERE e.empname = w.empname AND
w.compname='FBC' AND salary>10000
OUTPUT:
c) Find all employees who live in the same cities, as do their managers.
QUERY:
SELECT e.empname FROM Employee0 e, Employee0 f, Manager4 m WHERE e.empname =
m.managername AND f.empname = m.empname AND e.city = f.city
OUTPUT:
d) Find all employees who earn more than every employee of SBC.
QUERY:
SELECT empname FROM Work2 WHERE Salary > ( SELECT MAX (salary) FROM Work2 WHERE
compname = 'SBC')
OUTPUT:
e) Find the company that has most employees.
QUERY:
SELECT compname, c FROM (SELECT Compname, Count(empname) AS c FROM Work2 GROUP
BY Compname ORDER BY c ASC ) WHERE rownum = 1
OUTPUT:
f) Find the Company that has the smallest payroll.
QUERY:
UPDATE Employee0 SET City= 'Newtown' WHERE empname = 'Jones'
OUTPUT:
g) Modify the database so that jones now lives in Newtown.
QUERY:
UPDATE Work2 SET salary = salary*0.10 WHERE compname 'FBC'
OUTPUT:
h) Give all employees of FBC a 10% raise.
QUERY:
SELECT compname, c as count from (SELECT compname, count (empname) as c from work2
GROUP BY compname ORDER BY c ASC) WHERE rownum = 1
OUTPUT:
i) Delete all tuples in Works relation for employees of SBC.
QUERY:
DELETE FROM WORK2 WHERE COMPNAME
OUTPUT:
j) Give all managers of FBC a 10% raise unless the salary becomes greater than
$100,000 in such cases give only a 3% raise.
QUERY:
UPDATE Work2 SET salary = salary + ( salary * CASE WHEN salary < 100000 THEN 0.10 ELSE 0.03
END ) WHERE empname IN( SELECT w.empname FROM manager4 m, work2 w WHERE
m.managername = w.empname AND w.compname ='FBC' )
OUTPUT: