This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
1)Find the eid’s of pilots certified for some boeing aircraft.
SQL> select eid
2 from aircraft a,certified c
3 where a.aid=c.aid and
4 aname='boeing';
Output:
EID
---------
1
2)Find the names of pilots certified for some boeing aircraft.
SQL> select ename
2 from aircraft a,certified c,employees e
3 where c.aid=a.aid and
4 e.eid=c.eid and aname='boeing';
Output:
ENAME
--------------------
vidita
3)Find the aid of all aircraft that can be used on nonstop flights from bonn to madras.
SQL> select a.aid
2 from aircraft a,flights f
3 where f.frm='bonn' and f.too='madras' and
4 f.distance < a.cruisingrange;
Output:
AID
---------
101
102
103
104
105
4)Find the names of pilots who can obtain planes with the range greater than 3000 miles but are not certified for any boeing aircraft.
SQL> select e.ename
2 from employees e,aircraft a,certified c
3 where e.eid=c.eid and a.aid=c.aid and
4 cruisingrange>3000 and
5 e.eid not in(select c1.eid
6 from certified c1,aircraft a1
7 where c1.aid=a1.aid and
8 aname='boeing');
Output:
ENAME
--------------------
varada
kali
ruchi
5)Find the eid’s of employees who make the highest salary.
SQL> select e1.eid from employees e1
2 where e1.salary=(select max(e2.salary)from employees e2);
Output:
EID
---------
1
6)Find the eid’s of employees who make the second highest salary.
SQL> select e.eid from employees e
2 where e.salary=(select max(e1.salary)from employees e1
3 where e1.salary !=(select max(e2.salary)from employees e2));
Output:
EID
------------
2
7)Find the eid of employees who are certified for the largest number of aircraft.
SQL> create view temp as
2 (select c.eid as eid,count(c.aid) as cnt
3 from certified c
4 group by(c.eid));
View created.
SQL> select temp.eid
2 from temp where temp.cnt=(select max(cnt) from temp);
Output:
EID
-----------
1
8)Find the eid’s of employees who are certified for exactly 3 aircraft.
SQL> select temp.eid from(select c.eid as eid,count(c.aid) as cnt
2 from certified c
3 group by(c.eid))temp
4 where temp.cnt=3;
Output:
EID
---------
1
9)Find the total amount paid to employees as salaries.
SQL> select sum(salary) as sum_sal
2 from employees;
Output:
SUM_SAL
---------
1210000
10)Identify the flights that can be piloted by every pilot whose salary is more than $100000.
SQL> select distinct e.ename
2 from aircraft a,certified c,employees e,flights f
3 where a.aid=c.aid and e.eid=c.eid and
4 distance<cruisingrange and salary>100000;
Output:
ENAME
--------------------
vidita
Practical No. 5
AIM: Creation of Employee database
Consider the employee database of Figure (a), where the primary keys are underlined.
Give an expression in SQL for each of the following queries.
employee (employee-name, street, city)
works (employee-name, company-name, salary)
company (company-name, city)
manages (employee-name, manager-name)
Figure (a) Employee database.
a. Find the names of all employees who work for First Bank Corporation.
select employee-name
from works
where company-name = ’First Bank Corporation’
b. Find the names and cities of residence of all employees who work for First
Bank Corporation.
select e.employee-name, city
from employee e, works w
where w.company-name = ’First Bank Corporation’ and
w.employee-name = e.employee-name
c. Find the names, street address, and cities of residence of all employees who
work for First Bank Corporation and earn more than $10,000.
select *
from employee
where employee-name in
(select employee-name
from works
where company-name = ’First Bank Corporation’ and salary > 10000)
d. Find all employees in the database who live in the same cities as the companies
for which they work.
select e.employee-name
from employee e, works w, company c
where e.employee-name = w.employee-name and e.city = c.city and
w.company -name = c.company –name
e. Find all employees in the database who live in the same cities and on the
same streets as do their managers.
select P.employee-name
from employee P, employee R, manages M
where P.employee-name = M.employee-name and
M.manager-name = R.employee-name and
P.street = R.street and P.city = R.city
f. Find all employees in the database who do not work for First Bank Corporation.
select employee-name
from works
where company-name != ’First Bank Corporation’
OR
select employee-name
from employee
where employee-name not in
(select employee-name
from works
where company-name = ’First Bank Corporation’)
g. Find all employees in the database who earn more than every employee of
Small Bank Corporation.
select employee-name
from works
where salary > all
(select salary
from works
where company-name = ’Small Bank Corporation’)
h. Find all employees who earn more than the average salary of all employees
of their company.
select employee-name
from works T
where salary > (select avg (salary)
from works S
where T.company-name = S.company-name)
i. Find the company that has the most employees.
select company-name
from works
group by company-name
having count (distinct employee-name) >= all
(select count (distinct employee-name)
from works
group by company-name)
j. Find the company that has the smallest payroll.
select company-name
from works
group by company-name
having sum (salary) <= all (select sum (salary)
from works
group by company-name)
k. Find those companies whose employees earn a higher salary, on average,
than the average salary at First Bank Corporation.
select company-name
from works
group by company-name
having avg (salary) > (select avg (salary)
from works
where company-name = ’First Bank Corporation’)
l. Modify the database so that Jones now lives in Newtown.
update employee
set city = ’Newton’
where person-name = ’Jones’
m. Give all employees of First Bank Corporation a 10-percent raise.
update works
set salary = salary * 1.1
where company-name = ’First Bank Corporation’
n. Give all managers of First Bank Corporation a 10-percent raise.
update works
set salary = salary * 1.1
where employee-name in (select manager-name
from manages)
and company-name = ’First Bank Corporation’
o. Delete all tuples in the works relation for employees of Small Bank Corporation.
delete works
where company-name = ’Small Bank Corporation’
Practical No. 6
AIM: CREATING TRIGGERS
Example 1
Create a transparent audit system for a table CUST_MSTR. The system must keep track of the records that are being deleted or updated. The functionality being when a record is deleted or modified the original record details and the date of operation are stored in the audit table, then the delete or update operation is allowed to go through.
The table definition for audit system is given below:
Write a database trigger on the TRANS_MSTR that checks the following:
The account number for which the transaction is being performed is a valid account number The Transaction Amount is not zero and is positive and In case of a withdrawal the amount does not exceed the current balance for that account
number
CREATE OR REPLACE TRIGGER TRANS_CHECK
BEFORE INSERT ON TRANS_MSTR FOR EACH ROW
DECLARE
v_CNT_ACCT_NO VARCHAR2(10);
v_CURBAL NUMBER(10);
BEGIN
SELECT COUNT(ACCT_NO) INTO v_CNT_ACCT_NO
FROM ACCT_MSTR WHERE ACCT_NO = :new.ACCT_NO;
IF v_CNT_ACCT_NO = 0 THEN
RAISE_APPLICATlON_ERROR( -20000,'The Account number is invalid.');
END IF;
IF :new.AMT <= 0 THEN
RAISE_APPLICATlON_ERROR(-20001,'The Transaction amount cannot' be negative or zero.');
END IF;
SELECT CURBAL INTO v_CURBAL
FROM ACCT_MSTR WHERE ACCT_NO= :new.ACCT_NO;
IF v_CURBAL < :new.AMT AND :new.DR_CR = 'W' THEN
RAISE_APPLICATION_ERROR(-20002,'The amount of withdrawal cannot exceed the balance held in the account.');
END IF;
END;
Output:
Trigger created.
Example 3
Generating Primary Key Using Sequences
Sequence Creation
CREATE SEQUENCE CUST_SEQ INCREMENT BY 1 START WITH 1;
Output:
Sequence created.
Database Trigger Creation
CREATE OR REPLACE TRIGGER CUST_NO_GENERATION
BEFORE INSERT ON CUST_MSTR FOR EACH ROW
DECLARE
PRIMARY_KEY_VALUE VARCHAR2(10);
BEGIN
SELECT ‘C’||TO_CHAR(CUST_SEQ.NEXTVAL) INTO PRIMARY_KEY_VALUE FROM DUAL;