SQL Programs

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

SQL PROGRAMS

Q.No 1) Consider the following table PRODUCT. Write SQL commands for the statements
(i) to (v)
Table: PRODUCT
P_ID ProductName 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

CREATE TABLE PRODUCT


(P_ID char(4) PRIMARY KEY,
ProductName char(20) NOT NULL,
Manufacturer char(3),
Price integer);

INSERT INTO PRODUCT VALUES(‘TP01’, ‘Talcom Powder’, ‘LAK’, 40) ;

i) To display the details of those Products whose ProductName is ‘Face Wash’.

Ans: Select * from PRODUCT where ProductName = ‘Face Wash’ ;

(ii) To display the details of Products whose Price is in the range of 50 to 100(Both values
included).

Ans: Select * from product where Price between 50 and 100 ;

(iii) To display the ProductName, Manufacturer in ascending order of Manufacturer.

Ans: Select ProductName, Manufacturer from Product order by Manufacturer ;

(iv) To increase the Price of all Products by 10


Ans: Update Product
Set Price=Price +10 ;

(v) To display the Maximum Price of the Product whose Manufacturer is XYZ.

Ans: Select MAX(Price) from Product where Manufacturer = ‘XYZ’ ;


Q.No 2) Consider the following table FLIGHTS. Write SQL commands for the statements
(i) to (v)

CREATE TABLE FLIGHTS


(FL_NO char(5) PRIMARY KEY,
STARTING char(15) NOT NULL,
ENDING char(15) NOT NULL,
NO_FLIGHTS integer,
NO_STOPS integer);

INSERT INTO FLIGHTS VALUES(‘IC301’, ‘MUMBAI’, ‘DELHI’, 8, 0) ;

(i) Arrange the contents of the table FLIGHTS in the ascending order of FL_NO.

Ans: select * from FLIGHTS order by FL_NO asc ;

(ii) Display FL_NO and NO_FLIGHTS where starting airport is ‘KANPUR’.

Ans: select FL_NO, NO_FLIGHTS from FLIGHTS where STARTING = ‘KANPUR’ ;

(iii) Display the details of flights in which NO_FLIGHTS is more than 3.

Ans: select * from FLIGHTS where NO_FLIGHTS > 3 ;

(iv) Display the details of flights in which FL_NO starts with ‘IC’.

Ans: select * from FLIGHTS where FL_NO like ‘IC%’;

(v) To increase the NO_STOPS by 1 where NO_STOPS is 0.

Ans: Update FLIGHTS


Set NO_STOPS = NO_STOPS + 1
Where NO_STOPS = 0 ;
Q.No 3) Consider the following table WORKER. Write SQL commands for the
statements (i) to (v)

CREATE TABLE WORKER


(ECODE integer PRIMARY KEY,
NAME char(15) NOT NULL,
DESIG char(15),
PLEVEL char(4),
DOJ date,
DOB date);
INSERT INTO WORKER VALUES(11, ‘Radhe Shyam’, ‘Supervisor’, ‘P001’, ’13-09-2004’, ’23-
08-1981’) ;

(i) To display the details of all WORKERs in descending order of DOB.


Ans: select * from WORKER order by DOB desc ;

(ii) To display NAME and DESIG of those WORKERs, whose PLEVEL is either P001 or
P002.
Ans: select NAME, DESIG from WORKER where PLEVEL = ‘P001’ or PLEVEL=’P002’ ;

(iii) To display the content of all the WORKERs, whose DOB is in between '19-JAN-
1984' and '18-JAN-1987'.
Ans: select * from WORKER where DOB between ’19-01-1984’ and ’18-01-1987’ ;

(iv) To display the NAME, DOJ of those WORKERs whose DOJ is after ’14-Jun-2009’ .
Ans: select NAME, DOJ from WORKER where DOJ > ’14-06-2009’ ;

(v) To display the no of WORKERs whose DESIG is ‘Operator’ .


Ans: select COUNT(DESIG) from WORKER where DESIG = ‘Operator’ ;
Q.No 4) Consider the following table RESULT . Write SQL commands for the statements
(i) to (v)
Table: RESULT
Scode SubjectName NumberOfStudents AvgMarks ExamDate
083 Computer Sc 67 95 22-Mar-2015
301 English 110 85 01-Mar-2015
041 Maths 110 80 20-Mar-2015
042 Physics 100 90 05-Mar-2015
043 Chemistry 100 85 11-Mar-2015

CREATE TABLE RESULT


(Scode integer PRIMARY KEY,
SubjectName char(20) NOT NULL,
NumberOfStudents integer,
AvgMarks integer,
ExamDate date);

INSERT INTO RESULT VALUES(083, ‘Computer Sc’, 67, 95, ’22-03-2015’) ;


(i) To display the name of all Subjects in descending order of their subject codes.

Ans: select SubjectName from RESULT order by Scode desc;

(ii) To display Average of AvgMarks for each number of students groupings.


(As shown in column NumberOfStudents 67, 110, 100)

Ans: select AVG(AvgMarks) from RESULT group by NumberOfStudents ;

(iii) To display the content of the RESULT table in alphabetic order of SubjectName
whose exam date is on or after 20-Mar-2015.

Ans: select * from RESULT where ExamDate >= ’20-03-2015’ order by


SubjectName ;

(iv) Display all information about the ‘Computer Sc’ from the above table.

Ans: select * from RESULT where SubjectName = ‘Computer Sc’ ;

(v) To display Scode, SubjectName where SubjectName starts with ‘C’ ;

Ans: select Scode, SubjectName from RESULT where SubjectName like ‘C%’ ;
Q.No 5) Consider the following table FACULTY. Write SQL commands for the statements
(i) to (v)

CREATE TABLE FACULTY


(F_ID integer PRIMARY KEY,
Fname char(15) NOT NULL,
Lname char(15) NOT NULL,
Hire_date date,
Salary decimal);

INSERT INTO FACULTY VALUES(102, ‘Amit’, ‘Mishra’, ’12-10-1998’, 12000) ;

(i) To display details of those Faculties whose date of joining is before 31-12-2001.

Ans: select * from FACULTY where Hire_date < ‘ 31-12-2001’ ;

(ii) To display F_ID, Fname, Lname of those faculties whose Salary is more than 10000.

Ans: select F_ID, Fname, Lname from FACULTY where Salary > 10000 ;

(iii) To display Fname, Lname of those faculties whose Lname starts with ‘M’ ;

Ans: Select Fname, Lname from FACULTY Where Lname like ‘M%’;

(iv) To display total salary of all the faculties.

Ans: Select SUM(Salary) from FACULTY ;

(v) To display details of those Faculties in the descending order of their Lname.

Ans: select * from FACULTY order by Lname desc;

]
Q.No 6) Consider the following table GRADUATE. Write SQL commands for the statements
(i) to (v)

TABLE : GRADUATE
SNO NAME STIPEND SUBJECT AVERAGE DIV
1 KARAN 400 PHYSICS 68 I
2 DIWAKAR 450 COMP SC 68 I
3 REKHA 350 PHYSICS 56 II
4 ARJUN 500 MATHS 70 I
5 SABINA 400 CHEMISTRY 55 II
6 RUBINA 500 COMP SC 62 I

CREATE TABLE GRADUATE


(SNO integer PRIMARY KEY,
NAME char(15) NOT NULL,
STIPEND integer,
SUBJECT char(15),
AVERAGE integer,
DIV char(2));

INSERT INTO GRADUATE VALUES(1, ‘KARAN’, 400, ’PHYSICS’, 68, ‘I’) ;

i) List the names of those students who have obtained DIV I sorted by NAME.
Ans: select NAME from GRADUATE where DIV = ‘I’ order by NAME ;
ii) Display NAME, STIPEND, SUBJECT of those students whose name starts with ‘R’
Ans: select NAME, STIPEND, SUBJECT from GRADUATE where NAME like ‘R%’ ;

iii) To count the number of students who are either PHYSICS or COMPUTER SC
graduates.

Ans: select COUNT(SUBJECT) from GRADUATE where SUBJECT = ‘PHYSICS’ or SUBJECT =


‘COMP SC’ ;
iv) Display total STIPEND of those students who have obtained DIV I
Ans: Select SUM(STIPEND) from GRADUATE WHERE DIV = ‘I’;
v) Display average STIPEND of those students who have obtained AVERAGE more
than 65.
Ans: Select AVG(STIPEND) from GRADUATE where AVERAGE>=65;
Q.No 7). Consider the following table MOVIE. Write SQL commands for the statements
(i) to (v)
Table : MOVIE

CREATE TABLE MOVIE


(No integer PRIMARY KEY,
Title char(25) NOT NULL,
Type char(10),
Rating char(4),
Stars char(10),
Qty integer,
Price decimal);

INSERT INTO MOVIE VALUES(1, ‘Gone with the Wind’, ’Drama’, ‘G’, ‘Gable’, 4, 39.95) ;

(i) Display a list of all movies with Price over 20 and sorted by Price.

Ans: select * from MOVIE where Price > 20 order by Price

(ii) Display all the movies sorted by Qty in decreasing order.

Ans: select * from MOVIE order by Qty desc;

(iii) Display Title, Type for all movies where Rating is ‘R’.

Ans: select Title, Type from MOVIE where Rating = ‘R’ ;

(iv) Find the total no of the movie title available in the library.

Ans: select COUNT(TITLE) from MOVIE ;

(v) Display maximum price of the movie where price is over 30 .

Ans: Select MAX(Price) from MOVIE where price > 30;


Q.No 8). Consider the following table GAMES . Write SQL commands for the statements
(i) to (v)

CREATE TABLE GAMES


(GCode integer PRIMARY KEY,
GameName char(20) NOT NULL,
Number integer,
PrizeMoney decimal,
ScheduleDate date);

INSERT INTO GAMES VALUES(101, ‘Carom Board’,2, 5000, ’23-01-2004’) ;

(i) To display details of those games which are having PrizeMoney more than 7000.

Ans: select * from GAMES where PrizeMoney > 7000 ;

(ii) To display the content of the GAMES table in ascending order of ScheduleDate.

Ans: select * from GAMES order by ScheduleDate ;

(iii) To display sum of PrizeMoney for each of the Number of participation groupings
(as shown in column Number 2 or 4)

Ans: select SUM(PrizeMoney) from GAMES group by Number ;

(iv) Display all information about the ‘Carom Board’ from the above table.

Ans: select * from GAMES where GameName = ‘Carom Board’ ;

(v) To display Gcode, GameName where GameName ends with ‘Tennis’ ;


Ans: select Gcode, GameName from GAMES where GameName like ‘%Tennis’ ;
Q.No.9)

create table ITEM


(I_ID char(4) primary key,
ItemName char(20) not null,
Manufacturer char(3),
Price decimal);
insert into ITEM values ('PC01','Personal Computer', 'ABC', 35000);
insert into ITEM values ('LC05','Laptop', 'ABC', 55000);
insert into ITEM values ('PC03','Personal Computer', 'XYZ', 32000);
insert into ITEM values ('PC06','Personal Computer', 'COMP', 37000);
insert into ITEM values ('LC03','Laptop', 'PQR', 57000);

create table CUSTOMER


(C_ID integer primary key,
CustomerName char(20) not null,
City char(15),
I_ID char(4) Foreign key references ITEM(I_ID)
);
insert into CUSTOMER values (01,'N Roy', 'Delhi', 'LC03');
insert into CUSTOMER values (06,'H Singh', 'Mumbai', 'PC03');
insert into CUSTOMER values (12,'R Pandey', 'Delhi', 'PC06');
insert into CUSTOMER values (15,'C Sharma', 'Delhi', 'LC03');
insert into CUSTOMER values (16,'K Agarwal', 'Bangalore', 'PC01');
(i) To display the details of those Customers whose city is Delhi.

Ans: Select * from Customer Where City=”Delhi”;

(ii) To display the details of Item whose Price is in the range of 35000 to 55000 (Both values
included).

Ans: Select * from Item Where Price between 35000 and 55000;

(iii) To display the CustomerName, City from table Customer, and ItemName and Price from
table Item, with their corresponding matching I_ID.

Ans: Select CustomerName, City, ItemName, Price from Item, Customer where
Item.I_ID=Customer.I_ID;

iv) To increase the Price of all Items by 1000 in the table Item.
Ans: Update Item
set Price=Price+1000;

v) SELECT DISTINCT City FROM Customer.

Ans: City
Delhi
Mumbai
Bangalore

vi) SELECT ItemName, MAX(Price), Count(*) FROM Item GROUP BY ItemName;

Ans: ItemName Max(Price) Count(*)


Personal Computer 38000 3
Laptop 58000 2

(vii) SELECT CustomerName, Manufacturer FROM Item, Customer WHERE


Item.Item_Id=Customer.Item_Id;

Ans: CustomerName Manufacturer Name

N.Roy PQR
H.Singh XYZ
R.Pandey COMP
C.Sharma PQR
K.Agarwal ABC
(viii) SELECT ItemName, Price * 100 FROM Item WHERE Manufacturer = ‘ABC’;

Ans: ItemName Price*100


Personal Computer 3600000
Laptop 5600000
Q.No.10)

(i) Display NAME of all doctors who are in “MEDICINE” having more than 10 years experience from
the Table DOCTOR.

Ans: Select Name from Doctor where Dept=”Medicine” and Experience>10

(ii) Display the average salary of all doctors working in “ENT”department using the tables. DOCTORS
and SALARY Salary =BASIC+ALLOWANCE.

Ans: Select avg(basic+allowance) from Doctor,Salary where Dept=”ENT” and Doctor.ID=Salary.ID;

(iii) Display the minimum ALLOWANCE of female doctors.

Ans: Select min(Allowance) from Doctor,Salary where Sex=”F” and Doctor.ID=Salary.ID;

(iv) Display the highest consultation fee among all male doctors.

Ans: Select max(Consulation) from Doctor,Salary where Sex=”M” and Doctor.ID=Salary.ID;

(v) SELECT count (*) from DOCTOR where SEX = “F”


Ans: count(*)
4
(vi) SELECT NAME, DEPT , BASIC from DOCTOR, SALRY Where DEPT = “ENT” AND DOCTOR.ID =
SALARY.ID
Ans: Name Dept Basic
John ENT 12000
Q.No.11)

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


Ans: Select * from Sender where SenderCity =’Mumbai’;
(ii) To display the RecID, SenderName, SenderAddress, RecName, RecAddress for every recipient.
Ans: Select RecID, SenderName, SenderAddress, RecName, RecAddress from Sender, Recipient
where Sender.SenderID=Recipient.SenderID;
(iii) To display the sender details in ascending order of SenderName.
Ans: Select * from Sender order by SenderName;
(iv) To display number of Recipients from each city.
Ans: Select RecCity,Count(*) from Recipient group by RecCity;
(v) SELECT DISTINCT SenderCity FROM Sender;
Ans: SenderCity
New Delhi
Mumbai
(vi) SELECT A.SenderName A, B.RecName FROM Sender A, Recipient B WHERE A.SenderID=B.
SenderID AND B.RecCity=’Mumbai’;

Ans: SenderName RecName


R.Jain H.Singh
S.Jha P.K.Swamy
(vii) SELECT RecName,RecAddress FROMRecipient WHERE RecCity Not IN (‘Mumbai’,Kolkata’);

Ans: RecName RecAddress


S Mohan 116, A Vihar
S Tirupathi 13, B1 D, Mayur Vihar
(viii) SELECT RecID, RecName FROM Recipient WHERE SenderID = ‘MU02’ OR SenderID = ‘ND50’;

Ans: RecID RecName


ND08 S Mohan
ND48 S Tirupathi
Q.No.12)Consider the following table FLIGHT and FARES. Write the SQL
commands for the statements (i) to (iv) and output from (v) to (viii).

Table: FLIGHT

FL_NO STARTING ENDING NO_FLIGHTS NO_ STOPS

IC301 MUMBAI DELHI 8 0

IC799 BANGALORE DELHI 2 1

MC101 INDORE MUMBAI 3 0

IC302 DELHI MUMBAI 8 0

AM812 KANPUR BANGALORE 3 1

IC899 MUMBAI KOCHI 1 4

AM501 DELHI TRIVANDRUM 1 5

MU499 MUMBAI MADRAS 3 3

IC701 DELHI AHMEDABAD 4 0

Table: FARES
FL_NO AIRLINES FARE TAX%

IC701 Indian Airlines 6500 10

MU499 Sahara 9400 5

AM501 Jet Airways 13450 8

IC899 Indian Airlines 8300 4

IC302 Indian Airlines 4300 9

IC799 Indian Airlines 10500 10

MC101 Deccan Airlines 3500 4

(i) Display FL_NO and NO_FLIGHTS from “KANPUR” TO “BANGALORE” from the table FLIGHTS.

Ans: Select FL_NO, NO_FLIGHTS from FLIGHTS where Starting=”KANPUR” AND


ENDING=”BANGALORE”;

(ii) Arrange the contents of the table FLIGHTS in the ascending order of FL_NO.

Ans: Select * from FLIGHTS ORDER BY FL_NO;


(iii) Display the FL_NO and fare to be paid for the flights from DELHI to MUMBAI using the tables
FLIGHTS and FARES, where the fare to paid = FARE+FARE+TAX%/100.

Ans: Select FL_NO, FARE+FARE+(TAX%/100) from FLIGHTS, FARES where Starting=”DELHI” AND
Ending=”MUMBAI”;

iv) Display the minimum fare “Indian Airlines” is offering from the tables FARES.

Ans: Select min(FARE) from FARES Where AIRLINES=”Indian Airlines”;

Give the Output:

v) Select FL_NO,NO_FLIGHTS,AIRLINES from FLIGHTS, FARES Where STARTING = “DELHI” AND


FLIGHTS.FL_NO = FARES.FL_NO

Ans: FL_NO NO_FLIGHTS AIRLINES


IC799 2 Indian Airlines

(vi) SELECT count (distinct ENDING) from FLIGHTS.

Ans: count (distinct ENDING)

(vii) SELECT AVG(FARE) FROM FARE WHERE AIRLINES = ‘Indian Airlines’;

Ans: AVG(FARE)

7400

(viii) SELECT FL_NO, NO_FLIGHTS FORM FLIGHT WHERE STARTING=’MUMBAI’;

Ans: FL_NO NO_FLIGHTS


IC301 8
IC899 1
MU499 3
Q.No.13)
Consider the following tables CUSTOMER and MOBILE. Write SQL commands for the
statements (i) to (iv) and give outputs for SQL queries (v) to (viii)

Create Table & Insert Rows

CREATE TABLE CUSTOMER


(ID INTEGER PRIMARY KEY,
Cname CHAR(15) NOT NULL,
Activation_date DATE,
Validity INTEGER,
Amount INTEGER,
Connection CHAR(15)
);
CREATE TABLE MOBILE
(ID INTEGER FOREIGN KEY REFERENCES CUSTOMER(ID),
Make CHAR(15),
Model_No CHAR(10)
);
INSERT INTO CUSTOMER VALUES(101, ‘Preeti’, ’04-06-09’, 365, 330, ‘Hutch’);
INSERT INTO MOBILE VALUES(101, ‘Nokia’, ’N76’);
(i) To display the records of those customer who take the connection of Bsnl and Airtel in
ascending order of Activation date.

Ans:
SELECT * FROM CUSTOMER WHERE Connection = ‘Bsnl’ OR Connection = ‘Airtel’ ORDER BY
Activation_date;

(ii) To decrease the amount of all customers of Reliance connection by 500.

Ans:
UPDATE CUSTOMER
SET Amount = Amount – 500
WHERE Connection=’ Reliance’;

(iii) Count the no. of companies giving connection from CUSTOMER table whose name starts
with ‘P’.

Ans:
SELECT COUNT(*) FROM CUSTOMER WHERE Cname LIKE ‘P%’;

(iv) To display the ID and Cname from table Customer and Make from table Mobile,
with their corresponding matching ID.

SELECT ID, Cname, Make FROM Customer, Mobile WHERE CUSTOMER.ID=MOBILE.ID;

You might also like