Uploaded by kanishka.csa.nbu

SQL-ASSIGNMEN

advertisement
ASSIGNMENT-1
Create a table for students’ information as follows.
STUDENT (Sid, Name, City, Adm_date, Paper1, Paper2)
Insert appropriate records in the above table so that at least one record is found for the following
queries.
a) Show the students information whose name is started with ‘U’.
b) Show the students information whose name is ended with ‘a’.
c) Show the students information whose name contains ‘b’ at second place.
d) Show the students information whose name contains ‘h’ at second last place.
e) Show the students information who obtained highest marks in paper1.
f) Show the average marks in paper2.
g) Show the students information who obtained highest marks in total.
h) Show the students information who obtained marks greater than equal to 75%.
i) Show the students information who obtained second highest marks in paper2.
j) Show city wise number of students.
k) Along with number of students display the city name where maximum number of students lives.
l) In which date maximum number of students were admitted.
SOLUTION:
▪ Create a table for students’ information as follows.
STUDENT (Sid, Name, City, Adm_date, Paper1, Paper2)
SQL STATEMENT:
CREATE TABLE STUDENT (
Sid NUMBER PRIMARY KEY,
Name VARCHAR2(22) NOT NULL,
City VARCHAR2(15) NOT NULL,
Adm_date DATE,
Paper1 NUMBER(3),
Paper2 NUMBER(3));
OUTPUT:
Table created.
▪
Insert appropriate records in the above table
SQL STATEMENT:
SQL> INSERT INTO STUDENT VALUES(1,'UTTAM PAL','JALPAIGUR','19-AUG-23',67,87);
1 row created.
SQL> INSERT INTO STUDENT VALUES(2,'MALANCHA DASGUPTA','JALPAIGUR','19-AUG23',97,96);
1 row created.
SQL> INSERT INTO STUDENT VALUES(3,'KANISHKA SARKAR','ISLAMPUR','19-AUG-23',93,98);
1 row created.
SQL> INSERT INTO STUDENT VALUES(4,'ABHIJIT SARKAR','ISLAMPUR','23-AUG-23',51,69);
1 row created.
SQL> INSERT INTO STUDENT VALUES(5,'ANKITA SAHA','JALPAIGURI','27-AUG-23',41,79);
1 row created.
SQL> SELECT * FROM STUDENT;
OUTPUT:
SID NAME
---------- ---------------------1 UTTAM PAL
2 MALANCHA DASGUPTA
3 KANISHKA SARKAR
4 ABHIJIT SARKAR
5 ANKITA SAHA
▪
NAME
---------------------UTTAM PAL
CITY
ADM_DATE PAPER1
-------------------------------JALPAIGUR 19-AUG-23
67
PAPER2
---------87
Show the students information whose name is ended with ‘a’.
QUERY:
SQL> SELECT * FROM STUDENT WHERE NAME LIKE '%A';
OUTPUT:
SID
NAME
-------- ---------------------2
MALANCHA DASGUPTA
5
ANKITA SAHA
▪
PAPER2
---------87
96
98
69
79
Show the students information whose name is started with ‘U’.
QUERY:
SQL> SELECT * FROM STUDENT WHERE NAME LIKE 'U%';
OUTPUT:
SID
---------1
▪
CITY
ADM_DATE PAPER1
-------------------------------JALPAIGUR
19-AUG-23
67
JALPAIGUR
19-AUG-23
97
ISLAMPUR
19-AUG-23
93
ISLAMPUR
23-AUG-23
51
JALPAIGURI 27-AUG-23
41
CITY
ADM_DATE PAPER1
-------------------------------JALPAIGUR 19-AUG-23
97
JALPAIGURI 27-AUG-23
41
PAPER2
---------96
79
Show the students information whose name contains ‘b’ at second place.
QUERY:
SQL> SELECT * FROM STUDENT WHERE NAME LIKE '_B%';
OUTPUT:
SID
---------4
NAME
---------------------ABHIJIT SARKAR
CITY
--------------ISLAMPUR
ADM_DATE PAPER1
-----------------23-AUG-23
51
PAPER2
---------69
▪
Show the students information whose name contains ‘h’ at second last place.
QUERY:
SQL> SELECT * FROM STUDENT WHERE NAME LIKE '%H_';
OUTPUT:
SID
---------5
▪
NAME
CITY
---------------------- --------------ANKITA SAHA JALPAIGURI
ADM_DATE PAPER1
-----------------27-AUG-23
41
PAPER2
---------79
Show the students information who obtained highest marks in paper1.
QUERY:
SQL> SELECT * FROM STUDENT WHERE PAPER1=(SELECT MAX(PAPER1) FROM STUDENT);
OUTPUT:
SID
---------- 2
▪
NAME
--------------------MALANCHA DASGUPTA
CITY
ADM_DATE PAPER1
-------------------------------JALPAIGUR 19-AUG-23
97
PAPER2
---------96
Show the average marks in paper2.
QUERY:
SQL> SELECT AVG(PAPER2) AS AVERAGE_OF_PAPER2 FROM STUDENT;
OUTPUT:
AVERAGE_OF_PAPER2
----------------85.8
▪
Show the students information who obtained highest marks in total.
QUERY:
SQL> SELECT * FROM STUDENT WHERE PAPER1+PAPER2=(SELECT MAX(PAPER1+PAPER2)
FROM STUDENT);
OUTPUT:
SID
---------2
▪
NAME
---------------------MALANCHA DASGUPTA
CITY
--------------JALPAIGUR
ADM_DATE PAPER1
-----------------19-AUG-23 97
PAPER2
---------96
Show the students information who obtained marks greater than equal to 75%.
QUERY:
SQL> SELECT * FROM STUDENT WHERE (PAPER1+PAPER2)/2>75;
OUTPUT:
SID
---------1
2
3
NAME
---------------------UTTAM PAL
MALANCHA DASGUPTA
KANISHKA SARKAR
CITY
--------------JALPAIGUR
JALPAIGUR
ISLAMPUR
ADM_DATE PAPER1
-----------------19-AUG-23
67
19-AUG-23
97
19-AUG-23
93
PAPER2
---------87
96
98
▪
Show the students information who obtained second highest marks in paper2.
QUERY:
SQL> SELECT * FROM STUDENT WHERE PAPER2=(SELECT MAX(PAPER2) FROM STUDENT
WHERE PAPER2<(SELECT MAX(PAPER2) FROM STUDENT));
OUTPUT:
SID
---------2
▪
ADM_DATE PAPER1
-----------------19-AUG-23 97
PAPER2
---------96
NUMBER_OF_STUDENT
----------------3
2
Along with number of students display the city name where maximum number of students lives.
QUERY:
SQL> SELECT * FROM (SELECT CITY, COUNT(*) AS NUMBER_OF_STUDENT FROM STUDENT
GROUP BY CITY) WHERE NUMBER_OF_STUDENT=(SELECT MAX(COUNT(*)) FROM STUDENT
GROUP BY CITY);
OUPUT:
CITY
--------------JALPAIGURI
▪
CITY
--------------JALPAIGUR
Show city wise number of students.
QUERY:
SQL> SELECT CITY, COUNT(*) AS NUMBER_OF_STUDENT FROM STUDENT GROUP BY CITY;
OUTPUT:
CITY
--------------JALPAIGURI
ISLAMPUR
▪
NAME
---------------------MALANCHA DASGUPTA
NUMBER_OF_STUDENT
----------------3
In which date maximum number of students were admitted.
QUERY:
SELECT * FROM (SELECT ADM_DATE, COUNT(*) AS NUMBER_OF_STUDENT FROM STUDENT
GROUP BY ADM_DATE) WHERE NUMBER_OF_STUDENT=(SELECT MAX(COUNT(*)) FROM
STUDENT GROUP BY ADM_DATE);
OUTPUT:
ADM_DATE
--------19-AUG-23
NUMBER_OF_STUDENT
----------------3
ASSIGNMENT-2
Create a table for students’ information as follows. STUDENT1 (Sid, Name, City, Adm_date, Semester,
Roll, Course) Insert appropriate records in the above table so that at least one record is found for the
following queries. (M.Tech, MCA and M.Sc are three different courses).
a) Show the information of M.Tech students.
b) Show the information of MCA students.
c) Show the information of M.Sc students.
d) Add a column at the end of the table to store student’s contact number.
e) Rename the table as STUDENT2 if possible.
f) Delete the “City” column from the table.
g) Rearrange the column as (Course, Sid, Semester, Roll, Name, Adm_date) if possible
▪
Create a table for students’ information as follows. STUDENT1 (Sid, Name, City, Adm_date, Semester,
Roll, Course)
SQL STATEMENT:
SQL> CREATE TABLE STUDENT1(SID NUMBER PRIMARY KEY, NAME VARCHAR2(15), CITY
VARCHAR2(15),ADM_DATE DATE, SEMESTER VARCHAR2(5), ROLL_NO NUMBER, COURSE
VARCHAR2(6));
OUTPUT:
Table created.
▪
Insert appropriate records in the above table so that at least one record is found for the following queries.
(M.Tech, MCA and M.Sc are three different courses).
SQL STATEMENT:
SQL> INSERT INTO STUDENT1 VALUES(11,'SONALI DAS','ISAMPUR','13-JUL-23','1ST',2,'MCA');
1 row created.
SQL> INSERT INTO STUDENT1 VALUES(12,'KUNAL DAS','SILIGURI','14-JUL-23','1ST',1,'MCA');
1 row created.
SQL> INSERT INTO STUDENT1 VALUES(13,'MUNNA ISLAM','JALPAIGURI','14-JUL23','1ST',3,'MCA');
1 row created.
SQL> INSERT INTO STUDENT1 VALUES(21,'SOHEL RANA','MALDA','13-JUL-23','1ST',1,'M.SC');
1 row created.
SQL> INSERT INTO STUDENT1 VALUES(22,'TANMOY HALDER','MALDA','13-JUL-23','1ST',2,'M.SC');
1 row created.
SQL> INSERT INTO STUDENT1 VALUES(23,'SUVRO SARKAR','JALPAIGURI','13-JUL23','1ST',3,'M.SC');
1 row created.
SQL> INSERT INTO STUDENT1 VALUES(33,'KANISHKA SARKAR','JALPAIGURI','18-JUL23','2ND',1,'M.TECH');
1 row created.
SQL> INSERT INTO STUDENT1 VALUES(31,'RIPON GOSH','COOCHBEHAR','18-JUL23','2ND',2,'M.TECH');
1 row created.
SQL> INSERT INTO STUDENT1 VALUES(32,'MRINMOY ROY','COOCHBEHAR','10-JUL23','2ND',3,'M.TECH');
1 row created.
OUTPUT:
SQL> SELECT * FROM STUDENT1;
SID
---------11
12
13
21
22
23
33
31
32
▪
NAME
CITY
ADM_DATE SEMES ROLL_NO
-------------------------------------------------SONALI DAS
ISAMPUR
13-JUL-23 1ST
2
KUNAL DAS
SILIGURI
14-JUL-23 1ST
1
MUNNA ISLAM
JALPAIGURI
14-JUL-23 1ST
3
SOHEL RANA
MALDA
13-JUL-23 1ST
1
TANMOY HALDER MALDA
13-JUL-23 1ST
2
SUVRO SARKAR
JALPAIGURI
13-JUL-23 1ST
3
KANISHKA SARKAR JALPAIGURI
18-JUL-23 2ND
1
RIPON GOSH
COOCHBEHAR 18-JUL-23 2ND
2
MRINMOY ROY
COOCHBEHAR 10-JUL-23 2ND
3
COURSE
-----MCA
MCA
MCA
M.SC
M.SC
M.SC
M.TECH
M.TECH
M.TECH
9 rows selected.
Show the information of M.Tech students.
QUERY:
SQL> SELECT * FROM STUDENT1 WHERE COURSE='M.TECH';
OUTPUT:
SID NAME
CITY
---------- ----------------------------33 KANISHKA SARKAR JALPAIGURI
31 RIPON GOSH
COOCHBEHAR
32 MRINMOY ROY
COOCHBEHAR
▪
ADM_DATE
--------18-JUL-23
18-JUL-23
10-JUL-23
SEMES ROLL_NO
-------------2ND
1
2ND
2
2ND
3
COURSE
-----M.TECH
M.TECH
M.TECH
Show the information of MCA students.
QUERY:
SQL> SELECT * FROM STUDENT1 WHERE COURSE='MCA';
OUTPUT:
SID NAME
CITY
ADM_DATE
---------- --------------- ----------------------11 SONALI DAS
ISAMPUR
13-JUL-23
12 KUNAL DAS
SILIGURI
14-JUL-23
13 MUNNA ISLAM JALPAIGURI 14-JUL-23
SEMES ROLL_NO
-------------1ST
2
1ST
1
1ST
3
COURSE
-----MCA
MCA
MCA
▪
Show the information of M.SC students.
QUERY:
SQL> SELECT * FROM STUDENT1 WHERE COURSE='M.SC';
OUTPUT:
SID NAME
CITY
ADM_DATE SEMES ROLL_NO
---------- -------------------------------------------------21 SOHEL RANA
MALDA
13-JUL-23
1ST
1
22 TANMOY HALDER MALDA
13-JUL-23
1ST
2
23 SUVRO SARKAR
JALPAIGURI 13-JUL-23
1ST
3
▪
COURSE
-----M.SC
M.SC
M.SC
Add a column at the end of the table to store student’s contact number.
SQL STATEMENT:
SQL> ALTER TABLE STUDENT1 ADD CONTACT_NO NUMBER;
Table altered.
OUTPUT:
SQL> DESCRIBE STUDENT1;
Name
Null?
-------------------------------------SID
NOT NULL
NAME
CITY
ADM_DATE
SEMESTER
ROLL_NO
COURSE
CONTACT_NO
▪
Type
----------------------------NUMBER
VARCHAR2(15)
VARCHAR2(15)
DATE
VARCHAR2(5)
NUMBER
VARCHAR2(6)
NUMBER
Rename the table as STUDENT2 if possible.
SQL STATEMENT
SQL> RENAME STUDENT1 TO STUDENT2;
Table renamed.
OUTPUT:
SQL> SELECT * FROM STUDENT2;
SID NAME
CITY
ADM_DATE SEMES ROLL_NO COURSE CONTACT_NO
---------- ----------------------------------------- -----------------------11 SONALI DAS
ISAMPUR
13-JUL-23
1ST
2
MCA
12 KUNAL DAS
SILIGURI
14-JUL-23
1ST
1
MCA
13 MUNNA ISLAM
JALPAIGURI 14-JUL-23
1ST
3
MCA
21 SOHEL RANA
MALDA
13-JUL-23
1ST
1
M.SC
22 TANMOY HALDER MALDA
13-JUL-23
1ST
2
M.SC
23 SUVRO SARKAR
JALPAIGURI 13-JUL-23
1ST
3
M.SC
33 KANISHKA SARKAR JALPAIGURI 18-JUL-23
2ND
1
M.TECH
31 RIPON GOSH
COOCHBEHAR 18-JUL-23
2ND
2
M.TECH
32 MRINMOY ROY
COOCHBEHAR 10-JUL-23
2ND
3
M.TECH
▪
Delete the “City” column from the table.
SQL STATEMENT:
ALTER TABLE STUDENT2 DROP COLUMN CITY;
Table altered.
OUTPUT:
SQL> SELECT * FROM STUDENT2;
SID
---------11
12
13
21
22
23
33
31
32
▪
NAME
ADM_DATE
----------------------SONALI DAS
13-JUL-23
KUNAL DAS
14-JUL-23
MUNNA ISLAM
14-JUL-23
SOHEL RANA
13-JUL-23
TANMOY HALDER 13-JUL-23
SUVRO SARKAR
13-JUL-23
KANISHKA SARKAR 18-JUL-23
RIPON GOSH
18-JUL-23
MRINMOY ROY
10-JUL-23
SEMES ROLL_NO
-------------1ST
2
1ST
1
1ST
3
1ST
1
1ST
2
1ST
3
2ND
1
2ND
2
2ND
3
COURSE CONTACT_NO
--------------MCA
MCA
MCA
M.SC
M.SC
M.SC
M.TECH
M.TECH
M.TECH
Rearrange the column as (Course, Sid, Semester, Roll, Name, Adm_date) if possible
SQL STATEMENT:
SQL> CREATE TABLE STUDENT_REORDERED AS SELECT COURSE, SID, SEMESTER, ROLL_NO,
NAME, ADM_DATE FROM STUDENT2;
Table created.
SQL> DROP TABLE STUDENT2;
Table dropped.
SQL> RENAME STUDENT_REORDERED TO STUDENT2;
Table renamed.
OUTPUT:
SQL> SELECT * FROM STUDENT2;
COURSE
-----MCA
MCA
MCA
M.SC
M.SC
M.SC
M.TECH
M.TECH
M.TECH
SID
SEMES ROLL_NO
---------- ----- ---------11
1ST
2
12
1ST
1
13
1ST
3
21
1ST
1
22
1ST
2
23
1ST
3
33
2ND
1
31
2ND
2
32
2ND
3
9 rows selected.
NAME
ADM_DATE
----------------------SONALI DAS
13-JUL-23
KUNAL DAS
14-JUL-23
MUNNA ISLAM
14-JUL-23
SOHEL RANA
13-JUL-23
TANMOY HALDER 13-JUL-23
SUVRO SARKAR
13-JUL-23
KANISHKA SARKAR 18-JUL-23
RIPON GOSH
18-JUL-23
MRINMOY ROY
10-JUL-23
ASSIGNMENT 3:
Consider the following tables of a relational database.
Employee (Empno, Empname, City)
Project (Pno, Pname)
Part (Partno, Partname, Color)
Use (Pno, Partno)
Works (Empno, Pno)
Create tables using appropriate SQL commands. Define all integrity constraints and enter sufficient data.
a) Show the name of the employee who are working on more than 1 project.
b) Show the name of the city where the maximum employees are located.
c) Show the part names which are used in all the projects.
d) Show the project-name in which no employees are working whose name starts with ‘S’.
e) Show the project-name which uses maximum red part.
f) Show the project-name which has used exactly 3 parts.
SOLUTION:
Create tables using appropriate SQL commands. Define all integrity constraints
SQL> CREATE TABLE EMPLOYEE(EMPNO VARCHAR2(3) PRIMARY KEY, EMPNAME
VARCHAR2(15) NOT NULL, CITY VARCHAR2(15) NOT NULL);
Table created.
SQL> CREATE TABLE PROJECT(PNO VARCHAR2(3) PRIMARY KEY, PNAME VARCHAR2(15) NOT
NULL);
Table created.
SQL> CREATE TABLE PART(PARTNO VARCHAR2(5) PRIMARY KEY, PARTNAME VARCHAR2(15)
NOT NULL, COLOUR VARCHAR2(10));
Table created.
SQL> CREATE TABLE USE ( PNO VARCHAR2(3) REFERENCES PROJECT(PNO) , PARTNO
VARCHAR2(5) REFERENCES PART(PARTNO));
Table created.
SQL> CREATE TABLE WORKS ( EMPNO VARCHAR2(3) REFERENCES EMPLOYEE(EMPNO) , PNO
VARCHAR2(3) REFERENCES PROJECT(PNO));
Table created.
OUTPUT:
SQL> DESCRIBE EMPLOYEE
Name
-------------------------------------EMPNO
EMPNAME
CITY
Null?
-------NOT NULL
NOT NULL
NOT NULL
Type
----------------------------------------------------------VARCHAR2(3)
VARCHAR2(15)
VARCHAR2(15)
SQL> DESCRIBE PROJECT
Name
----------------------------------PNO
PNAME
SQL> DESCRIBE PART
Name
--------------------------------PARTNO
PARTNAME
COLOUR
SQL> DESCRIBE USE
Name
------------------------------------PNO
PARTNO
SQL> DESCRIBE WORKS
Name
-----------------------------------EMPNO
PNO
Null?
-------NOT NULL
NOT NULL
Type
--------------------------------------------------------VARCHAR2(3)
VARCHAR2(15)
Null?
-------NOT NULL
NOT NULL
Type
------------------------------------------------VARCHAR2(5)
VARCHAR2(15)
VARCHAR2(10)
Null?
--------
Type
----------------------------------------------VARCHAR2(3)
VARCHAR2(5)
Null?
--------
Type
--------------------------------------VARCHAR2(3)
VARCHAR2(3)
Entering sufficient data.
SQL> INSERT INTO EMPLOYEE VALUES('E1','K SARKAR','ISLAMPUR');
1 row created.
SQL> INSERT INTO EMPLOYEE VALUES('E2','M ROY','JALPAIGURI');
1 row created.
SQL> INSERT INTO EMPLOYEE VALUES('E3','M SARKAR','COOCHBEHAR');
1 row created.
SQL> INSERT INTO EMPLOYEE VALUES('E4','T HALDER','JALPAIGURI');
1 row created.
SQL> INSERT INTO EMPLOYEE VALUES('E5','M DASGUPTA','JALPAIGURI');
1 row created.
SQL> INSERT INTO EMPLOYEE VALUES('E6','S DAS','ISLAMPUR');
1 row created.
SQL> INSERT INTO EMPLOYEE VALUES('E7','S SEN','ALIPURDUAR');
1 row created.
SQL> INSERT INTO PROJECT VALUES('PR1','ML');
1 row created.
SQL> INSERT INTO PROJECT VALUES('PR2','IP');
1 row created.
SQL> INSERT INTO PROJECT VALUES('PR3','NLP');
1 row created.
SQL> INSERT INTO PROJECT VALUES('PR4','NT');
1 row created.
SQL> INSERT INTO PART VALUES('PA1','LAPTOP-1','RED');
1 row created.
SQL> INSERT INTO PART VALUES('PA2','LAPTOP-2','GREEN');
1 row created.
SQL> INSERT INTO PART VALUES('PA3','LAPTOP-3','BLUE');
1 row created.
SQL> INSERT INTO PART VALUES('PA4','LAPTOP-4','RED');
1 row created.
SQL> INSERT INTO PART VALUES('PA5','PENDRIVE','RED');
1 row created.
SQL> INSERT INTO PART VALUES('PA6','PENDRIVE','WHITE');
1 row created.
SQL> INSERT INTO USE VALUES('PR1','PA1');
1 row created.
SQL> INSERT INTO USE VALUES('PR2','PA1');
1 row created.
SQL> INSERT INTO USE VALUES('PR2','PA2');
1 row created.
SQL> INSERT INTO USE VALUES('PR3','PA1');
1 row created.
SQL> INSERT INTO USE VALUES('PR3','PA2');
1 row created.
SQL> INSERT INTO USE VALUES('PR3','PA3');
1 row created.
SQL> INSERT INTO USE VALUES('PR1','PA5');
1 row created.
SQL> INSERT INTO USE VALUES('PR1','PA4');
1 row created.
SQL> INSERT INTO USE VALUES('PR4','PA1');
1 row created.
SQL> INSERT INTO USE VALUES('PR4','PA2');
1 row created.
SQL> INSERT INTO WORKS VALUES('E6','PR1');
1 row created.
SQL> INSERT INTO WORKS VALUES('E7','PR2');
1 row created.
SQL> INSERT INTO WORKS VALUES('E7','PR1');
1 row created.
SQL> INSERT INTO WORKS VALUES('E1','PR1');
1 row created.
SQL> INSERT INTO WORKS VALUES('E2','PR3');
1 row created.
SQL> INSERT INTO WORKS VALUES('E3','PR4');
1 row created.
SQL> INSERT INTO WORKS VALUES('E4','PR3');
1 row created.
SQL> INSERT INTO WORKS VALUES('E5','PR1');
1 row created.
OUTPUT:
SQL> SELECT * FROM EMPLOYEE ;
EMP EMPNAME
CITY
--- --------------- --------------E1 K SARKAR
ISLAMPUR
E2 M ROY
JALPAIGURI
E3 M SARKAR
COOCHBEHAR
E4 T HALDER
JALPAIGURI
E5 M DASGUPTA JALPAIGURI
E6 S DAS
ISLAMPUR
E7 S SEN
ALIPURDUAR
SQL> SELECT * FROM PROJECT;
PNO
--PR1
PR2
PR3
PR4
PNAME
--------------ML
IP
NLP
NT
SQL> SELECT * FROM PART;
PARTN
----PA1
PA2
PA3
PA4
PA5
PA6
PARTNAME
--------------LAPTOP-1
LAPTOP-2
LAPTOP-3
LAPTOP-4
PENDRIVE
PENDRIVE
SQL> SELECT * FROM USE;
PNO
--PR1
PR2
PR2
PR3
PR3
PR3
PR1
PR1
PR4
PR4
PARTN
----PA1
PA1
PA2
PA1
PA2
PA3
PA5
PA4
PA1
PA2
COLOUR
---------RED
GREEN
BLUE
RED
RED
WHITE
SQL> SELECT * FROM WORKS;
EMP
--E6
E7
E7
E1
E2
E3
E4
E5
PNO
--PR1
PR2
PR1
PR1
PR3
PR4
PR3
PR1
8 rows selected.
▪
Show the name of the employee who are working on more than 1 project.
QUERY:
SQL> SELECT * FROM EMPLOYEE WHERE EMPNO=(SELECT EMPNO FROM (SELECT
EMPNO,COUNT(*) AS NOP FROM WORKS GROUP BY EMPNO HAVING COUNT(*)>1) );
OUTPUT:
EMP EMPNAME
----------------E7
S SEN
▪
▪
CITY
--------------ALIPURDUAR
Show the name of the city where the maximum employees are located.
QUERY:
SQL> SELECT CITY FROM (SELECT CITY,COUNT(*) AS CNT FROM EMPLOYEE GROUP BY CITY)
WHERE CNT= ( SELECT MAX(COUNT(*)) AS CNT FROM EMPLOYEE GROUP BY CITY);
OUTPUT:
CITY
--------------JALPAIGURI
Show the part names which are used in all the projects.
QUERY:
SQL> CREATE VIEW T1 AS (SELECT PARTNO FROM USE);
View created.
SQL> CREATE VIEW T2 AS (SELECT PARTNO FROM (SELECT PNO,PARTNO FROM PROJECT,T1
MINUS SELECT * FROM USE));
View created.
SQL> SELECT * FROM T1 MINUS SELECT * FROM T2;
OUTPUT:
PARTN
----PA1
▪
Show the project-name in which no employees are working whose name starts with ‘S’.
QUERY
SQL> SELECT DISTINCT PNAME FROM (SELECT PNO FROM WORKS WHERE PNO NOT IN
(SELECT PNO FROM (SELECT EMPNO FROM EMPLOYEE WHERE EMPNAME LIKE 'S%')
NATURAL JOIN WORKS)) NATURAL JOIN PROJECT;
OUTPUT:
PNAME
--------------NLP
NT
▪
Show the project-name which uses maximum red part.
QUERY:
SQL> CREATE VIEW T AS SELECT PNAME,COUNT(*) AS CNT FROM (SELECT * FROM PART
WHERE COLOUR='RED') NATURAL JOIN PROJECT NATURAL JOIN USE GROUP BY PNAME;
View created.
SQL> SELECT PNAME FROM T WHERE CNT=(SELECT MAX(CNT) FROM T );
OUTPUT:
PNAME
--------------ML
▪
Show the project-name which has used exactly 3 parts
QUERY:
SQL> SELECT PNAME FROM (SELECT PNO,COUNT(*) AS CNT FROM USE GROUP BY PNO)
NATURAL JOIN PROJECT WHERE CNT=3 ;
OUTPUT:
PNAME
--------------NLP
ASSIGNMENT -4
Consider the following tables of a relational database.
Aircraft (Aid, type)
Flights (Fno, Aid, Company)
Employees (Eid, Ename, Salary)
Certified (Eid, Aid)
Schedule (Eid, Fno, Source_city, Destination_city, Date)
Create tables using appropriate SQL commands.
Define all integrity constraints and enter sufficient data.
a) Show the name of the employee who has certification on more than 1 type of aircrafts.
b) Show the name of the employee who has no certification.
c) Show the name of the city where from maximum flight takes off (Source_city).
d) Show the name of the company which spend maximum salary for their employees.
e) Show the name of the company which has maximum type of flight.
Create tables using appropriate SQL commands.
SQL> CREATE TABLE Aircraft (Aid NUMBER PRIMARY KEY,Type VARCHAR2(22) NOT NULL);
Table created.
SQL> CREATE TABLE Flights (Fno NUMBER PRIMARY KEY,Aid NUMBER(8),Company
VARCHAR2(22));
Table created.
SQL> CREATE TABLE Employees (Eid NUMBER PRIMARY KEY,Ename VARCHAR2(22) NOT NULL,
Salary NUMBER(22) NOT NULL);
Table created.
CREATE TABLE Certified (Eid NUMBER REFERENCES EMPLOYEES(EID) ,Aid NUMBER
REFERENCES AIRCRAFT(AID));
Table created.
SQL> CREATE TABLE Schedule (Eid NUMBER REFERENCES EMPLOYEES(EID),Fno NUMBER
REFERENCES FLIGHTS(FNO),Source_city VARCHAR2(22), DESTINATION_CITY VARCHAR2(22),
DATE_ DATE);
Table created.
OUTPUT
SQL> DESCRIBE AIRCRAFT
Name
Null?
-----------------------------------AID
NOT NULL
TYPE
NOT NULL
Type
-----------------------------------------NUMBER
VARCHAR2(22)
SQL> DESCRIBE Flights
Name
Null? Type
------------------------------------ -----------------------------------------------------FNO
NOT NULL
NUMBER
AID
NUMBER(8)
COMPANY
VARCHAR2(22)
SQL> DESCRIBE EMPLOYEES
Name
Null?
Type
-----------------------------------------------------------------------------------EID
NOT NULL
NUMBER
ENAME
NOT NULL
VARCHAR2(22)
SALARY
NOT NULL
NUMBER(22)
SQL> DESCRIBE Certified
Name
----------------------------------------EID
AID
Null? Type
--------
SQL> DESCRIBE SCHEDULE
Name
Null?
---------------------------------------------- -------EID
FNO
SOURCE_CITY
DESTINATION_CITY
DATE_
Entering sufficient data
SQL> INSERT INTO Aircraft VALUES (1, 'A1');
1 row created.
SQL> INSERT INTO Aircraft VALUES (2, 'A2');
1 row created.
SQL> INSERT INTO Aircraft VALUES (3, 'A3');
1 row created.
OUTPUT:
AID
---------1
2
3
TYPE
---------------------A1
A2
A3
SQL> INSERT INTO Flights VALUES (1001, 1,'X');
1 row created.
SQL> INSERT INTO Flights VALUES (1002, 1,'X');
1 row created.
SQL> INSERT INTO Flights VALUES (1003, 2,'X');
1 row created.
SQL> INSERT INTO Flights VALUES (1004, 3,'X');
1 row created.
SQL> INSERT INTO Flights VALUES (2001, 1,'Y');
1 row created.
SQL> INSERT INTO Flights VALUES (2002, 3,'Y');
1 row created.
SQL> INSERT INTO Flights VALUES (3001, 2,'Z');
--------------------------------NUMBER
NUMBER
Type
-------------------------------NUMBER
NUMBER
VARCHAR2(22)
VARCHAR2(22)
DATE
OUTPUT:
SQL> SELECT * FROM Flights;
FNO
---------1001
1002
1003
1004
2001
2002
3001
AID
---------1
1
2
3
1
3
2
COMPANY
---------------------X
X
X
X
Y
Y
Z
SQL> INSERT INTO Employees VALUES (11, 'AKASH SUR','50000');
1 row created.
SQL> INSERT INTO Employees VALUES (12, 'PRANAB DAS','60000');
1 row created.
SQL> INSERT INTO Employees VALUES (13, 'UJJAL DEY','45000');
1 row created.
SQL> INSERT INTO Employees VALUES (14, 'MANIK DEBNATH','80000');
1 row created.
SQL> INSERT INTO Employees VALUES (15, 'NABARUN BAR','15000');
1 row created.
OUTPUT:
SQL> SELECT * FROM Employees;
EID
ENAME
------------------------------11 AKASH SUR
12 PRANAB DAS
13 UJJAL DEY
14 MANIK DEBNATH
15 NABARUN BAR
SALARY
---------50000
60000
45000
80000
15000
SQL> INSERT INTO Certified VALUES (14, 1);
1 row created.
SQL> INSERT INTO Certified VALUES (14, 2);
1 row created.
SQL> INSERT INTO Certified VALUES (14, 3);
1 row created.
SQL> INSERT INTO Certified VALUES (13, 1);
1 row created.
SQL> INSERT INTO Certified VALUES (12, 1);
1 row created.
SQL> INSERT INTO Certified VALUES (12, 2);
1 row created.
SQL> INSERT INTO Certified VALUES (11, 1);
1 row created.
OUTPUT:
SQL> SELECT * FROM CERTIFIED;
EID
AID
---------- ---------14
1
14
2
14
3
13
1
12
1
12
2
11
1
SQL> INSERT INTO Schedule VALUES (14,1001,'COOCHBEHAR','KOLKATA','14-AUG-24');
1 row created.
SQL> INSERT INTO Schedule VALUES (14,1001,'KOLKATA','COOCHBEHAR','15-AUG-24');
1 row created.
SQL> INSERT INTO Schedule VALUES (14,1002,'COOCHBEHAR','BAGDOGRA','15-AUG-24');
1 row created.
SQL> INSERT INTO Schedule VALUES (14,1002,'BAGDOGRA','KOLKATA','16-AUG-24');
1 row created.
SQL> INSERT INTO Schedule VALUES (14,1003,'KOLKATA','GWAHATI','16-AUG-24');
1 row created.
SQL> INSERT INTO Schedule VALUES (14,1004,'GWAHATI','KOLKATA','17-AUG-24');
1 row created.
SQL> INSERT INTO Schedule VALUES (13,2001,'COOCHBEHAR','KOLKATA','14-AUG-24');
1 row created.
SQL> INSERT INTO Schedule VALUES (13,1001,'COOCHBEHAR','KOLKATA','15-AUG-24');
1 row created.
SQL> INSERT INTO Schedule VALUES (12,1004,'KOLKATA','BAGDOGRA','17-AUG-24');
1 row created.
SQL> INSERT INTO Schedule VALUES (12,2002,'GWAHATI','BAGDOGRA','17-AUG-24');
1 row created.
SQL> INSERT INTO Schedule VALUES (11,3001,'COOCHBEHAR','BAGDOGRA','17-AUG-24');
1 row created.
OUTPUT:
SQL> SELECT * FROM SCHEDULE;
EID
FNO SOURCE_CITY
DESTINATION_CITY
DATE_
---------- ---------- ---------------------- ---------------------- --------14
1001 COOCHBEHAR
KOLKATA
14-AUG-24
14
1001 KOLKATA
COOCHBEHAR
15-AUG-24
14
1002 COOCHBEHAR
BAGDOGRA
15-AUG-24
14
1002 BAGDOGRA
KOLKATA
16-AUG-24
14
1003 KOLKATA
GWAHATI
16-AUG-24
14
1004 GWAHATI
KOLKATA
17-AUG-24
13
13
12
12
11
▪
2001 COOCHBEHAR
1001 COOCHBEHAR
1004 KOLKATA
2002 GWAHATI
3001 COOCHBEHAR
KOLKATA
KOLKATA
BAGDOGRA
BAGDOGRA
BAGDOGRA
14-AUG-24
15-AUG-24
17-AUG-24
17-AUG-24
17-AUG-24
Show the name of the employee who has certification on more than 1 type of aircrafts.
SQL> SELECT ENAME FROM EMPLOYEES WHERE EID IN (SELECT EID FROM (SELECT
EID,COUNT(*) AS CNT FROM CERTIFIED GROUP BY EID) WHERE CNT>1);
ENAME
---------------------PRANAB DAS
MANIK DEBNATH
▪
Show the name of the employee who has no certification.
QUERY:
SQL> SELECT EID FROM EMPLOYEES WHERE EID NOT IN (SELECT DISTINCT EID FROM
CERTIFIED);
OUTPUT:
EID
---------15
▪
Show the name of the city where from maximum flight takes off (Source_city).
QUERY:
SQL> SELECT Source_CITY FROM (SELECT Source_city,COUNT(*) AS CNT FROM SCHEDULE
GROUP BY(Source_city)) WHERE CNT=(SELECT MAX(COUNT(*)) FROM SCHEDULE GROUP BY
(Source_city) );
OUTPUT:
SOURCE_CITY
---------------------COOCHBEHAR
▪
Show the name of the company which spend maximum salary for their employees.
QUERY:
SQL> SELECT COMPANY FROM (SELECT COMPANY,SUM(SALARY)AS TOTAL_SALARY FROM F
GROUP BY(COMPANY)) WHERE TOTAL_SALARY= (SELECT MAX(SUM(SALARY)) FROM F
GROUP BY (COMPANY));
OUTPUT:
COMPANY
---------------------X
▪
Show the name of the company which has maximum type of flight
QUERY:
SQL> SELECT COMPANY FROM (SELECT COMPANY,COUNT(DISTINCT AID) AS
TYPE_OF_FLIGHT FROM FLIGHTS GROUP BY COMPANY) WHERE TYPE_OF_FLIGHT=(SELECT
MAX(COUNT(DISTINCT AID)) FROM FLIGHTS GROUP BY COMPANY);
OUTPUT:
COMPANY
---------------------X
Download