You are on page 1of 14

COMPUTER

SCIENCE

SQL
ACKNOWLEDGEMENT
I would like to express my special gratitude to my
teacher who gave me the golden opportunity to do
this wonderful project on the topic “ STRUCTURED
QUERY LANGUAGE “ which also helped me in doing
a lot of Research and I came to know about so many
new things I am really thankful to them.
Secondly I would also like to thank my parents and
friends who helped me a lot in finalizing this project
within the limited time frame.
INDEX
SNO. NAME OF TABLE TEACHER’S SIGN
SCHOOL
1 ADMIN

WORKER
2 PAYLEVEL

PRODUCT
3 CLIENT

SENDER
4 RECEPIENT

STORE
5 ITEM

EMPLOYEE
6 SALGRADE

CONSIGNER
7 CONSIGNEE

CARDEN
8 CUSTOMER

DEPT
9 EMPLOYEE

GAMES
10 PLAYERS
TABLE 1
QUES 1 :Consider the following tables SCHOOL and ADMIN. Write SQL commands for the statements (i) to (iv) and
give outputs for SQL queries (v) to (viii).
SCHOOL
CODE TEACHER NAME SUBJECT DOJ PERIODS EXPERIENCE
1001 RAVI SHANKAR ENGLISH 12/03/2000 24 10
1009 PRIYA RAI PHYSICS 03/09/1998 26 12
1203 LISA ANAND ENGLISH 09/04/2000 27 5
1045 YASHRAJ MATHS 24/08/2000 24 15
1123 GANAN PHYSICS 16/07/1999 28 3
1167 HARISH B CHEMISTRY 19/10/1999 27 5
1215 UMESH PHYSICS 11/05/1998 22 16
ADMIN

CODE GENDER DESIGNATION


1001 MALE VICE PRINCIPAL
1009 FEMALE COORDINATOR
1203 FEMALE COORDINATOR
1045 MALE HOD
1123 MALE SENIOR TEACHER
1167 MALE SENIORTEACHER
1215 MALE HOD
i) To display TEACHERNAME, PERIODS of all teachers whose periods less than 25.
Select TEACHERNAME , PERIODS from SCHOOL where PERIODS> 25 ;
ii) To display TEACHERNAME, CODE and DESIGNATION from tables SCHOOL and ADMIN
whose gender is male.
Select TEACHER NAME , CODE , DESIGNATION from SCHOOL , ADMIN where GENDER = ‘MALE’;
iii) To display number of teachers in each subject wise.
Select count(*) from TEACHER group by SUBJECT;
iv) To display CODE, TEACHERNAME and SUBJECT of all teachers who have joined the school
after 01/01/1999.
Select CODE , TEACHERNAME , SUBJECT from SCHOOL where DOJ > 01/01/1999;
v) SELECT MAX (EXPERIENCE), SUBJECT FROM SCHOOL GROUP BY SUBJECT;

ENGLISH 10
PHYSICS 16
MATHS 15
CHEMISTRY 5

vi) SELECT TEACHERNAME, GENDER FROM SCHOOL, ADMIN WHERE DESIGNATION


= ‘COORDINATOR’ AND SCHOOL.CODE=ADMIN.CODE;
PRIYA RAI FEMALE
LISA ANAND FEMALE

vii)SELECT DESIGNATION, COUNT (*) FROM ADMIN GROUP BY DESIGNATION


HAVING COUNT (*) <2;
1001 RAVI ENGLISH 12/03/2000 24 10 MALE VICE
SHANKAR PRINCIPAL

viii) SELECT COUNT (DISTINCT SUBJECT) FROM SCHOOL;


4
TABLE 2

QUES2: Consider the following tables WORKER and PAYLEVEL and answer (a) and (b) parts of this questions:

Table: WORKER
ECODE NAME DESIGN PLEVEL DOJ DOB
11 RadheShyam Supervisor P001 13-Sep-2004 23-Aug-1981
12 ChanderNath Operator P003 22-Feb-2010 12-Jul-1987
13 Fizza Operator P003 14-Jun-2009 14-Oct-1983
15 Ameen Ahmed Mechanic P002 21-Aug-2006 13-Mar-1984
18 Sanya Clerk P002 19-Dec-2005 09-Jan-1983

Table: PAYLEVEL
PLEVEL PAY ALLOWENCE
P001 26000 12000
P002 22000 10000
P003 12000 6000
a) Write SQL commands for the following statement:
1. To display the details of all WORKERS in descending order of DOB.
Select * from WORKERS order by DOB desc;
2. To display NAME and DESIGN of those WORKERS, whose PLEEL is either P001 or P002.
Select NAME , DESIGN from WORKERS where PLEVEL In ( P001 , P002);
3. To display the content of all the WORKERS table, whose DOB is in between
‘19-JAN-1984’ and ’18-JAN-1987’.
Select * from WORKER where DOB between ‘19-JAN-1984’ and ’18-JAN-1987’;
4. To add a new row with the following:
19, ‘Daya Kishore’, ‘Operator’, ‘P003’, ’19-JUN-2008’, ’11-JUL-1984’
Insert into WORKER
Values (19, ‘Daya Kishore’, ‘Operator’, ‘P003’, ’19-JUN-2008’, ’11-JUL-1984’ );

b) Give the output of the following SQL queries:


1. SELECT COUNT (PLEVEL), PLEVEL FROM WORKER GROUP BY PLEVEL;
3
2. SELECT MAX(DOB), MIN(DOJ) FROM WORKER;
MAX : 12-Jul-1987
MIN : 13- Sep - 2004
3. SELECT Name, pay FROM WORKER W, PAYLEVEL P WHERE W.PLEVEL=S.PLEVEL AND P.ECODE<13;
RadheShyam 26000
ChanderNath 22000

4. SELECT PLEVEL, PAY+ALLOWENCE FROM PAYLEVEL WHERE PLEVEL=‘P003’;


18000
TABLE 3

QUES 3 : Consider the following tables Product and Client. Write SQL commands for the statement (i) to
(iv) and give outputs for SQL queries (v) to (viii)
Table: PRODUCT
P_ID Product Name Manufacturer Price
TP01 TalcomPowder LAK 40
FW05 Face Wash ABC 45
BS01 Bath Soap ABC 55
SH06 Shampoo XYZ 120
FW12 Face Wash XYZ 95
Table: CLIENT
C_ID Client Name City P_ID
01 Cosmetic Shop Delhi FW05
06 Total Health Mumbai BS01
12 Live Life Delhi SH06
15 Pretty Woman Delhi FW12
16 Dreams Banglore TP01
(i) To display the details of those Clients whose city is Delhi.
Select * from Client where City=”Delhi”;
(ii) To display the details of Products whose Price is in the range of 50 to 100.
Select * from product where Price between 50 and 100;
(iii) To display the ClientName, City from table Client, and ProductName and Price from table Product, with their
corresponding matching P_ID.
Select ClientName,City,ProductName, Price from Product,Client where Product.P_ID=Client.P_ID.
(iv) To increase the Price of all Products by 10
Update Product Set Price=Price +10;
(v) SELECT DISTINCT City FROM Client
City
Delhi
Mumbai
Bangalore
(vi) SELECT Manufacturer, MAX(Price), Min(Price), Count(*) FROM Product GROUP BY Manufacturer;

Manufacturer Max( Price) Min( Price) Count(*)


LAK 40 40 1
ABC 55 45 2
XYZ 120 95 2

(vii) SELECT ClientName, ManufacturerName FROM Product, Client WHERE Client.Prod_Id=Product.P_Id;

Client Name Manufacturer


Cosmetic Shop ABC
Total Health ABC
Live Life XYZ
Pretty Woman XYZ
Dreams LAK

(viii) SELECT ProductName, Price * 4 FROM Product.


Product Name Price*4
Talcom Powder 160
Face Wash 180
Bath Soap 220
Shampoo 480
Face Wash 380

QUES 4 ;Consider the following tables. Write SQL command for the statements (i)to(iv)and give outputs for the
SQL quries (v) to (viii).

TABLE : SENDER

Sender
SenderID SenderName Sender Address
City
ND01 R jain 2,ABC Appts New Delhi
MU02 H sinha 12, Newton Mumbai
27/ A,Park
MU1 5 S haj New Delhi
Street
ND5 0 T Prasad 122-K,SDA Kolkata

TABLE :RECIPIENT

RecID SenderID ReCName RecAddress ReCCity


KO05 ND01 RBajpayee 5,Central Avenue Kolkata
ND08 MU0 2 S Mahajan 116, A Vihar NewDelhi
MU19 ND01 H sing 2A,Andheri East Mumbai
MU32 MU1 5 PK Swamy B5, CS erminus Mumbai
13, B1
ND48 ND50 S Tripathi NewDelhi
D,MayurVihar
(i) To display the names of all senders from Mumbai.
Select * from Sender where SenderCity =’Mumbai’;
(ii) To display the recID, senderName, senderAddress, RecName, RecAddress for every recipt.
Select recID, SenderName, SenderAddress, RecName, RecAddress from Sender, Recipient where
Sender.Senderid=Recipient.RenderId;
(iii) To display the sender details in ascending order of SenderName.
Select * from Sender order by SenderName;
(iv) To display number of Recipients from each city.
Select RecCity,Count(*) from Recipient group by RecCity;
(v) SELECT Sender Name from Sender where Sender City;
T Prasad
(vi) SELECT A.SenderName A, B.RecName FROM Sender A, Recipient B WHERE A.SenderID=B. SenderID AND
B.RecCity=’Mumbai’;
SenderNameRecName
R.JainH.Singh
S.JhaP.K.Swamy
(vii) SELECT RecName,RecAddressFROMRecipient WHERE RecCity Not IN (‘Mumbai’,Kolkata’);
RecNameRecAddressS
Mahajan 116, A Vihar
S Tripati 13, B1 D, MayurVihar
(viii) SELECT RecID, RecName FROM Recipient WHERE SenderID = ‘MU02’ OR SenderID = ‘ND50’;
RecIDRecName
ND08 S Mahajan
ND48 S Tripathi

QUES 5 : Consider the following tables STORE and ITEM and answer the following questions :-
TABLE : STORE
SNo SName Area
S01 ABC Conputronics GK 2
S02 All Infotech Media CP
S03 Tech Shoppe Nehru Palace
S04 Geeks Techno Soft Nehru Palace
S05 Hitech Tech Store CP

TABLE: ITEM
INo IName Price SNo
T01 Mother board 12000 S01
T02 Hard Disk 5000 S01
T03 Keyboard 500 S02
T04 Mouse 300 S01
T05 Mother Board 13000 S02
T06 Key Board 400 S03
T07 LCD 6000 S04
T08 LCD 5500 S05
T09 Mouse 350 S05
T10 Hard Disk 4500 S03

A) WRITE THE SQL QUERY FOR THE FOLLOWING:-


1. To display IName and Price of all the items in ascending order of their price.
Select IName , Price from ITEM order by Price;
2. To display SNoand SName of all stores located at CP.
Select SName ,SNo from STORE where Area = “CP” ;
3. To display min and max price of each IName from the table ITEM .
Select IName , Min(Price), Max(Price) from ITEM group by IName ;
4. To display IName , Price of all items and their respective SName where they are available .
Select IName , Price ,SName from ITEM , STORE where ITEM.SNo=STORE.SNo;

B) Write the outputs of the following :-


1. Select Distinct IName from ITEM where price>=5000;
Mother Board
Hard Disk
LCD
2. Select Area , count(Distinct Area ) from STORE;
3
3. Select Area count(*) from STORE group by Area ;
GK 2 1
CP 2
Nehru Palace 2

4. Select IName ,Price *0.05 discount from ITEM where SNo In(‘S02’,’S03’);
IName Discount
Key Board 25
Mother Board 650
Keyboard 20
Hard Disk 225

QUES 6 : Consider the following tables EMPLOYEE and SALGRADE and answer the following:-

TABLE : EMPLOYEE
ECODE NAME DESIG SGRADE DOJ DOB
101 ABDUL EXECUTIVE S03 23-03-2003 13-01-1980
102 RAVI HEAD – IT S02 12-02-2010 22-07-1987
103 JOHN RECEPTIONIST S03 24-06-2009 24-02-1983
105 NAZAR GM S02 11-08-2006 03-03-1984
108 PRIYAM CEO S01 29-12-2004 19-01-1982

TABLE : SALGRADE
SGRADE SALARY HRA
S01 56000 18000
S02 32000 12000
S03 24000 8000

WRITE SQL COMMANDS OF THE FOLLOWING:-


1. To display the details of all employee in ascending order of DOJ .
Select * from EMPLOYEE order by DOJ desc;
2. To display NAME and DESIG of those employees, whose SALGRADE is either SO2 or S03.
Select NAME , DESIG from EMPLOYEE where SALGRADE In(‘S02’, ‘S03’)
3. To display the content of all the employees table whose DOJ is in between ’09-02-2006’ and ’08-08-2009’.
Select * from EMPLOYEE where DOJ between ‘ 09-02-2006’ and ’08-08-2009’;
4. To add a new row with the following : 19, ‘HARISH’, ‘HEAD – IT ‘,’S02’ , ’09-09-2007’ , ’21-04-1983’.
Update EMPLOYEE
Values( 19,‘HARISH’, ‘HEAD – IT ‘,’S02’ , ’09-09-2007’ , ’21-04-1983’);
WRITE OUTPUTS OF THE FOLLOWING:-
1. SELECT COUNT ( SGRADE), SGRADE FROM EMPLOYEE GROUP BY SGRADE;
COUNT SGRADE
2 S03
2 S02
1 S01

2. SELECT MIN (DOB) , MAX(DOJ) FROM EMPLOYEE;


13-01-1980 12-02-2010
3. SELECT NAME, SALARY FROM EMPLOYEE E , SALGRADE S WHERE E.SGRADE=S.SGRADE AND
E.ECODE<103;
NAME SALARY
ABDUL 24000
RAVI 32000

4. SELECT SGRADE , SALARY + HRA FROM SALGRADE WHERE SGRADE =’S02’ ;


SGRADE SALARY+HRA
S02 4400
QUES: 7. Consider the following tables Consignor and Consignee. Write SQL commands for the statement (i) to (iv)
and give outputs for SQL queries (v) to (viii).
Table: CONSIGNER
CnorID CnorName CnorAddress City
ND01 R Singhal 24, ABC Enclave New Delhi
ND02 Amit Kumar 123, Palm Avenue New Delhi
MU15 R Kohli 5/A, South Street Mumbai
MU50 S Kaur 27-K, Westend Mumbai

Table: CONSIGNEE
CneeID CnorId CneeName CneeAddress CneeCity
MU05 ND01 Rahul Kishore 5, Park Avenue Mumbai
ND08 ND02 P Dhingra 16/J, Moore Enclave New Delhi
KO19 MU15 A P Roy 2A, Central Avenue Kolkata
MU32 ND02 S Mittal P245, AB Colony Mumbai
ND48 MU50 B P Jain 13, Block D, A Vihar New Delhi

(i) To display the names of all Consigner from Mumbai.


Select CnorName from CONSIGNER where City=”Mumbai” ;
(ii) To display the CneeID, CnorName, CnorAddress, CeeName, CneeAddress for every Consignee.
Select CneeID ,CNor Name , CNorAddress , CneeName , CNee Address from CONSIGNER , CONSIGNEE
where CONSIGNER .CNor ID= CONSIGNEE .CNorID
(iii) To display consignee details in ascending order of CneeName.
Select * from CONSIGNEE order by CNee Name ASC;
(iv) To display number of consignors from each city.
Select CNee City, Count(CNeeCity) from CONSIGNEE group by CNeeCity;
(v) SELECT DISTINCT CNeeCity FOM CONSIGNEE;
CNeeCity
Mumbai
New Delhi
Kolkata
(vi) SELECT A.CnorName, B.CneeName
FROM Consignor A, Consignee B
WHERE A.Cnor.ID=B.CnorID AND B.CneeCity=’Mumbai’;

CnorName CneeName
R singhal Rahul Kishore
Amit Kumar S mittal

(vii) SELECT CneeName, CneeAddress FROM Consignee WHERE CneeCity NOT IN (‘Mumbai’, ‘Kolkata’);
CneeName CneeAddress
P Dhingra 16/j,Moore Enclave
B P jain 13,Block d,a,viha

(viii) SELECT CneeID, CneeName FROM Consignee WHERE CnorID= ‘MU15’n R CnorID = ‘ND01’;
MU05 Rahul Kishore
K019 AP ROY
QUES: 8. Consider the following tales CARDEN and CUSTOMER and answer (a) and (b) parts of this questions:
Table: CARDEN
CCODE Car Name Make Color Capacity Charges
501 A-Star Suzuki RED 3 14
503 Indigo Tata SILVER 3 12
502 Innova Toyota WHITE 7 15
509 SX4 Suzuki SILVER 4 14
510 C-Class Mercedes RED 4 35
Table: CUSTOMER
Code CName Ccode
1001 Hemant Sahu 501
1002 Raj Lal 509
1003 Feroza Shah 503
1004 Ketan Dhal 502

a) Write SQL commands for the following statements:


(i) To display the names of all the silver colored cars.
Select Car Name from CARDEN where color = ‘ SILVER ‘;
(ii) To display name of car, make and capacity of cars in descending order of their sitting capacity.
Select Car Name , Make , Capacity from CARDEN order by Capacity desc;
(iii) To display the highest charges at which a vehicle can be hired from CARDEN.
Select max(Charges) from CARDEN;
(iv) To display the customers names and the corresponding name of the cars hired by them.
Select B.Name ,A.Car Name from CARDEN A , CUSTOMER B where A.Ccode=B.Ccode
b) Give the output of the following SQL queries:
(i) SELECT COUT (DISTINCT Make) FROM CARDEN;
4
(ii) SELECT MAX(Charges), MIN (Charges) FROM CARDEN;
MAX: 35
MIN:12
(iii) SELECT CarName from CARDEN where color = ‘SILVER’;
SX4
(iv) SELECT CarName FROM CARDEN WHERE Capacity=4;
Suzuki
Mercedes
QUES : 9 Consider the following tables and answer the following:-
TABLE : DEPT
DCODE DEPARTMENT LOCATION
D01 INFRASTRUCTURE DELHI
D02 MARKETING DELHI
D03 MEDIA MUMBAI
D05 FINANCE KOLKATA
D04 HUMAN RESOURCE MUMBAI
TABLE: EMPLOYEE
WNO NAME DOJ DOB GENDER DCODE
1001 George K 2013-09-02 1991-09-01 MALE D01
1002 Ryma Sen 2012-12-11 1990-12-15 FEMALE D03
1003 Mohitesh 2013-02-03 1987-09-04 MALE D05
1007 Anil Jha 2014-01-17 1984-10-19 MALE D04
1004 Manila Sahai 2012-12-09 1986-11-14 FEMALE D01
1005 R Sahay 2013-11-18 1987-03-31 MALE D02
1006 Jaya Priya 2014-06-09 1985-06-23 FEMALE D05

1. To display WNO , name , gender, from the table EMPLOYEE in descending order of WNO.
Select WNO , Name , Gender , from EMPLOYEE order by WNO desc;
2. To display the name of all the MALE employee from the table.
Select Name from EMPLOYEE where Gender=’MALE’;
3. To display the WNO and name of those workers from the table EMPLOYEE who are born between ‘1987-
01-01’ and ‘1991-12-01’ .
Select WNO, Name from EMPLOYEE where DOB between ‘1987-01-01’ and ‘1991-12-01’ ;

4. To count and display female employees who have joined after ‘1986-01-01’.
Select count (FEMALE) from EMPLOYEE where DOJ>’1986-01-01’;
5. SELECT COUNT(*), DCODE FROM EMPLOYEE GROUP BY DCODE HAVING COUNT(*) >1;
5
6. SELECT DISTINCT DEPARTMENT FROM DEPT;
MEDIA
MARKETING
INFRASTRUCTURE
FINANCE
HUMAN RESOURCE
7. SELECT NAME , DEPARTMENT , FROM EMPLOYEE E , DEPT D WHERE E.DCODE=D.DCODE AND WNO<1003;
MEDIA DELHI
INFRASTRUCTURE MUMBAI

8. SELECT MAX(DOJ) , MIN(DOB) FROM EMPLOYEE;


MAX:- 2014-06-09 ; MIN :- 1984-10-19
QUES :10 Consider the following tables and answer the following:-
TABLE : GAMES
Gcodes Game Name Number Prize Money Date
101 Carom Board 2 5000 23-Jan-2016
102 Badminton 2 12000 12-Dec -2016
103 Table Tennis 4 8000 14-Feb-2016
105 Chess 2 9000 01-Jan-2016
108 Lawn Tennis 4 25000 19-March-2016
TABLE: PLAYER
Pcode Name Gcode
1 Nabi Ahmad 101
2 Ravi 108
3 Jatin 101
4 Varun 103

1. To displaythe names of all the games with their Gcodes.


Select Game Name , code from GAMES;
2. To display details of those Games which are having price money morethan 7000.
Select * from GAMES where Prize Money>7000;
3. To display the content of the games in ascending order by Date.
Select * from GAMES order by Date;
4. To display sum of prize money for each of the number of participants groupings.
Select sum (Prize Money), Number from GAMES group by Number;
5. Select count(distinct number) from GAMES;
2
6. Select max( Date ) , min(Date) from GAMES;
19-March-2016 ; 12-Dec-2016
7. Select sum ( Prize Money) from GAMES;
59000
8. Select distinct Gcodes from PLAYER;
101
103
108

You might also like