SQL Nikk
SQL Nikk
PAPER 7B
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)
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.
Table created.
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)
7 rows selected.
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Table created.
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.
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Table created.
SQL>
SQL> insert into dept
2 values ('d-01','system','e-03','3');
1 row created.
Commit complete.
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
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:
Adodc1.Recordset.AddNew
pname.Text = ""
dname.Text = ""
End Sub
Adodc1.Recordset.Delete
End Sub
Adodc1.Recordset.Update
End Sub
End Sub
Adodc1.Recordset.MoveNext
End Sub
Adodc1.Recordset.MovePrevious
End Sub
Adodc1.Recordset.Update
End Sub
12
Problem statement:-2
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.
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;
10 rows selected.
Table created.
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.
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.
1 row created.
Commit complete.
SQL> select * from course;
8 rows selected.
SQL> --
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Table created.
16
SID NOT NULL VARCHAR2(5)
Table created.
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)
1 row created.
Commit complete.
17
SQL> select * from section;
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.
1 row created.
Commit complete.
SQL> select * from enroll;
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:-
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
Adodc1.Recordset.Delete
End Sub
Adodc1.Recordset.Update
End Sub
Adodc1.Recordset.MoveNext
End Sub
Adodc1.Recordset.MovePrevious
End Sub
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> 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> CREATE TABLE EMPLOYEES(E_ID NUMBER(4) PRIMARY KEY, E_NAME VARCHAR2(25), SALARY
NUMBER(6));
Table created.
26
E_ID NOT NULL NUMBER(4)
E_NAME VARCHAR2(25)
SALARY NUMBER(6)
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.
27
8908 ANIRUDDH GOENKA 30000
3453 ISHAN LAHIRI 42000
15 rows selected.
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
(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?
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.
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.
32
4 rows selected.
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.
34
SSN NOT NULL NUMBER(10)
ADDRESS VARCHAR2(25)
D_NO NUMBER(4)
SALARY NUMBER(6)
TABLES:
SQL> SELECT * FROM DEPARTMENT;
D_NO D_NAME
------- ---------------------
3 MARKETING
4 CONSTRUCTION
5 ADVERTISING
1 SALES
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.
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
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.
40
SQL> CREATE TABLE CITY1(CITY_NAME VARCHAR2(25) PRIMARY KEY,
POPULATION NUMBER(10));
Table created.
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.
41
TRUCK_NO DRIVER_NAME
-------------- -------------------------
WB4023 MANPREET SINGH
WB2709 RANJIT SINGH
WB9012 RAJESH KHURANA
WB8912 PRASHANT BADHERA
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