3 CS Structured Query Language
3 CS Structured Query Language
3 CS Structured Query Language
com
Computer Science
om
table and information. It is a DDL command.
.c
Question 2:
What is the use of wildcard ? ay
Аnswer:
od
The wildcard operators are used with the LIKE operator to search a value similar to a specific
pattern in a column. There are 2 wildcard operators.
st
Question 3:
tu
Write SQL query to add a column total price with datatype numeric and size 10, 2 in a table
.s
product.
Аnswer:
w
Question 4:
While creating table ‘customer’, Rahul a forgot to add column ‘price’. Which command is
used to add new column in the table. Write the command to implement the same.
Аnswer:
ALTER TABLE customer ADD price number(10,2)
Question 5:
Deepika wants to remove all rows from the table BANK. But he needs to maintain the
structure of the table. Which command is used to implement the same ?
Аnswer:
DELETE FROM BANK
Question 6:
Sonal needs to display name of teachers, who have “0” as the third character in their name.
Question 7:
Consider the following tables School and Admin and answer this question :
Give the output the following SQL queries :
1. Select Designation Count (*) From Admin Group By Designation Having Count (*) <2;
2. SELECT max (EXPERIENCE) FROM SCHOOL;
om
3. SELECT TEACHERNAME FROM SCHOOL WHERE EXPERIENCE >12 ORDER BY TEACHER
NAME;
.c
4. SELECT COUNT (*), GENDER FROM ADMIN GROUP BY GENDER;
Table : SCHOOL
ay
od
Аnswer:
(i)
VICE PRINCIPAL 01
(ii)
16
om
(iii)
.c
UMESH ay
YASH RAJ
od
st
Question 1:
tu
.s
Write SQL queries for (i) to (iv) and find outputs for SQL queries (v) to (viii), which are based
w
on the tables.
w
w
Table: VEHICLE
105 SUV 40
104 CAR 20
Note :
Table: TRAVEL
om
106 Kripal Anya 2016-02-06 200 101 25
.c
Note:
ay
NO is Traveller Number
od
KM is Kilometer Travelled
NOP is number of travellers travelled in vehicle.
st
1. To display NO, NAME, TDATE from the table TRAVEL in descending order of NO.
2. To display the NAME of all the travelers from the table TRAVEL who are travelling by
tu
3. To display the NO and NAME of those travelers from the table TRAVEL who travelled
between ‘2015-1231’ and ‘2015-04-01’.
w
4. To display all the details from table TRAVEL for the travelers, who have travelled
w
5. SELECT COUNT (*), CODE FROM TRAVEL GROUP BY CODE HAVING COUNT(*)>1;
6. SELECT DISTINCT CODE FROM TRAVEL;
7. SELECT A. CODE,NAME, VTYPE
Аnswer:
3. Select NO, NAME from TRAVEL where TDATE between ’2015-12-31′ and ‘2015-04-01’.
4. Select * from TRAVEL where KM > 100 order by NOP.
5.
2 101
2 102
6.
DISTANCE (CODE)
101
om
103
.c
102
104
ay
od
105
7.
st
8.
w
NAME KM*PERKM
w
Question 2:
Write SQL queries for (i) to (iv) and find outputs for SQL queries (v) to (viii), which are based
on the tables.
Table :VEHICLE
V0’5 SUV 30
V04 CAR 18
Note:
PERKM is Freight Charges per kilometer.
Table : TRAVEL
om
102 Ravi Anish 2016-01-13 80 V02 40
.c
104 Sahanubhuti 2016-01-28
ay 90 V0 5 4
Note:
e st
Km is Kilometers Travelled
di
1.To display CNO, CNAME, TRAVELDATE from the table TRAVEL in descending order of CNO.
.s
2.To display the CNAME of all the customers from the table TRAVEL who are travelling by
vehicle with code V01 or V02.
w
3.To display the CNO and CNAME of those customers from the table TRAVEL who travelled
w
4.To display all the details from table TRAVEL for the customers, who have travel distance
more than 120 KM in ascending order of NOP.
5.SELECT COUNT (*) , VCODE FROM TRAVEL
GROUP BY VCODE HAVING COUNT(*)>1;
6. SELECT DISTINCT VCODE FROM TRAVEL;
7. SELECT A. VCODE, CNAME, VEHICLETYPE
FROM TRAVEL A,VEHICLE B
WHERE A.VCODE=B.VCODE AND KM<90;
8. SELECT CNAME, KM*PERKM FROM TRAVEL A,VEHICLE B
WHERE A.VCODE=B . VCODE AND A.VCODE= ‘V05 ‘ ;
Аnswer:
(i) Select CNO, CNAME, TRAVELDATE from TRAVEL order by CNO desc
(ii) Select CNAME from TRAVEL, where VCODE in (‘VOl’, ‘ V02 ‘)
(iii)Select CNO, CNAME from TRAVEL where TRAVELDATE between ‘2015-12-31’ and ‘2015-
05-01 ‘
2 V01
2 V02
(vi)
DISTANCE (CODE)
V01
om
V03
.c
V02
V04
ay
od
V05
(vii)
e st
di
(viii)
CNAME KM*PERKM
w
Sahanubhuti 30
w
Note: PERKM is neither given in query nor in TABLE so no output is also acceptable.
w
Question 1:
Consider the following tables FACULTY and COURSES. Write SQL commands for the
statements (i) to (v) and give outputs for SQL queries (vi) to (vii)
FACULTY
om
C23 104 Computer Security 8000
.c
C25 102 Computer Network
ay 20000
(i) To display details of those Faculties whose salary is greater than 12000.
Аnswer:
st
(ii) To display the details of courses whose fees is in th range of 15000 to 50000(both values
tu
included).
Аnswer:
.s
<strongАnswer:
w
Question 2:
Consider the following DEPT and WORKER tables. Write SQL queries for (i) to (iv) and find
outputs for SQL queries (v) to (viii):
TABLE : DEPT
om
TABLE : WORKER
.c
WNO NAME . Y; DOJ DOB
ay GENDER DCODE
Note : DOJ refers to date of joining and DOB refers to date of birth of workers.
w
(i)To display Wno. Name, Gender from the table WORKER in descending order of Wno. Ans.
Аnswer:
SELECT WNO, Name, Gender FROM Worker
ORDER BY Wno DESC;
(ii)To display the Name of all the FEMALE workers from the table WORKER.
Аnswer:
SELECT Name FROM Worker
WHERE gender = ‘FEMALE’;
(iii)To display the Wno and Name of those workers from the table WORKER who are born
between
‘1987-01-01’ and ‘1991-12-01’.
Аnswer:
SELECT Wno, Name FROM Worker
WHERE DOB BETWEEN ‘1987-01-01’ AND ‘1991-12-01’;
OR
OR
om
OR
.c
SELECT * FROM Worker ay
WHERE GENDER = ‘MALE’ AND DOJ > ‘198601-01’;
(Any valid query for counting and/or displaying for male workers will be awarded 1 mark)
od
(v) SELECT COUNT (*), DCODE FROM WORKER GROUP BY DCODE HAVING COUNT (*) > 1;
Аnswer:
st
2 D01
tu
.s
2 D05
(vi)SELECT DISTINCT DEPARTMENT FROM DEPT;
w
Аnswer:
w
Department
w
MEDIA
MARKETING
INFRASTRUCTURE
FINANCE
HUMAN RESOURCE
(viii)SELECT NAME, DEPARTMENT, CITY FROM WORKER W, DEPT D WHERE W DCODE = D.
DCODE AND WNO < 1003;
Аnswer:
Question 3:
Consider the following DEPT and EMPLOYEE tables. Write SQL queries for (i) to (iv) and find
outputs for SQL queries (v) to (viii).
TABLE : DEPT
om
D02 MARKETING DELHI
.c
DOS FINANCE ay KOLKATA
TABLE : EMPLOYE
e st
OR
OR
SELECT Eno, Name FROM Employee WHERE DOB > ‘1987-01-01’ AND DOB < ‘199112-01’;
WHERE DOB BETWEEN ‘1987-01-01’ AND ‘1991-12-01’;
om
OR
.c
WHERE DOB > = ‘1987-01-01’ AND DOB < = ‘1991-12-01’;
ay
OR
od
(iv)To count and display FEMALE employees who have joined after ‘1986-01-01’;
e
OR
.s
w
(Any valid query for counting and/or displaying for female employees will be awarded 1
mark)
(v)SELECT COUNT (*), DCODE FROM EMPLOYEE
GROUP BY DCODE HAVING COUNT (*) > 1;
Аnswer:
COUNT DCODE
2 D01
2 D05
Аnswer: Department
INFRASTRUCTURE
MARKETING
MEDIA
FINANCE
HUMAN RESOURCE
(vii) SELECT NAME, DEPARTMENT FROM EMPLOYEE E, DEPT D WHERE E. DCODE = D.DCODE
AND ENO <1003;
NAME DEPARTMENT
George K MEDIA
om
Ryma Sen infrastructure
.c
(viii) SELECT MAX (DOJ), MIN (DOB) FROM EMPLOYEE;
Аnswer: ay
MAX (DOJ) MIN (DOB)
2014-06-09 1984-10-19
od
Write SQL commands for the queries (i) to (iv) and output for (v) & (viii) based on a table
e
1. To display those company name which are having prize less than 30000.
2. To display the name of the companies in reverse alphabetical order.
3. To increase the prize by 1000 for those customer whose name starts with S?
4. To add one more column total price with decimal(10,2) to the table customer
5. SELECT COUNTO ,CITY FROM COMPANY GROUP BY CITY;
om
6. SELECT MIN(PRICE), MAX(PRICE) FROM CUSTOMER WHERE QTY>10;
7. SELECT AVG(QTY) FROM CUSTOMER WHERE NAME LIKE “%r%;
8. SELECT PRODUCTNAME,CITY, PRICE FROM COMPANY, CUSTOMER WHERE
.c
COMPANY.CID=CUSTOMER.CID AND PRODU CTN AME=”MOBILE”;
ay
Аnswer:
od
1. To display those company name which are having prize less than 30000.
SELECT NAME FROM COMPANY WHERE COMPANY.CID=CUSTOMER. CID AND PRICE <
st
30000
e
3.To increase the prize by 1000 for those customer whose name starts with “S”
.s
UPDATE CUSTOMER
SET PRICE = PRICE + 1000;
w
4.To add one more column total price with decimal(10,2) to the table customer
w
3 DELHI
2 MUMBAI
1 MADRAS
6.SELECT MIN(PRICE), MAX(PRICE) FROM
CUSTOMER WHERE QTY> 10;
50000,70000
7.SELECT AVG(QTY) FROM CUSTOMER
WHERE NAME LIKE “%r%; [
om
1009 Priya Rai Physics 03/09/1998 26 , 12
.c
1045 Yashraj Maths
ay
24/08/2000 24 15
Table : Admin
tu
1. To display TEACHERNAME, PERIODS of all teachers whose periods are more than 25.
2. To display all the information from the table SCHOOL in descending order of
experience.
3. To display DESIGNATION without duplicate entries from the table ADMIN.
Аnswer:
1. To display TEACHERNAME, PERIODS of all teachers whose periods are more than 25.
SELECT TEACHERNAME, PERIODS
FROM SCHOOL WHERE PERIODS >25.
2.To display all the information from the table SCHOOL in descending order of experience.
3.To display DESIGNATION without duplicate entries from the table ADMIN.
SELECT DISTINCT DESIGNATION FROM ADMIN;
4.To display TEACHERNAME, CODE and corresponding DESIGNATION from tables SCHOOL
om
and ADMIN of Male teachers.
SELECT TEACHERNAME.CODE
DESIGNATION FROM SCHOOL.CODE = ADMIN.CODE
.c
WHERE GENDER = MALE; ay
Question 6:
od
Answer the questions (a) and (b) on the basis of the following tables SHOPPE and
ACCESSORIES.
st
Аnswer:
e
di
Id SName Area
tu
No Name Price Id
om
1. To display Name and Price of all the Accessories in ascending order of their Price.
2. To display Id and SName of all Shoppe located in Nehru Place.
3. To display Minimum and Maximum Price of each Name of Accessories.
.c
4. To display Name, Price of all Accessories and their respective SName where they are
ay
available.
od
(b)Write the output of the following SQL commands:
st
Аnswer: (a) (i) SELECT Name, Price FROM ACCESSORIES ORDER BY Prices;
w
SHOPPE.ID=ACCESSORIES.ID
(b)(i) Name
Mother Board
Hard Disk
LCD
(ii)
Area Count
CP 2
GK II 1
Nehru Place 2
Name DISCOUNT
600 600
Key Board 20
om
COURSB.
.c
1. To display name, fee, gender, join year about the applicants, who have joined before
2010.
ay
2. To display names of applicants, who are paying fee more than 30000.
od
3. To display names of all applicants in ascending order of their join year.
4. To display the year and the total number of applicants joined in each YEAR from the
st
table APPLICANTS.
e
di
Join
No Name Fee Gender C_ID
Year
tu
Table Courses
C_ID Course
A02 Networking
om
(i)SELECT Name, Join year FROM APPLICANTS
WHERE GENDER=’F’ and C_ID=’A02′;
.c
(ii) SELECT MIN (Join year) FROM APPLICANTS
ay
(iii)SELECT AVG (Fee) FROM APPLICANTS WHERE C_ID=’A01′ OR C_ID=’A05′;
(iv)SELECT SUM (Fee), C_ID FROM APPLICANTS
od
GROUP BY C_ID HAVING COUNT(*)=2;
Аnswer: (a) SELECT NAME,FEE,GENDERJOINYEAR FROM APPLICANTS
st
WHERE JOINYEAR<2000;
(b)SELECT NAME FROM APPLICANTS
e
WHERE FEE>30000;
di
Question 8:
Write SQL queries for (a) to (g) and write the output for the SQL queries mentioned shown
in (hi) to (h4) parts on the basis of table ITEMS and TRADERS :
Table : ITEMS
om
T01 MUMBAI
ELECTRONIC SALES BUSY STORE CORP DISP HOUSE
T03 DELHI
INC
T02 CHENNAI
.c
ay
To display the details of all the items in ascending order of item names (i.e., INAME).
To display item name and price of all those items, whose price is in the range of 10000
od
To display the price, item name and quantity (i.e., qty) of those items which have
di
To display the names of those traders, who are either from DELHI or from MUMBAI.
To display the names of the companies and the names of the items in descending
.s
Obtain the outputs, of the following SQL queries based on the data given in tables
ITEMS and TRADERS above.
w
w
Question 9:
om
Write SQL queries for (a) to (f) and write the outputs for the SQL queries mentioned shown
in (hi) to (h4) parts on the basis of tables PRODUCTS and SUPPLIERS
Table : PRODUCTS
.c
ay
PID PNAME QTY PRICE COMPANY SUPCODE
od
Table : SUPPLIERS
w
w
CORP
2
1
(d)To display the price, product name and quantity (i.e., qty) of those products which have
quantity more than 100.
(e)To display the names of those suppliers, who are either from DELHI or from CHENNAI.
(f)To display the name of the companies and the name of the products in descending order
of company names.
(g)Obtain the outputs of the following SQL queries based on the data given in tables
PRODUCTS and SUPPLIERS above.
(gl) SELECT DISTINCT SUPCODE FROM PRODUCTS;
(g2) SELEC MAX (PRICE), MIN (PRICE) FROM PRODUCTS
(g3) SELECT PRICE*QTY
FROM PRODUCTS WHERE PID = 104;
(g4) SELECT PNAME, SNAME
FROM PRODUCTS P, SUPPLIERS S WHERE P SUPCODE = S. SUPCODE AND QTY>100;
om
Аnswer:
(a) SELECT * FROM PRODUCTS ORDER. BY PNAME ASC;
.c
(b)SELECT PNAME, PRICE FROM PRODUCTS WHERE ((PRICE => 10000) AND (PRICE = <
15000)); ay
(c)SELECT SUPCODE, COUNT (PID) [Yz] FROM PRODUCTS GROUP BY SUPCODE;
od
(d)SELECT PRICE, PNAME, QTY FROM PRODUCTS WHERE (QTY > 100);
(e)SELECT SNAME FROM SUPPLIERS WHERE ((CITY = “DELHI”) OR (CITY = “CHENNAI”));
st
(g1) S02
di
S03
tu
(g2) 28000
1100
.s
(g3) 550000
w
Question 10:
Give a suitable example of a table with sample data and illustrate Primary and Alternate
Keys in it. Consider the following tables CARDEN and CUSTOMER and answer (b) and (c)
parts of this question :
Table : CARDEN
om
(b)Write SQL commands for the following statements:
.c
2. Tp display names of car, make and capacity of cars in descending order of their sitting
ay
capacity.
3. To display the highest charges at which a vehicle can be hired from CARDEN.
od
4. To display the customer name and the corresponding name of the cars hired by them.
st
Аnswer:
(a) Primary Key of CARDEN = Ceode of CARDEN
w
(a) Give a suitable example of a table with sample data and illustrate Primary agd Candidate
Keys in it. Consider the following tables CABHUB and CUSTOMER and answer (b) and (c)
parts of this question :
Table : CABHUB
om
Table : CUSTOMER
.c
Ceode Cname
ay Vcode
2. To display name of vehicle name and capacity of vehicles in ascending order of their
w
sitting capacity.
w
3. To display the highest charges at which a vehicle can be hired from CABHUB.
4. To display the customer name and the corresponding name of the vehicle hired by
them.
Аnswer:
(a) Primary Key of CABHUB = Vcode
Alternate key of CABHUB = Vehicle Name.
om
Long Answer Type Question – II
.c
Question 1:
ay
Watchid Watch_Name Price Type Qty_Store
od
wool 10 1
W003 5 1
W002 20 2
W003 10 2
W001 15 3
W002 20 3
W005 10 3
W003 15 4
1. To display all the details of those watches whose name ends with ‘Time’
2. To display watch’s name and price of those watches which have price range in
between 5000-15000.
3. To display total quantity in store of Unisex type watches.
4. To display watch name and their quantity sold in first quarter.
5. select max(price), min(qty_store) from watches;
6. select quarter, sum(qty_sold) from sale group by quarter;
7. select watch_name,price, type from watches w, sale s where w.watchid!=s.watchid;
8. select watch_name, qty_store, sum(qty_sold), qty_store-sum(qty_sold) “Stock” from
watches w, sales where w. watchid=s. watchid group by s.watchid;
Аnswer:
om
(i) Select*from watches where watch_name like’Time’
(ii)select watchjname, price from watches where price between 5000 and 15000;
(iii)select sum(qty_store) from watches where type like ‘Unisex’;
.c
(iv)select watch name, qty_sold from watches w,sale s where w.watchid=s.watchid and
ay
quarter=l;
(v)
od
st
max(price) min(qty_store)
e
25000 100
di
(vi)
tu
.s
quarter suxn(qty_sold)
w
1 15
w
2 30
w
3 45
4 15
(vii)
HighTime 100 25 75
GoldenTime 100 10 90
om
.c
ay
od
ste
di
tu
.s
w
w
w