0% found this document useful (0 votes)
48 views11 pages

Prev Yr SQL Qs

Uploaded by

Sneha Malik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views11 pages

Prev Yr SQL Qs

Uploaded by

Sneha Malik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

1.(a)What do you understand by Primary key in a table?

Give a suitable example of Primary Key


from a table containing some meaningful data. 2
(b) Consider the following tables STOCK and DEALER and answer (b1) and (b2) parts of the question:
Table: STOCK
ITEMNO ITEM DCODE QTY RATE LASTBUY
2005 Sharpner Classic 23 60 8 31-Jan-09
2003 Ball Pen 0.25 22 50 25 01-Feb-10
2002 Gel Pen Premium 21 150 12 24-Feb-10
2006 Gel Pen Classic 21 250 20 11-Mar-09
2001 Eraser Small 22 220 6 19-Jan-09
2004 Eraser BIg 22 110 8 02-Dec-09
2009 Ball Pen 0.5 21 180 18 03-Nov-09

Table : DEALER
DCODE DNAME
21 Premium Stationers
23 Soft Plastics
22 Tetra Supply

(b1) Write SQL commands for the following statements: 4 x1=4


(i) To display details of all the items in the STOCK table in ascending order of LastBuy.
(ii) To display ItemNo and Item name of those items from STOCK table whose Rate is more than 15
Rupees.
(iii) To display the details of those items whose Dcode is 22 or Qty is more than 110 from the table
STOCK.
(iv) To display minimum Rate of items for each Dealer individually as per Dcode from the table
STOCK.
(b2) Give the output of the following SQL queries: 4x ½ =2
(i) SELECT COUNT(DISTINCT DCODE) FROM STOCK;
(ii) SELECT RATE*QTY FROM STOCK WHERE ITEMNO = 2004;
(iii) SELECT ITEM, DNAME FROM STOCK S, DEALER P WHERE S.DCODE = P.DCODE AND
ITEMNO = 26;
(iv) SELECT MAX(LASTBUY) FROM STOCK;

Page 1 of 11
2. ( a) What do you understand by the terms Cardinality and Degree of a relation in relational database.
(b ) Consider the following tables and answer the questions that follow :
TABLE : GRADUATE
S. No. NAME STIPEND SUBJECT AVERAGE DIV
1 Auro 500 Computer Sc. 78 1
2. Bharti 200 Economics 57 2
3 Dhriti 450 Maths 94 1
4 Kuhikaa 350 Chemistry 95 1
5 Gitagya 400 Physics 60 1
6 Tripti 250 Computer Sc 50 2
TABLE : GUIDE
Subject ADVISOR
Physics Sandip
Computer Science Rizwan
Chemistry Geetika
Maths Rohini

Write SQL Commands for the following statements : 4


(i) List the names of the students along with their advisors who have obtained DIV 1 ( first division)
(ii) Display a report , listing NAME , STIPEND , SUBJECT and amount of stipend received in a year
assuming that the STIPEND is paid every month , sorted by Stipend in descending order..
(iii) To count the number of students who are either Physics or Chemistry graduates.
(iv) To increase the stipend by Rs 100 for students who are graduate in Computer Science

Give the output for the following SQL queries : 2


(v ) SELECT MIN(AVERAGE) from GRADUATE where SUBJECT = ‘PHYSICS’
(vi) SELECT COUNT(DISTINCT SUBJECT) from GRADUATE
(vii) SELECT ADVISOR from GUIDE WHERE NAME LIKE “ R%”.
(viii )SELECT * FROM GRADUATE Where SUBJECT=’Maths’ AND AVERAGE>90
Ans:
Cardinality : Number of rows in a relation
Degree : number of attributes in a relation
(b )
(I )Select NAME , ADVISOR From Graduate , GuideWhere DIV = 1
(ii)Select NAME , STIPEND, SUBJECT , STIPEND * 12 From Graduate Order by STIPEND DESC

(iii)Select count(*)From GraduateWhere SUBJECT =’Physics’ OR SUBJECT=’Chemistry’

(iv)Update GraduateSet STIPEND= STIPEND + 100Where SUBJECT = ‘Computer Sc’

(v)60

(vi) ComputerSc
Maths
Economics
Chemistry
Physics
(vii) Rizwan
(viii)

3 Dhriti 450 Maths 94 1


Page 2 of 11
3. (a) How Alternate key is different from candidate key. Give suitable example of each through a table with
sample data. 2
Ans: The table may have multiple candidate keys. Out of that user has to decide one to the primary key and
others are automatically called alternate kay.
If STUDENT table contains the fields admno class sec roll fname address percentage
then candidate key may be (i) admno
(ii) class + sec+ roll
(iii) fname + address
Suppose user selects admno as primary key then other groups are called alternate keys.

(b) Consider the following tables EMPLOYEE and INCHARGE and answer (b1) and
(b2) parts of the question:
Table: EMPLOYEE
EMPNAME BASIC DEPARTMENT DATEOFAPP AGE SEX
KARAN 8000 PERSONNEL 27/03/97 35 M
DIVAKAR 9500 COMPUTER 20/01/98 34 M
DIVYA 7300 ACCOUNTS 19/02/97 34 F
ARUN 8350 PERSONNEL 01/01/95 33 M
SABINA 9500 ACCOUNTS 12/01/96 36 F
JOHN 7400 ACCOUNTS 24/02/97 36 M
ROBERT 8250 PERSONNEL 20/02/97 39 M
RUBINA 9450 MAINTENANCE 22/02/98 37 F
VIKAS 7500 COMPUTER 13/01/94 41 M
MOHAN 9300 MAINTENANCE 19/02/98 37 M

Table :INCHARGE
DEPT HEAD
PERSONNEL RAHUL
COMPUTER SATYAM
ACCOUNTS NATH
FINANCE GANESH
MAINTENANCE JACOB

(b1) Write SQL commands for the following statements: 4 x1=4


1. To display name of all employees who are more than 34 years of age in ascending order of their
name.
Ans: SELECT EMPNAME FROM EMPLOYEE WHERE AGE>34 ORDER BY EMPNAME;
2 . To display name, department and annual basic salary ( assume monthly basic given in the table).
Ans: SELECT EMPNAME, DEPARTMENT, BASIC *12 FROM EMPLOYEE;
Page 3 of 11
3. To display number of employees who are either in PERSONNEL or COMPUTER department.
Ans: SELECT COUNT(*) FROM EMPLOYEE WHERE DEPARTMENT=’PERSONNEL’ OR
DEPARTMENT=’COMPUTER’;
4. To display name, department, sex and head for all employees.
Ans: SELECT EMPNAME,DEPARTMENT, SEX, HEAD FROM EMPLOYEE, INCHARGE
WHERE EMPLOYEE.DEPARTMENT=INCHARGE.DEPT;

(b2) Give the output of the following SQL queries: 4x 1 =4


(i) SELECT COUNT(DISTINCT DEPARTMENT) FROM EMPLOYEE;
Ans: 4
(ii) SELECT EMPNAME, DEPARTMENT FROM EMPLOYEE WHERE EMPNAME like ‘%A’;
Ans: DIVYA ACCOUNTS
SABINA ACCOUNTS
RUBINA MAINTENANCE
(iii) SELECT MAX(BASIC), MIN(BASIC) FROM EMPLOYEE where DATEOFAPP>’22/02/97’;
Ans: 9500 7400
(iv) SELECT EMPNAME, HEAD FROM EMPLOYEE, INCHARGE WHERE BASIC=9500
AND DEPARTMENT=DEPT;
Ans: DIVAKAR SATYAM
SABINA NATH
_________________________________________________________________________________________________

4. .( a) What is the difference between primary key and candidate key of a relation in relational database?
Explain with suitable example. 2
Ans:

Primary key: It is the field or combination of fields through which we can identify a record uniquely. There will
be only one primary key possible in a relation.

Candidate key: The groups of various key or combination of keys which can form primary key is called
candidate keys. There are more than one candidate keys possible in a relation.

admno name class sec roll

13107 Arundeep 5 A 7

16112 Abhinash 1 C 1

11110 Pradipta 12 E 25

10109 Ankita 11 D 2

Admno may be the primary key. (i) admno and (ii) class+sec+roll may be the candidate key.

Page 4 of 11
(b) What is the difference between Cartesian product and natural join of two relations? 2

Ans:
Cartesian product is called unrestricted join. It displays all possible combination of records of both tables. If we
consider following tables( PRODUCT and CLIENT) and to display all combination of records the it will display
25 records.
SELECT * FROM PRODUCT, CLIENT;
Natural Join will join the tables vertically to display all fields for the corresponding records with mathing field
value in both tables. It also avoids to display the common field more than once.
SELECT CLIENT.*, PRODUCTNAME,MANUFACTURER,PRICE FROM PRODUCT, CLIENT WHERE
PRODUCT.P_ID=CLIENT.P_ID;
Consider the following tables PRODUCT and CLIENT and answer( c) and (d) parts of this question.
Table: PRODUCT
P_id Productname Manufacture Price

TP01 Talcom Powder 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 ClientName 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 Bangalore TP01

( c) Write SQL commands for the following statements: 4

(i) To display the details of those clients whose city is delhi


Ans: SELECT * FROM CLIENT WHERE CITY=”DELHI”;

(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;

Page 5 of 11
(iii) To display the clientname, city from table CLIENT and productname, price from table PRODUCT
with their corresponding matching p_id
Ans: SELECT CLIENTNAME, CITY, PRODUCTNAME,PRICE FROM CLIENT, PRODUCT WHERE
CLIENT.P_ID=PRODUCT.P_ID;

(iv) To increase the price of products by 10 for ‘ABC’ manufacturer.


Ans:

UPDATE PRODUCTSET PRICE=PRICE + 10 WHERE MANUFACTURER=’ABC’;

( d) Give the output of the following SQL queries. 2

(i) Select distinct city from client;


Ans: Distinct(city)

DELHI

MUMBAI

BANGALORE

(ii) Select manufacture, max(price), min(price), count(*) from product group by manufacturer;
Ans:

Manufacturemax(price)min(price)count(*)

LAK 40 40 1

ABC 55 45 2

XYZ 120 95 2

(iii) Select clientname, manufacture from product, client where client.p_id=product.p_id and
client.c_id>=15;
Ans: clientnamemanufacture

FW12 XYZ

TP01 LAK

(iv) Select productname, price*4 from product where price>90;


Ans: Productnameprice*4

Shampoo 480

Face Wash 380

Page 6 of 11
5. (a) Observe the following Table and answer the parts (i) and (ii) accordingly 2

Table: MEMBER

Mno Name Qty PurchaseDate


101 Pen 102 12-12-2011
102 Pencil 201 21-02-2012
102 Eraser 90 09-08-2010
109 Sharpener 90 31-08-2012
113 Clips 900 08-08-2011
(i) In the above table, can we take Mno as Primary Key? (Answer as [YES/NO] only).

Justify your answer with a valid reason.

Ans. Yes, Mno is unique for each item.

(ii) What are the degree and the cardinality of the above table?

Ans. Degree = 4 cardinality = 5

Consider the following tables SUBJECT and TEACHER and answer (b), (c), (d) and (e) parts of this
question: Table: SUBJECT

Code Title Marks_Theor Marks_Prac


y
301 English 100 0
041 Maths 100 0
083 Computer 70 30
042 Physics 70 30
043 Chemistry 70 30
Table: TEACHER

TCode Name Sub_Code


1 P.Jain 301
2 RNagpal 301
3 Supatra 041
4 Shabnam 083
5 Rashika 042
6 Vidushi 041
7 Yash 043
(b) Write SQL commands for the flowing statements: 4

(i) To display the names of all the subjects for which practical marks are 0.

(ii) To display the total number of teachers in each subject separately.

(iii) To display the names of all the teachers in the ascending order of the Sub_Code.

(iv) To display each subject’s details along with Total_Marks in each subject from the table SUBJECT.
(Total_Marks = Marks_Theory + Marks_Practical).

Ans. (i) SELECT TITLE FROM SUBJECT WHERE MARKS_PRAC = 0;

(ii) SELECT SUB_CODE, COUNT(*) FROM TEACHER GROUP BY SUB_CODE;

(iii) SELECT NAME FROM TEACHER ORDER BY SUB_CODE;

(iv) SELECT CODE,TITLE, (Marks_Theory + Marks_Practical) FROM SUBJECT ;


Page 7 of 11
(c) Write SQL statement to display each teacher’s name along with his/her respective subject name from
the tables TEACHER and SUBJECT. 2

Ans. SELECT NAME, TITLEFROM TEACHER, SUBJECT

WHERE TEACHER.SUB_CODE = SUBJECT.CODE;

(d) Give the output of the following SQL queries: 1


(i) SELECT DISTINCT(Marks_Theory) from SUBJECT;
(ii) SELECT TCode, Name from Teacher where Sub_Code like ‘0%’;
Ans. (i) 2

(ii)

Supatra 041
Shabnam 083
Rashika 042
Vidushi 041
Yash 043
(e) Identify primary keys of the tables SUBJECT and TEACHER. 1

Ans. Code and TCode

_________________________________________________________________________________________

6. .(a) Explain the concept of Cartesian product of two tables, with the help of appropriate example.2
Ans:
It is called unrestricted join. It gives all possible combinations of records of both tables.
If table 1 contains 3 records and table 2 contains 2 records then Cartesian product will be
6 records.

Table 1 Table 2
Admno Name classsec Admno Eng Maths Sc
111 Abhinash 3E 222 98 99 88
222 Arundeep 7B 111 90 100 99

333 Aman 10E

Then Cartesian product will be


Admno Name Classsec Admno Eng Maths Sc
111 Abhinash 3E 222 98 99 88
111 Abhinash 3E 111 90 100 99
222 Arundeep 7B 222 98 99 88
222 Arundeep 7B 111 90 100 99
333 Aman 10E 222 98 99 88
333 Aman 10E 111 90 100 99

Page 8 of 11
(b) Consider the following tables PRODUCTS and SUPPLIERS and answer (b1) and (b2) parts of the
question: Table: PRODUCTS

PID PNAME SUPCODE QTY PRICE COMPANY


101 DIGITAL CAMERA S01 120 12000 CREATIVE
102 DIGITAL PAD S02 100 22000 iBALL
104 PENDRIVE 16GB S01 500 1100 KINGSTON
106 LED SCREEN 32” S02 70 28000 SAMSUNG
105 GPS SYSTEM S03 60 12000 SONY
Table :SUPPLIERS
SUPCODE SNAME CITY
S01 PERFECT COMPUTER SYSTEMS KOLKATA
S02 RAGHAV ENTERPRISES DELHI
S03 BIG VIEW CHENNAI
(b1) Write SQL commands for the following statements: 4 x1=4
(i) To display details of all the products in ascending order of product name(pname).
(ii) To display product name and price of those products whose price is in the range of 10000 and
15000.
(iii) To display the number of products which are supplied by each supplier i.e,the expected output
should be :
S01 2
S02 2
S03 1
(iv) To display product name and quantity of those products which have quantity more than 100.
(b2) Give the output of the following SQL queries: 4x ½ =2
(i) SELECT DISTINCT SUPCODE FROM PRODUCTS;
(ii) SELECT MAX(PRICE), MIN(PRICE) FROM PRODUCTS;
(iii) SELECT PRICE * QTY AMOUNT FROM PRODUCTS WHERE PID=104;
(v) SELECT PNAME,SNAME FROM PRODUCT P, SUPPLIERS S WHERE
P.SUPCODE=S.SUPCODE AND QTY>100;
Ans:
(b1) (i) select * from products order by pname;
(ii)selectpname, price from products where price between 10000 and 15000
(iii)selectsupcode, count(*) from products group by supcode;
(iv)selectpname, qty from products where qty>100
(b2) (i) DISTINCT(SUPCODE)
S01
S02
S03
Page 9 of 11
(ii) MAX(PRICE) MIN(PRICE)
28000 1100
(iii) AMOUNT
550000
(iv) PNAME SNAME
DIGITAL CAMERA PERFECT COMPUTER SYSTEMS
PEN DRIVE 16GB PERFECT COMPUTER SYSTEMS

______________________________________________________________________________________

7. What do you understand by Candidate Keys in a table? Give a suitable example of Candidate Keys from a
table containing some meaningful data. 2
Ans:
A table may have more than one such attribute/group of attribute that identifies a row/tuple uniquely. All such
attributes are known as Candidate keys. Out of the candidate keys, one will be the primary key.

TCode initialName Sub_Code


1 BRL 301
2 GTR 301
3 SJT 041
4 PKC 083
5 MNM 042
6 ASN 041
7 NER 043

In the above example TCode or initialName are candidate keys

(b ) Consider the following tables SUBJECTS and TEACHER and answer the questions (i) to (v) and give
output for (vi) to (vii) : 1X5+ ½X2

Table: SUBJECT
Code Title Marks _Theory Marks_Prac
301 English 100 0
041 Maths 100 0
083 Computer Sc. 70 30
042 Physics 70 30
043 Chemistry 70 30

Table: TEACHER
TCode Name Sub_Code
1 P. Jain 301
2 R. Nagpal 301
3 Supatra 041
4 Shabnam 083
5 Rashika 042
6 Vidushi 041
7 Yash 043

(i) To display each teacher’s name along with his/her respective subject name.

Page 10 of 11
(ii) To display the names of all the subjects for which practical marks are 0.
(iii) To display the total number of teachers in each subject separately.
(iv) To display the names of all the teachers in the ascending order of the Sub_Code.
(v) To display each subject’s details along with Total_Marks in each subject from the table SUBJECT.
(Total_Marks = Marks_Theory + Marks_Practical).

(vi) SELECT DISTINCT(Marks_Theory) from SUBJECT;


(vii) SELECT TCode, Name from Teacher where Sub_Code like ‘0%’;

Ans:
(i) select name, title from teacher, subject where teacher.sub_code=subject.ode;
(ii) select title from subject where marks_prac=0;
(iii) select sub_code, count(*) from teacher group by sub_code;
(iv) select name from teacher order by sub_code;
(v) select *, marks_theory+marks_pracTotal_mark from subject;

OUTPUT
(vi) 100
70
(vii)
3 Supatra
4 Shabnam
5 Rashika
6 Vidushi
7 Yash

Page 11 of 11

You might also like