Escolar Documentos
Profissional Documentos
Cultura Documentos
Assignments Day 3
1. Create a database called COMPANY consisting of two tables - EMP &
DEPT
1
Assignment Solutions for RDBMS Punjabi University Patiala
2
Assignment Solutions for RDBMS Punjabi University Patiala
4. List the names of employees whose employee numbers are 7369, 7521, 7839,
7934, 7788.
6. List employee names for those who have joined between 30 June and 31
Dec. 81.
SQL> select ename from emp where hiredate between 30-jun-81 and 31-
dec-81;
8. List the names of employees who are not eligible for commission.
9. List the name and designation of the employee who does not report to
anybody.
3
Assignment Solutions for RDBMS Punjabi University Patiala
SQL> select ename from emp where ename like S% and like %S;
13. List names of employees whose names have i as the second character.
17. List the maximum, minimum and average salary in the company.
4
Assignment Solutions for RDBMS Punjabi University Patiala
Assignment Day 4
2. List name, salary and PF amount of all employees. (PF is calculated as 10%
of basic salary)
3. List names of employees who are more than 2 years old in the company.
SQL>
4. List the employee details in the ascending order of their basic salary.
5. List the employee name and hire date in the descending order of the hire
date.
6. List employee name, salary, PF, HRA, DA and gross; order the results in the
ascending order of gross. HRA is 50% of the salary and DA is 30% of the
salary.
5
Assignment Solutions for RDBMS Punjabi University Patiala
8. List the department number and total salary payable in each department.
9. List the jobs and number of employees in each job. The result should be in
the descending order of the number of employees.
10. List the total salary, maximum and minimum salary and average salary of
the employees jobwise.
11. List the total salary, maximum and minimum salary and average salary of
the employees, for department 20.
12. List the total salary, maximum and minimum salary and average salary of
the employees jobwise, for department 20 and display only those rows
having an average salary > 1000
6
Assignment Solutions for RDBMS Punjabi University Patiala
The significance of an SPJ record is that the specified supplier supplies the
specified part to the specified project in the specified quantity (and the
combination S#-P#-J# uniquely identifies such a record).
SQL> select S#,P#,J# from S,P,J where S.city = P.city and P.city = J.city and
J.city = S.city and (S#,P#,J#) in (select S#,P#,J# from SPJ);
5. Get al S#, P#, J# triples such that they are not all co-located.
SQL> select S#,P#,J# from S,P,J where (S.city <> J.city or J.city <> P.city or
S.city <> P.city) and (S#,P#,J#) in (select S#,P#,J# from SPJ);
SQL> select distinct (P#) from SPJ where S# in (select S# from A where city =
London);
7. Get all pairs of cities such that a supplier in the first city supplies to a Project
in the second city.
7
Assignment Solutions for RDBMS Punjabi University Patiala
SQL> select S.city,J.city from S,J where S.city <> J.city and (S#,J#) in (select
S#,J# from SPJ);
8. Get J# for projects supplied by at least one supplier not in the same city.
SQL> select distinct (J#) from J where exist (select S# from S where S.city <>
J.city and (J#,S#) in (select J#,S# from SPJ));
9. Get all pairs of part numbers such that some supplier supplies both the
indicated parts.
SQL> select SPJ.P#,P.P# from SPJ, P where P.P# <> SPJ.P# and (S#,P.P#) in
(select S#,P# from SPJ) group by (SPJ.P#,P.P#);
11. For each part supplied to a project, get the P#, J# and corresponding total
quantity.
12. Get P# of parts supplied to some project in an average quantity > 320.
SQL> select Pname from P where P# in (select P# from SPJ where S# = S1);
SQL> select colour from P where P# in (select P# from SPJ where S# = S1);
15. Get J# for projects using at least one part available from supplier S1.
8
Assignment Solutions for RDBMS Punjabi University Patiala
16. Get supplier numbers for suppliers supplying at least one part supplied by at
least one supplier who supplies at least one red part.
17. Get supplier numbers for suppliers with a status lower than that of supplier
S1.
SQL> select S# from S where status < (select status from S where S# = S!);
18. Get project numbers for projects not supplied with any red part by any
London supplier.
SQL> select J# from J where J# in (select J# from SPJ where P# not in (select
P# from P where colour = red)) and S# in ( select S# from S where city
=London);
9
Assignment Solutions for RDBMS Punjabi University Patiala
Assignment Day 5
1) Write the SQL commands to create a database schema for the following
relational schema:
1. What are the names of customers who have sent packages (shipments) to
Sioux City?
2. To what destinations have companies with revenue less than $1 million sent
packages?
3. What are the names and populations of cities that have received shipments
weighing over 100 pounds?
10
Assignment Solutions for RDBMS Punjabi University Patiala
4. Who are the customers having over $5 million in annual revenue who have
sent shipments weighing less than 1 pound?
5. Who are the customers having over $5 million in annual revenue who have
sent shipments weighing less than 1 pound or have sent a shipment to San
Francisco?
6. Who are the drivers who have delivered shipments for customers with
annual revenue over $20 million to cities with populations over 1 million?
7. List the cities that have received shipments from customers having over $15
million in annual revenue.
8. List the names of drivers who have delivered shipments weighing over 100
pounds.
9. List the name and annual revenue of customers who have sent shipments
weighing over 100 pounds.
11
Assignment Solutions for RDBMS Punjabi University Patiala
10. List the name and annual revenue of customers whose shipments have been
delivered by truck driver Jensen.
12. List cities that have received shipments from every customer.
SQL> select city_name from city where (select count (distinct ( cust_id)) from
shipment where designation = city.city_name) >= (select count (*) from
customer);
SQL> select driver_name from truck where truck_# in (select truck_# from
truck where (select count(distinct(designation)) from shipment where
shipment.truck_# = truck.truck#) >= (select count (*) from city));
14. Customers who are manufacturers or have sent a package to St. Louis.
SQL> select city_name from city where population > 1000000 and city_name
in (select designation from shipment where weight = 100 and cust_id = 311);
12
Assignment Solutions for RDBMS Punjabi University Patiala
16. Trucks driven by Jake Stinson which have never delivered a shipment to
Denver.
SQL> select truck_# from truck where driver_name = Jake Stinson and
truck_# in (select truck_# from shipment where designation <> Denver);
17. Customers with annual revenue over $10 million which have sent packages
under 1 pound to cities with population less than 10,000.
a. Which drivers have taken shipments to Los Angeles for customers with
revenue over $5 million?
b. What are the populations of cities which have received shipments from
customers with revenue between $1 million and $5 million?
c. Which drivers have taken shipments to cities for customers with revenue
under $1 million, and what are the populations of those cities?
13
Assignment Solutions for RDBMS Punjabi University Patiala
14
Assignment Solutions for RDBMS Punjabi University Patiala
PL/SQL
PL/SQL Blocks
Declare
A number:=&enter_A;
B number:=&enter_B;
Begin
If A>B then
Dbms_output.put_line(A is greater);
Else
Dbms_output.put_line(B is greater);
End if;
End;
15
Assignment Solutions for RDBMS Punjabi University Patiala
Declare
A number:=&enter_A;
B number:=&enter_B;
C number:=&enter_C;
Begin
If A>B then
If A>C then
Dbms_output.put_line(A is greatest);
Else
Dbms_output.put_line(C is greatest);
End if;
Else
If B>C then
Dbms_output.put_line(B is greater);
Else
Dbms_output.put_line(C is greater);
End if;
End if;
End;
16
Assignment Solutions for RDBMS Punjabi University Patiala
Declare
A number:=&A;
B number:=&B;
C number;
X number;
Begin
X:=&enter_choice;
If X=1 then
C:=A+B;
C:=A-B;
C:=A*B;
C:=A/B;
Else
End if;
Dbms_output.put_line(Result is||C);
End;
17
Assignment Solutions for RDBMS Punjabi University Patiala
Declare
Var1 number:=1;
n number:=&enter_n;
Begin
Loop
Dbms_output.put_line(var1);
Var1:=var1+1;
End loop;
End;
18
Assignment Solutions for RDBMS Punjabi University Patiala
Declare
Table_of number:=&enter_tableof;
Count number:=1;
Result number;
Begin
While count<=10
Loop
Result:=table_of *count;
Dbms_output.put_line(table_of||*||count||=||result);
Count:=count+1;
End loop;
End;
19
Assignment Solutions for RDBMS Punjabi University Patiala
Cursor management
1. A PL/SQL code to display a message to check whether the record is deleted
or not.
Begin
If sql%notfound then
Else
Dbms_output.put_line(record deleted);
End if;
End;
20
Assignment Solutions for RDBMS Punjabi University Patiala
Declare
n number;
Begin
N:=sql%rowcount;
End;
21
Assignment Solutions for RDBMS Punjabi University Patiala
Declare
Rec c1%rowtype;
Begin
Open c1;
Loop
Dbms_output.put_line(empno||rec.empno);
Dbms_output.put_line(ename||rec.ename);
Dbms_output.put_line(job||rec.job);
End loop;
Close c1;
End;
22
Assignment Solutions for RDBMS Punjabi University Patiala
4. A PL/SQL code to display the employee number and name of top 5 highest
paid employees.
Declare
Empname emp.ename%type;
Empsal emp.sal%type;
Cursor temp1 is select ename, sal from emp order by sal desc;
Begin
Open temp1;
Loop
Dbms_output.put_line(empname||empsal);
End loop;
Close temp1;
End;
23
Assignment Solutions for RDBMS Punjabi University Patiala
5. A PL/SQL code to calculate the total salary of first n records of emp table.
The value of n is passed to cursor as parameter.
Declare
e number(5);
s number(5):=0;
Begin
Open c1(&n);
Loop
Fetch c1 into e;
S:=s+e;
End loop;
Dbms_output.put_line(s);
Close c1;
End;
24
Assignment Solutions for RDBMS Punjabi University Patiala
Triggers
1. To create a trigger for the emp table, which makes the entry in ename
column in uppercase.
Begin
:new.ename:=upper(:new.ename);
End;
25
Assignment Solutions for RDBMS Punjabi University Patiala
2. A trigger on emp table, which shows the old values and new values of
ename after any updation on ename of emp table.
Begin
Dbms_output.put_line(old name:||:old.ename);
Dbms_output.put_line(new name:||:new.ename);
End;
26
Assignment Solutions for RDBMS Punjabi University Patiala
3. A trigger on the emp table, which store the empno and operation in table
auditor for each operation i.e. insert, update and delete.
Begin
If inserting then
End if;
End;
27
Assignment Solutions for RDBMS Punjabi University Patiala
Begin
If rtrim(upper(to_char(sysdate,day)))=sunday then
End if;
End;
28
Assignment Solutions for RDBMS Punjabi University Patiala
Begin
If :new.sal<:old.sal then
End if;
End;
29