0% found this document useful (0 votes)
45 views45 pages

SQL Nikk

1. The document describes SQL queries and Visual Basic code to manage data in several relational databases describing a university system. Tables are created for students, departments, professors, courses, student majors, course sections, and student enrollments. 2. Records are inserted into the tables through SQL commands and a Visual Basic form is used to manage the works table. 3. Several SQL queries are written to find employee salary increases, employee names based on department and floor, employee names working in multiple departments, and lowest student GPA. Visual Basic code is provided to manage adding, editing, and deleting records in the works table form.

Uploaded by

Muskaan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views45 pages

SQL Nikk

1. The document describes SQL queries and Visual Basic code to manage data in several relational databases describing a university system. Tables are created for students, departments, professors, courses, student majors, course sections, and student enrollments. 2. Records are inserted into the tables through SQL commands and a Visual Basic form is used to manage the works table. 3. Several SQL queries are written to find employee salary increases, employee names based on department and floor, employee names working in multiple departments, and lowest student GPA. Visual Basic code is provided to manage adding, editing, and deleting records in the works table form.

Uploaded by

Muskaan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

UNIVERSITY OF CALCUTTA

PAPER 7B

VB AND SQL PROJECT

BSc Part III Examination

Computer Science Honours

(under 2009 regulations, 1+1+1 system)

REGISTRATION NUMBER:

434-1221-0683-15

ROLL NUMBER:

3434-51-0009

1
INDEX

01 COMPANY DATABASE 4
02 STUDENT DATABASE 13

01 FLIGHTS 26
02 WORKER 31
03 EMPLOYEE 34
04 SHIPMENT 40

2
VB
+
ORACLE

3
Problem statement:-1
a) Write the SQL command to create a database for the following
relational scheme:
empioyee(e_id,e_name,salary)
works(e_id,d_id)
dept(d_id,d_name,manager_id,floor_num)

b) Insert records to the tables describe above through a suitable form.

c) Express the following quarries in "SQL".

i)Find and hike the salary for every employee by 10% for
those who works in toys department.
ii)Find the names of all the employees who works on the
foor(s) where Jane Donald works.
iii)Find the names of all the employees who works on the
2nd floor and top floor and earn salary less than Rs.10,000.
iv) Find the names of all the employees who works in both
the departments X and Y.

SQL> create table employee


2 (e_id varchar(5),
3 e_name varchar(10),
4 salary numeric(9,2),
5 primary key (e_id)
6 );

Table created.

SQL> insert into employee


2 values ('e-06','ajoy','900.50');

1 row created.

Commit complete.

4
SQL> desc employee;
Name Null? Type
----------------------------------------- -------- ----------------
-
E_ID NOT NULL VARCHAR2(5)
E_NAME VARCHAR2(10)
SALARY NUMBER(9,2)

SQL> select * from employee;

E_ID E_NAME SALARY


----- ---------- ----------
e-06 ajoy 900.5
e-07 mahima 10000
e-01 anamika 9000.5
e-02 fana 8000.5
e-03 rajat 9001.5
e-04 sharuk 9080.5
e-05 sandip 7000.5

7 rows selected.

--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

SQL> create table works


2 (e_id varchar(5),
3 d_id varchar(10),
4 primary key (e_id,d_id)
5 );

Table created.

SQL> desc works;


Name Null? Type
----------------------------------------- -------- ----------------
-
E_ID NOT NULL VARCHAR2(5)
D_ID NOT NULL VARCHAR2(10)

The elements of this table is inserted through Visual Basic 6.


SQL> select * from works;

E_ID D_ID
----- ----------
e-01 d-07
e-02 d-04
e-02 d-05
e-03 d-04
e-03 d-05
e-04 d-06
e-05 d-01
e-06 d-02
e-06 d-03

5
9 rows selected.

--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

SQL> create table dept


2 (d_id varchar(5),
3 d_name varchar(10),
4 mng_id varchar(10),
5 floor_num integer,
6 primary key (d_id)
7 );

Table created.

SQL>
SQL> insert into dept
2 values ('d-01','system','e-03','3');

1 row created.

Commit complete.

SQL> desc dept;


Name Null? Type
----------------------------------------- -------- ----------------
-
D_ID NOT NULL VARCHAR2(5)
D_NAME VARCHAR2(10)
MNG_ID VARCHAR2(10)
FLOOR_NUM NUMBER(38)

SQL> select * from dept;

D_ID D_NAME MNG_ID FLOOR_NUM


----- ---------- ---------- ----------
d-01 system e-03 3
d-02 sells e-03 4
d-03 toy e-03 3
d-04 x e-02 2
d-05 y e-02 4
d-06 manage e-04 4
d-07 purches e-04 3

7 rows selected.

SQL> --~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--SQL statement:-
--~~~~~~~~~~~~~~
SQL> select salary*1.1 from employee
2 where e_id in
3 (select e_id from works
4 where d_id in(select d_id from dept

6
5 where d_name ='toy'));

--output:-
--~~~~~~~
SALARY*1.1
----------
990.55

--SQL statement:-
--~~~~~~~~~~~~~~
SQL> select e_name from employee
2 where e_id in
3 (select e_id from works
4 where d_id in
5 (select d_id from dept
6 where floor_num in
7 (select floor_num from dept
8 where d_id in
9 (select d_id from works
10 where e_id in
11 (select e_id from employee
12 where e_name = 'sharuk')))));

--output:-
--~~~~~~~
E_NAME
----------
fana
rajat
sharuk
ajoy

--SQL statement:-
--~~~~~~~~~~~~~~
SQL> select e_name from employee
2 where e_id in((select e_id from works
3 where d_id in(select d_id from dept
4 where d_name='x'))
5 intersect
6 (select e_id from works
7 where d_id in(select d_id from dept
8 where d_name='y')));

--output:-
--~~~~~~~
E_NAME
----------
fana
rajat

--SQL statement:-

7
--~~~~~~~~~~~~~~
SQL> select e_name from employee
2 where salary<10000 and e_id in
3 (select e_id from works
4 where d_id in
5 (select d_id from dept
6 where floor_num in(2,4)));

--output:-
--~~~~~~~
E_NAME
----------
ajoy
fana
rajat
sharuk

Visual Basic Form:-

Works table form using VB6

8
Adding Data:

Before Add

After Add

9
Editing Data:

Before Edit

After Edit

10
Deleting Data:

Before delete

After Delete

11
Visual Basic Code:

Private Sub ad_Click()

Adodc1.Recordset.AddNew

pname.Text = ""

dname.Text = ""

End Sub

Private Sub dlt_Click()

Adodc1.Recordset.Delete

End Sub

Private Sub ed_Click()

Adodc1.Recordset.Update

End Sub

Private Sub Form_Load()

End Sub

Private Sub nxt_Click()

Adodc1.Recordset.MoveNext

End Sub

Private Sub prv_Click()

Adodc1.Recordset.MovePrevious

End Sub

Private Sub sav_Click()

Adodc1.Recordset.Update

End Sub

12
Problem statement:-2

a) Write the SQL command to create a database for the following


relational scheme:
stud(sid,sname,sex,age,year,GPA)
sdept(dname,no_phd)
proff(pname,dname)
course(dname,cno,cname)
major(dname,sid)
section(dname,cno,secno,pname)
enroll(sid,dname,cno,secno,grade)

b) Insert records to the tables describe above through a suitable form.

c) Express the following quarries in "SQL".

i)Find the names of professors who work in departments


that have fewer than 5 phd students.
ii)Find the name of students with lowest GPA.

SQL>
SQL> create table stud
2 (sid varchar(5),
3 sname varchar(10),
4 sex varchar(7),
5 year varchar(5),
6 age integer,
7 gpa varchar(10),
8 primary key (sid)
9 );

Table created.

SQL> desc stud;


Name Null? Type
----------------------------------------- -------- ----------------
-

13
SID NOT NULL VARCHAR2(5)
SNAME VARCHAR2(10)
SEX VARCHAR2(7)
YEAR VARCHAR2(5)
AGE NUMBER(38)
GPA VARCHAR2(10)

SQL>
SQL> insert into stud values('s_1','raj','male','2009','12','5');

1 row created.

Commit complete.

Commit complete.
SQL> select * from stud;

SID SNAME SEX YEAR AGE GPA


----- ---------- ------- ----- ---------- ----------
s_1 raj male 2009 12 5
s_2 rathindra male 2008 15 8
s_3 ishita female 2007 15 7
s_4 aditi female 2008 14 8
s_5 anik male 2009 18 7
s_6 raja male 2009 16 9
s_7 rana male 2008 15 7
s_8 shiksa female 2007 12 5
s_9 amita female 2008 13 6
s_10 arnab male 2009 15 7

10 rows selected.

SQL> create table sdept


2 (
3 dname varchar(10),
4 no_phd integer,
5 primary key (dname)
6 );

Table created.

SQL> desc sdept;

Name Null? Type


----------------------------------------- -------- ----------------
-
DNAME NOT NULL VARCHAR2(10)
NO_PHD NUMBER(38)

SQL> insert into sdept values('comp.sc','8');

1 row created.

Commit complete.

14
SQL> select * from sdept;

DNAME NO_PHD
---------- ----------
comp.sc 8
physics 4
chemistry 6
math 5
zoology 2

SQL>--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SQL> create table proff
2 (
3 pname varchar(10),
4 dname varchar(10),
5 primary key (pname),
6 foreign key (dname) references sdept(dname) on delete cascade
7 );

Table created.

SQL> desc proff;


Name Null? Type
----------------------------------------- -------- ----------------
-
PNAME NOT NULL VARCHAR2(10)
DNAME VARCHAR2(10)

SQL> select * from proff;

PNAME DNAME
---------- ----------
a.sarkar comp.sc
s.ghos comp.sc
s.kanrar physics
d.kar physics
m.mukharji chemistry
a.samanta chemistry
n.mondol math
c.mitra math
s.dutta zoology
p.sarkar zoology

10 rows selected.

SQL> --
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SQL> create table course
2 (
3 cno varchar(5),
4 dname varchar(10),
5 cname varchar(10),
6 primary key (cno),
7 foreign key (dname) references sdept(dname)

15
8 on delete cascade
9 );

Table created.

SQL> desc course;


Name Null? Type
----------------------------------------- -------- ----------------
-
CNO NOT NULL VARCHAR2(5)
DNAME VARCHAR2(10)
CNAME VARCHAR2(10)

SQL> insert into course values('c_3','math','calculas');

1 row created.

Commit complete.
SQL> select * from course;

CNO DNAME CNAME


----- ---------- ----------
c_3 math calculas
c_4 math geometry
c_1 comp.sc dbms
c_2 comp.sc networking
c_5 physics optics
c_6 physics dynamics
c_7 chemistry organic
c_8 zoology mamals

8 rows selected.

SQL> --
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

SQL> create table major


2 (
3 dname varchar(10),
4 sid varchar(5),
5 primary key (dname,sid)
6 );

Table created.

SQL> desc major;


Name Null? Type
----------------------------------------- -------- ----------------
-
DNAME NOT NULL VARCHAR2(10)

16
SID NOT NULL VARCHAR2(5)

SQL> create table section


2 (
3 secno varchar(5),
4 cno varchar(5),
5 dname varchar(10),
6 pname varchar(10),
7 primary key (secno),
8 foreign key (cno) references course(cno) on delete cascade,
9 foreign key (dname) references sdept(dname) on delete
cascade,
10 foreign key (pname) references proff(pname) on delete
cascade
11 );

Table created.

SQL> insert into major values('zoology','s_8');

1 row created.

Commit complete.
SQL> select * from major;

DNAME SID
---------- -----
zoology s_8
zoology s_5
comp.sc s_2
comp.sc s_1
math s_3
math s_6
chemistry s_5
chemistry s_9
physics s_10
physics s_7

10 rows selected.
SQL> --~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SQL> desc section;
Name Null? Type
----------------------------------------- -------- ----------------
-
SECNO NOT NULL VARCHAR2(5)
CNO VARCHAR2(5)
DNAME VARCHAR2(10)
PNAME VARCHAR2(10)

SQL> insert into section values('sec_1','c_1','comp.sc','a.sarkar');

1 row created.

Commit complete.

17
SQL> select * from section;

SECNO CNO DNAME PNAME


----- ----- ---------- ----------
sec_1 c_1 comp.sc a.sarkar
sec_2 c_3 math c.mitra
sec_3 c_2 comp.sc s.ghos
sec_4 c_5 physics d.kar
sec_5 c_8 zoology s.dutta
sec_6 c_6 physics m.mukharji
sec_7 c_4 math n.mondol
sec_8 c_7 chemistry a.samanta

8 rows selected.

SQL> --~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SQL> create table enroll
2 (
3 sid varchar(5),
4 cno varchar(5),
5 dname varchar(10),
6 secno varchar(5),
7 grad char(3),
8 foreign key (sid) references stud,
9 foreign key (dname) references sdept,
10 foreign key (cno) references course,
11 foreign key (secno) references section
12 );

Table created.

SQL> desc enroll;


Name Null? Type
----------------------------------------- -------- ----------------
-
SID VARCHAR2(5)
CNO VARCHAR2(5)
DNAME VARCHAR2(10)
SECNO VARCHAR2(5)
GRAD CHAR(3)

SQL> insert into enroll values('s_2','c_1','comp.sc','sec_1','A');

1 row created.

Commit complete.
SQL> select * from enroll;

SID CNO DNAME SECNO GRA


----- ----- ---------- ----- ---
s_2 c_1 comp.sc sec_1 A
s_8 c_8 zoology sec_5 B
s_5 c_8 zoology sec_5 C
s_1 c_1 comp.sc sec_3 B

18
s_3 c_3 math sec_2 A
s_6 c_4 math sec_7 A
s_5 c_7 chemistry sec_8 B
s_9 c_7 chemistry sec_8 A
s_10 c_6 physics sec_6 C
s_7 c_5 physics sec_4 B

10 rows selected.

SQL> --
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--SQL statement:-
__~~~~~~~~~~~~~~
SQL> select pname from proff
2 where dname in
3 (select dname from sdept
4 where no_phd<5);

--output:-
--~~~~~~~~
PNAME
----------
s.kanrar
d.kar
s.dutta
p.sarkar

--SQL statement:-
__~~~~~~~~~~~~~~
SQL> select sname from stud
2 where gpa in
3 (select min(gpa) from stud);

--output:-
--~~~~~~~~
SNAME
----------
raj
shiksa

--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

19
-:Visual Basic Form:-

Proffesor table Form using VB6

20
Adding Data:

Before Add

After Add

21
Editing Data:

Before Edit

After Edit

22
Deleting Data:

Before Delete

After Delete

23
VISUAL BASIC CODE:
Private Sub ad_Click()

Adodc1.Recordset.AddNew

pname.Text = ""

dname.Text = ""

End Sub

Private Sub dlt_Click()

Adodc1.Recordset.Delete

End Sub

Private Sub ed_Click()

Adodc1.Recordset.Update

End Sub

Private Sub nxt_Click()

Adodc1.Recordset.MoveNext

End Sub

Private Sub prv_Click()

Adodc1.Recordset.MovePrevious

End Sub

Private Sub sav_Click()

Adodc1.Recordset.Update

End Sub

24
SQL

25
PROBLEM 1
Flights ( fl_no, from, to_distance, departs, arrives )
Aircraft ( a_id, a_name, cruise_range )
a_name should be Boeing, Airbus, Dakota or Jet.
Certified ( e_id, a_id )
Employees ( e_id, e_name, salary )
Find total amount paid to employees as salary.
Find the e_ids of pilots certified for Boeing aircrafts.
Find the e_ids of employees certified for exactly 3 aircrafts.
Find the a_ids of all aircrafts that can be used for flights from Kolkata to Bangalore.
Find the e_ids of employees who are certified for largest number of aircrafts.
Find the flights that can be piloted by every pilot whose salary is greater than Rs. 50000/-.
Find the names of the pilots who can operate planes with a range greater than 3000Mile but are not
certified on any Jet aircraft.
Find e_ids of the employees who earn the 2nd highest salary.

SQL STATEMENTS FOR CREATING TABLES:


SQL> CREATE TABLE FLIGHTS ( FL_NO NUMBER(6), FROM_CITY VARCHAR2(12), TO_CITY
VARCHAR2(12), DISTANCE NUMBER(7), DEPARTS VARCHAR2(10), ARRIVES VARCHAR2(10), PRIMARY
KEY(FL_NO));
Table created.

SQL> DESC FLIGHTS;


Name Null? Type
----------------------------------------- ---------------- ----------------------------
FL_NO NOT NULL NUMBER(6)
FROM_CITY VARCHAR2(12)
TO_CITY VARCHAR2(12)
DISTANCE NUMBER(7)
DEPARTS VARCHAR2(10)
ARRIVES VARCHAR2(10)

SQL> CREATE TABLE AIRCRAFT ( A_ID NUMBER(4) PRIMARY KEY, A_NAME VARCHAR2(6),
CRUISE_RANGE NUMBER(12), CHECK(A_NAME IN ('BOEING','AIRBUS','DAKOTA','JET')));
Table created.

SQL> DESC AIRCRAFT;


Name Null? Type
----------------------------------------- ---------------- ----------------------------
A_ID NOT NULL NUMBER(4)
A_NAME VARCHAR2(6)
CRUISE_RANGE NUMBER(12)

SQL> CREATE TABLE EMPLOYEES(E_ID NUMBER(4) PRIMARY KEY, E_NAME VARCHAR2(25), SALARY
NUMBER(6));
Table created.

SQL> DESC EMPLOYEES;


Name Null? Type
----------------------------------------- ---------------- ----------------------------

26
E_ID NOT NULL NUMBER(4)
E_NAME VARCHAR2(25)
SALARY NUMBER(6)

SQL> CREATE TABLE CERTIFIED(E_ID REFERENCES EMPLOYEES(E_ID), A_ID REFERENCES


AIRCRAFT(A_ID));
Table created.

SQL> DESC CERTIFIED;


Name Null? Type
----------------------------------------- ---------------- ----------------------------
E_ID NUMBER(4)
A_ID NUMBER(4)

TABLES:
SQL> SELECT * FROM AIRCRAFT;
A_ID A_NAME CRUISE_RANGE
------ ------------ --------------------
3415 JET 5000
1264 DAKOTA 3000
5675 BOEING 4500
2453 AIRBUS 2800
4564 DAKOTA 4800
2967 JET 2950
3452 JET 5500
2341 BOEING 2500
3466 DAKOTA 4800
3534 AIRBUS 4000
2346 JET 8000
1240 DAKOTA 3500
6745 BOEING 5600

13 rows selected.

SQL> SELECT * FROM EMPLOYEES;


E_ID E_NAME SALARY
------ --------------------------- ----------
3452 ROHIT AHUJA 45000
3454 RAHUL MISHRA 55000
7654 PIYUSH DEV 48000
2353 BARUN YADAV 30000
3643 RIYA MOITRA 28000
2534 YASH SACHDEV 55000
2354 PIU CHOWDHURY 30000
3648 RAJIV KAPOOR 60000
3450 KANIKA SHARMA 25000
7897 HARSH MEHTA 45000
4574 VARUN MISHRA 35000
7078 MANPREET KAUR 35000
8888 RUPA PANDEY 28000

27
8908 ANIRUDDH GOENKA 30000
3453 ISHAN LAHIRI 42000

15 rows selected.

SQL> SELECT * FROM FLIGHTS;


FL_NO FROM_CITY TO_CITY DISTANCE DEPARTS ARRIVES
---------- --------------- ---------------- ------------- ------------ ----------
345123 KOLKATA BANGALORE 4500 23:45:56 02:12:34
656498 KOLKATA CHENNAI 4500 12:06:23 15:08:09
345609 CHENNAI GUWAHATI 2300 07:45:09 09:45:13
987905 KOLKATA BANGALORE 4000 16:56:09 18:08:34
897689 MUMBAI DELHI 4000 13:09:55 14:47:45

SQL> SELECT * FROM CERTIFIED;


E_ID A_ID
------ ------
3452 2967
3454 3466
7654 5675
3453 1264
7897 3415
3648 2346
2534 4564
3452 1264
3452 3452
3453 2453
7897 5675
7654 3466
3452 2341
3454 2453
7654 3534
3452 2453
2534 1264
3454 3534
3648 3452
3452 2346
3454 2341
2534 2453

22 rows selected.

QUERIES:
Find total amount paid to employees as salary.
SQL STATEMENT:
SQL> SELECT SUM(SALARY) FROM EMPLOYEES;
OUTPUT:
SUM(SALARY)
------------------
591000

28
Find the e_ids of pilots certified for Boeing aircrafts.
SQL STATEMENT:
SQL> SELECT E_ID FROM CERTIFIED,AIRCRAFT WHERE CERTIFIED.A_ID=AIRCRAFT.A_ID AND
A_NAME='BOEING';
OUTPUT:
E_ID
-------
7654
7897
3452
3454

(a) Find the e_ids of employees certified for exactly 3 aircrafts.


SQL STATEMENT:
SQL> SELECT E_ID FROM CERTIFIED GROUP BY E_ID HAVING COUNT(A_ID)=3;
OUTPUT:
E_ID
------
2534
7654

(b) Find the a_ids of all aircrafts that can be used for flights from Kolkata to Bangalore.
SQL STATEMENT:
SQL> SELECT UNIQUE AIRCRAFT.A_ID FROM FLIGHTS,AIRCRAFT WHERE CRUISE_RANGE>(SELECT
MIN(DISTANCE) FROM FLIGHTS WHERE FROM_CITY='KOLKATA' AND
TO_CITY='BANGALORE');
OUTPUT:
A_ID
------
4564
3466
3415
5675
3452
2346
6745

7 rows selected.

(c) Find the e_ids of employees who are certified for largest number of aircrafts.
SQL STATEMENT:
SQL> SELECT E_ID FROM CERTIFIED GROUP BY E_ID HAVING COUNT(A_ID)=(SELECT
MAX(COUNT(A_ID)) FROM CERTIFIED GROUP BY E_ID);
OUTPUT:
E_ID
------
3452

(d) Find the flights that can be piloted by every pilot whose salary is greater than Rs. 50000/-.

29
SQL STATEMENT:
SQL> SELECT FL_NO FROM FLIGHTS WHERE DISTANCE<=(SELECT MIN(CRUISE_RANGE) FROM
AIRCRAFT,CERTIFIED,EMPLOYEES WHERE CERTIFIED.E_ID=EMPLOYEES.E_ID AND
CERTIFIED.A_ID=AIRCRAFT.A_ID AND SALARY>=(SELECT MIN(SALARY) FROM EMPLOYEES
WHERE SALARY>=50000));
OUTPUT:
FL_NO
---------
345609

(e) Find the names of the pilots who can operate planes with a range greater than 3000Mile but
are not
certified on any Jet aircraft.
SQL STATEMENT:
SQL> SELECT E_NAME FROM AIRCRAFT,CERTIFIED,EMPLOYEES WHERE CERTIFIED.E_ID =
EMPLOYEES.E_ID AND CERTIFIED.A_ID=AIRCRAFT.A_ID AND CRUISE_RANGE>3000 MINUS SELECT
E_NAME FROM AIRCRAFT,CERTIFIED,EMPLOYEES WHERE CERTIFIED.E_ID =
EMPLOYEES.E_ID AND CERTIFIED.A_ID=AIRCRAFT.A_ID AND A_NAME='JET';
OUTPUT:
E_NAME
-------------------------
PIYUSH DEV
RAHUL MISHRA
YASH SACHDEV

(f) Find e_ids of the employees who earn the 2nd highest salary.
SQL STATEMENT:
SQL> SELECT UNIQUE EMPLOYEES.E_ID,SALARY FROM CERTIFIED,EMPLOYEES WHERE
CERTIFIED.E_ID=EMPLOYEES.E_ID AND SALARY=(SELECT MAX(SALARY) FROM EMPLOYEES
WHERE SALARY<(SELECT MAX(SALARY) FROM EMPLOYEES));
OUTPUT:
E_ID SALARY
------ ----------
2534 55000
3454 55000

30
PROBLEM 2
Worker ( worker_id, worker_name, salary_rate, skill_type, superviser_id,
superviser_name )
Building ( bldg_id, bldg_add, type, qty_level, status )
Assignment (worker_id, bldg_id, start_date, number_days)
a) What is the total no. of days allocated for plumbing on building 312?
b) For each supervisor, what is the highest hourly wage paid to a worker
reporting to the supervisor?

SQL STATEMENTS FOR CREATING TABLES:


SQL> create table Worker ( worker_id number(3), worker_name varchar2(8),
salary_rate number(6),
skill_type varchar2(12), superviser_id number(3), superviser_name
varchar2(8), primary key(worker_id));
Table created.

SQL> Desc Worker;


Name Null? Type
----------------------------------------- ------------- --------------------
--------
WORKER_ID NOT NULL NUMBER(3)
WORKER_NAME VARCHAR2(8)
SALARY_RATE NUMBER(6)
SKILL_TYPE VARCHAR2(12)
SUPERVISER_ID NUMBER(3)
SUPERVISER_NAME VARCHAR2(8)

SQL> create table Building ( bldg_id number(3), bldg_add varchar2(15), type


varchar2(10), qty_level
number(3), status varchar2(10), primary key(bldg_id));
Table created.

SQL> Desc Building;


Name Null? Type
----------------------------------------- ------------- --------------------
--------
BLDG_ID NOT NULL NUMBER(3)
BLDG_ADD VARCHAR2(15)
TYPE VARCHAR2(10)
QTY_LEVEL NUMBER(3)
STATUS VARCHAR2(10)

31
SQL> create table Assignment ( worker_id references Worker ( worker_id ),
bldg_id references
Building ( bldg_id ), start_date date, number_days number(3));
Table created.

SQL> Desc Assignment;


Name Null? Type
----------------------------------------- ------------- --------------------
--------
WORKER_ID NUMBER(3)
BLDG_ID NUMBER(3)
START_DATE DATE
NUMBER_DAYS NUMBER(3)

TABLES:
SQL> SELECT * FROM WORKER;
WORKER_ID WORKER_NAME SALARY_RATE
SKILL_TYPE SUPERVISER_ID SUPERVISER_NAME
--------------- -------------------- ----------------- -----------
---- -------------------- -------------------------
101 Ram 200
Plumbing 105 Tushar
102 Madhu 250
Cementing 109 Dipak
103 Hiru 100
Carpenter 105 Tushar
104 Rajen 400 Tile
Setter 105 Tushar
105 Tushar 350
Supervising 105 Tushar
106 Joy 450 Elevator
109 Dipak
108 Ganesh 200
Plumbing 109 Dipak
109 Dipak 350
Supervising 109 Dipak

8 rows selected.

SQL> SELECT * FROM BUILDING;


BLDG_ID BLDG_ADD TYPE QTY_LEVEL STATUS
----------- ---------------- ------------ -------------- ----------
310 Thakurpukur Remodel 100 Pending
311 Bullygunge Construct 150 Finshed
312 Kiderpore Remodel 100 Working
313 Bagbazar Construct 200 Pending

32
4 rows selected.

SQL> SELECT * FROM ASSIGNMENT;


WORKER_ID BLDG_ID START_DATE NUMBER_DAYS
--------------- ----------- ---------------- -------------------
101 310 12-DEC-12 45
102 312 13-NOV-12 30
108 312 11-NOV-12 25
101 312 28-DEC-12 10
105 310 02-JAN-13 15
106 313 16-DEC-12 23
108 313 03-DEC-12 34
105 312 11-NOV-12 45

8 rows selected.

QUERIES:
a) What is the total no. of days allocated for plumbing on building 312?
SQL> select sum(number_days) from worker,building,assignment
2 where
3 worker.worker_id=assignment.worker_id and
4 building.bldg_id=assignment.bldg_id and
5 skill_type='Plumbing' and
6 assignment.bldg_id=312;

OUTPUT:
SUM(NUMBER_DAYS)
--------------------------
35
b) For each supervisor, what is the highest hourly wage paid to a worker
reporting to the supervisor?
SQL> select superviser_id,max(salary_rate) from worker group by

superviser_id;

OUTPUT:
SUPERVISER_ID MAX(SALARY_RATE)
------------------- ------------------------
105 400
109 450

33
PROBLEM 3
Department ( d_no, d_name )
Employee ( f_name, l_name, SSN, address, salary, d_no )
Project ( p_no, p_name, p_location, d_no )
Works_on (SSN, p_no, hours)
a) For each project on which more than 1 employees work, retrieve the
project number, the project name and the no. of employees who work
on the project.
b) Create a view that has the employee name, project name, and
employee salary for each employee who works in the Marketing
department.
c) Retrieve the name of each employee who works on all the projects
controlled by department number 5.
d) For each department, list the department number, the number of
employees in the department, and their average salary.
e) List the names of employee whose salary is greater than the salary of
all the employees in department 4.

SQL STATEMENTS FOR CREATING TABLES:


SQL> CREATE TABLE DEPARTMENT ( D_NO NUMBER(4) PRIMARY KEY,
D_NAME VARCHAR2(12) );
Table created.

SQL> DESC DEPARTMENT;


Name Null? Type
----------------------------------------- ------------- --------------------
--------
D_NO NOT NULL NUMBER(4)
D_NAME VARCHAR2(12)

SQL> CREATE TABLE EMPLOYEE(F_NAME VARCHAR2(10), L_NAME


VARCHAR2(12), SSN NUMBER(10) PRIMARY KEY, ADDRESS
VARCHAR2(25), D_NO REFERENCES DEPARTMENT(D_NO), SALARY
NUMBER(6));
Table created.

SQL> DESC EMPLOYEE;


Name Null? Type
----------------------------------------- ------------- --------------------
--------
F_NAME VARCHAR2(10)
L_NAME VARCHAR2(12)

34
SSN NOT NULL NUMBER(10)
ADDRESS VARCHAR2(25)
D_NO NUMBER(4)
SALARY NUMBER(6)

SQL> CREATE TABLE PROJECT ( P_NO NUMBER(4) PRIMARY KEY,


P_NAME VARCHAR2(15), P_LOCATION VARCHAR2(12), D_NO
REFERENCES DEPARTMENT(D_NO) );
Table created.

SQL> DESC PROJECT;


Name Null? Type
----------------------------------------- ------------- --------------------
--------
P_NO NOT NULL NUMBER(4)
P_NAME VARCHAR2(15)
P_LOCATION VARCHAR2(12)
D_NO NUMBER(4)

SQL> CREATE TABLE WORKS_ON ( SSN REFERENCES EMPLOYEE(SSN),


P_NO REFERENCES PROJECT(P_NO), HOURS NUMBER(6) );
Table created.

SQL> DESC WORKS_ON;


Name Null? Type
----------------------------------------- ------------- --------------------
--------
SSN NUMBER(10)
P_NO NUMBER(4)
HOURS NUMBER(6)

TABLES:
SQL> SELECT * FROM DEPARTMENT;
D_NO D_NAME
------- ---------------------
3 MARKETING
4 CONSTRUCTION
5 ADVERTISING
1 SALES

SQL> SELECT * FROM EMPLOYEE;


F_NAME L_NAME SSN
ADDRESS D_NO SALARY
------------ --------------------- ------------ -----------------
---------- ------- ----------

35
ANKIT SHARMA 56451907 LINDSEY STREET 3
25000
SARBANI MITRA 76278460
JODHPUR PARK 4 30000
UMESH YADAV 92378148
RAJARHAT 5 35000
YAMINI MULLICK 76230978 BOWBAZAR 5
35000
TUSHAR ADHIKARY 76564312
GARIAHAT 1 28000
RAMIT MUKHERJEE 90123456
TALLYGUNGE 1 50000
RANIT THAKRE 12345678 JOKA 4
45000
SMRITI DESHAI 67453490 BANDRA, MUMBAI 5
55000
RAJESH MISHRA 78563490
BELUR, HOWRAH 5 37000
EMON BHATTACHARYA 89564019
DUMDUM, KOLKATA 3 34000
ISHAN MOITRA 51019386 BAGBAZAR, KOLKATA 5
50000

11 rows selected.

SQL> SELECT * FROM PROJECT;


P_NO P_NAME P_LOCATION D_NO
------- -------------------- ------------------ -------
80 NEW CALCUTTA BULLYGUNGE 5
67 RAJMAHAL BARASAT 4
34 PARADISE MALL FARIDABAD 1
30 DREAM HOME BELEGHATA 3
99 HOTEL ERINA RAJARHAT 5

SQL> SELECT * FROM WORKS_ON;


SSN P_NO HOURS
------------ ------- ----------
67453490 80 8
78563490 34 6
56451907 67 2
76278460 67 7
92378148 99 9
90123456 34 8
76564312 34 7
76230978 99 8

36
12345678 67 7
78563490 30 5
78563490 99 3
89564019 34 2
51019386 30 3
89564019 99 4
51019386 34 4
67453490 99 3

16 rows selected.

QUERIES:
a) For each project on which more than 1 employees work, retrieve the project
number, the project name
and the no. of employees who work on the project.
SQL STATEMENT:
SQL> SELECT PROJECT.P_NO,P_NAME,COUNT(SSN) AS NO_OF_EMP
FROM PROJECT,WORKS_ON WHERE PROJECT.P_NO=WORKS_ON.P_NO
AND PROJECT.P_NO IN (SELECT PROJECT.P_NO FROM
PROJECT,WORKS_ON WHERE PROJECT.P_NO=WORKS_ON.P_NO GROUP
BY PROJECT.P_NO
HAVING COUNT(SSN)>1)GROUP BY PROJECT.P_NO,P_NAME;

OUTPUT:
P_NO P_NAME NO_OF_EMP
------ -------------------- ---------------
67 RAJMAHAL 3
34 PARADISE MALL 5
30 DREAM HOME 2
99 HOTEL ERINA 5

b) Create a view that has the employee name, project name, and employee
salary for each employee who
works in the Marketing department.
SQL STATEMENT:
SQL> CREATE VIEW EMP AS (SELECT F_NAME,L_NAME,P_NAME,SALARY
FROM EMPLOYEE, PROJECT,WORKS_ON,DEPARTMENT WHERE
EMPLOYEE.SSN=WORKS_ON.SSN AND PROJECT.P_NO =
WORKS_ON.P_NO AND PROJECT.D_NO=DEPARTMENT.D_NO AND
D_NAME = 'MARKETING' );
View created.

OUTPUT:
SQL> SELECT * FROM EMP;

37
F_NAME L_NAME P_NAME SALARY
---------- ---------- ------------------ ----------
RAJESH MISHRA DREAM HOME 37000
ISHAN MOITRA DREAM HOME 50000

c) Retrieve the name of each employee who works on all the projects controlled by
department number 5.
SQL STATEMENT:
SQL> SELECT F_NAME,L_NAME FROM EMPLOYEE,PROJECT,WORKS_ON
WHERE EMPLOYEE.SSN= WORKS_ON.SSN AND
PROJECT.P_NO=WORKS_ON.P_NO AND PROJECT.D_NO=5 HAVING COUNT
(UNIQUE PROJECT.P_NO)=(SELECT COUNT(UNIQUE P_NO) FROM
PROJECT WHERE D_NO=5) GROUP
BY EMPLOYEE.SSN,F_NAME,L_NAME;

OUTPUT:
F_NAME L_NAME
---------- ------------
SMRITI DESHAI

d) For each department, list the department number, the number of


employees in the department, and their average salary.
SQL STATEMENT:
SQL> SELECT D_NO,COUNT(SSN),AVG(SALARY) FROM EMPLOYEE GROUP

BY D_NO;

OUTPUT:
D_NO COUNT(SSN) AVG(SALARY)
------- --------------- ----------------
1 2 39000
4 2 37500
5 5 42400
3 2 29500

e) List the names of employee whose salary is greater than the salary of all the employees
in department 4.
SQL STATEMENT:
SQL> SELECT F_NAME,L_NAME FROM EMPLOYEE WHERE SALARY > (
SELECT MAX(SALARY) FROM
EMPLOYEE WHERE D_NO = 4 );

OUTPUT:
F_NAME L_NAME
---------- ----------------

38
RAMIT MUKHERJEE
SMRITI DESHAI

39
PROBLEM 4
Customer( cust_id, cust_name, annual_revenue, cust_type )
cust_id must be between 100 to 10000, annual_revenue defaults to 20000,
cust_type must be manufacturer, wholesaler or retailer.
Shipment( ship_no, cust_id, weight, truck_no, destination, ship_date )
weight must be under 1000 and defaults to 10.
Truck ( truck_no, driver_name )
City ( city_name, population )
(a) Print the name of the customers who have sent the packets to Mumbai or
Hyderabad.
(b) List the customers who have had shipments delivered by every truck.
(c) List the name of the cities which have the largest and smallest number of
population.
(d) List the names of the drivers who have delivered shipments to every city.
(e) List the names and the annual revenue of the customers where the
shipments have been delivered by the truck driver Ranjit Singh.
(f) List the cities that have received shipments from every customer.

SQL STATEMENTS FOR CREATING TABLES:


SQL> CREATE TABLE CUSTOMER3(CUST_ID NUMBER(7) PRIMARY KEY,
CHECK(CUST_ID BETWEEN 100 AND 10000), CUST_NAME VARCHAR2(25),
ANNUAL_REVENUE NUMBER(6) DEFAULT 20000, CUST_TYPE
VARCHAR2(15), CHECK(CUST_TYPE
IN('MANUFACTURER','WHOLESALER','RETAILER')));
Table created.

SQL> DESC CUSTOMER3;


Name Null? Type
----------------------------------------- ------------- --------------------
--------
CUST_ID NOT NULL NUMBER(7)
CUST_NAME VARCHAR2(25)
ANNUAL_REVENUE NUMBER(6)
CUST_TYPE VARCHAR2(15)

SQL> CREATE TABLE TRUCK(TRUCK_NO VARCHAR2(10) PRIMARY KEY,


DRIVER_NAME VARCHAR2(25));
Table created.

SQL> DESC TRUCK;


Name Null? Type
----------------------------------------- ------------- --------------------
--------
TRUCK_NO NOT NULL VARCHAR2(10)
DRIVER_NAME VARCHAR2(25)

40
SQL> CREATE TABLE CITY1(CITY_NAME VARCHAR2(25) PRIMARY KEY,
POPULATION NUMBER(10));
Table created.

SQL> DESC CITY1;


Name Null? Type
----------------------------------------- ------------- --------------------
--------
CITY_NAME NOT NULL VARCHAR2(25)
POPULATION NUMBER(10)

SQL> CREATE TABLE SHIPMENT(SHIP_NO NUMBER(7) PRIMARY KEY,


CUST_ID REFERENCES CUSTOMER3(CUST_ID), WEIGHT NUMBER(6)
DEFAULT 10, CHECK(WEIGHT<1000), TRUCK_NO REFERENCES
TRUCK(TRUCK_NO), DESTINATION REFERENCES
CITY1(CITY_NAME),SHIP_DATE DATE);
Table created.

SQL> DESC SHIPMENT;


Name Null? Type
----------------------------------------- ------------- --------------------
--------
SHIP_NO NOT NULL NUMBER(7)
CUST_ID NUMBER(7)
WEIGHT NUMBER(6)
TRUCK_NO VARCHAR2(10)
DESTINATION VARCHAR2(25)
SHIP_DATE DATE

TABLES:
SQL> SELECT * FROM CUSTOMER3;
CUST_ID CUST_NAME ANNUAL_REVENUE CUST_TYPE
---------- ------------------------- ------------------------ ---------------
2001 GAGANDIP MISHRA 25000 WHOLESALER
3001 RANDIP OJHA 20000 RETAILER
2002 JAYANT SONI 35000 WHOLESALER
3002 SUDIP BOSE 28000 RETAILER
3003 PARAM DUTTA 30000 RETAILER
1002 SANDEEP KAUR 40000 MANUFACTURER

7 rows selected.

SQL> SELECT * FROM TRUCK;

41
TRUCK_NO DRIVER_NAME
-------------- -------------------------
WB4023 MANPREET SINGH
WB2709 RANJIT SINGH
WB9012 RAJESH KHURANA
WB8912 PRASHANT BADHERA

SQL> SELECT * FROM CITY1;


CITY_NAME POPULATION
------------------------- ----------------
AMRITSAR 230000
HYDERABAD 450000
MUMBAI 600000
LUDHIANA 200000
RAJKOT 250000
SQL> SELECT * FROM SHIPMENT;
SHIP_NO CUST_ID WEIGHT TRUCK_NO DESTINATION
SHIP_DATE
----------- ----------- ---------- ------------- ------------------------- ------------
--
101 2001 850 WB2709 HYDERABAD
14-OCT-11
103 3001 990 WB4023 RAJKOT
12-NOV-12
104 3003 450 WB2709 AMRITSAR
04-FEB-10
106 2001 800 WB4023 AMRITSAR
12-JAN-11
108 1002 450 WB4023 LUDHIANA 13-MAR-
12
110 2002 670 WB4023 HYDERABAD 03-DEC-
12
119 3002 670 WB9012 RAJKOT
05-JUL-12
118 2001 430 WB9012 MUMBAI
02-JAN-12
124 1002 560 WB4023 RAJKOT
03-MAY-12
234 2001 560 WB8912 RAJKOT
01-APR-13
125 2002 789 WB9012 RAJKOT
04-MAY-12
167 2002 456 WB2709 AMRITSAR
14-FEB-12

42
126 2001 569 WB4023 RAJKOT
05-JAN-13
109 2002 450 WB8912 RAJKOT
30-SEP-12
238 2002 345 WB9012 HYDERABAD
18-NOV-12
239 2002 670 WB9012 LUDHIANA
05-JUN-12
240 2002 890 WB9012 AMRITSAR
18-NOV-11
256 3002 990 WB2709 HYDERABAD
14-FEB-12
463 3003 345 WB4023 RAJKOT
05-AUG-12

19 rows selected.

QUERIES:
a) Print the name of the customers who have sent the packets to Mumbai or
Hyderabad.
SQL STATEMENT:
SQL> SELECT UNIQUE CUST_NAME FROM CUSTOMER3,SHIPMENT
WHERE
CUSTOMER3.CUST_ID=SHIPMENT.CUST_ID AND DESTINATION IN

('MUMBAI','HYDERABAD');

OUTPUT:
CUST_NAME
-------------------------
GAGANDIP MISHRA
SUDIP BOSE
JAYANT SONI

b) List the customers who have had shipments delivered by every truck.
SQL STATEMENT:
SQL> SELECT CUST_NAME FROM CUSTOMER3 WHERE CUST_ID
IN(SELECT CUST_ID FROM SHIPMENT HAVING COUNT(UNIQUE
TRUCK_NO)=( SELECT COUNT(TRUCK_NO) FROM TRUCK)
GROUP BY CUST_ID);

OUTPUT:

43
CUST_NAME
-------------------------
GAGANDIP MISHRA
JAYANT SONI

c) List the name of the cities which have the largest and smallest number of population.
SQL STATEMENT:
SQL> SELECT * FROM CITY1 WHERE POPULATION IN((SELECT
MAX(POPULATION) FROM CITY1),
(SELECT MIN(POPULATION) FROM CITY1));

OUTPUT:
CITY_NAME POPULATION
------------------------- ----------------
MUMBAI 600000
LUDHIANA 200000

d) List the names of the drivers who have delivered shipments to every city.
SQL STATEMENT:
SQL> SELECT DRIVER_NAME FROM TRUCK WHERE TRUCK_NO IN
(SELECT TRUCK_NO FROM SHIPMENT HAVING COUNT(UNIQUE
DESTINATION)=(SELECT COUNT(CITY_NAME) FROM CITY1)
GROUP BY TRUCK_NO);

OUTPUT:
DRIVER_NAME
-------------------------
RAJESH KHURANA

e) List the names and the annual revenue of the customers where the shipments have been
delivered by the truck driver Ranjit Singh.
SQL STATEMENT:
SQL> SELECT CUST_NAME, ANNUAL_REVENUE FROM
CUSTOMER3,SHIPMENT,TRUCK WHERE
CUSTOMER3.CUST_ID=SHIPMENT.CUST_ID AND
TRUCK.TRUCK_NO=SHIPMENT.TRUCK_NO AND
DRIVER_NAME='RANJIT SINGH';

OUTPUT:
CUST_NAME ANNUAL_REVENUE
------------------------- ------------------------
GAGANDIP MISHRA 25000
PARAM DUTTA 30000

44
JAYANT SONI 35000
SUDIP BOSE 28000

f) List the cities that have received shipments from every customer.
SQL STATEMENT:
SQL> SELECT DESTINATION FROM SHIPMENT HAVING COUNT(UNIQUE
CUST_ID)=(SELECT
COUNT(CUST_ID) FROM CUSTOMER3) GROUP BY DESTINATION;

OUTPUT:
DESTINATION
-----------------
RAJKOT

45

You might also like