Transcript
Page 1: Structure Query Language - GAPS · Structure Query Language ... S4 Chocolate Amul 250 10 Mumbai S5 Sauce 300 36 Jaipur ... Add a new coloumn ‘Total’ of integer type width is 5

1

GAPS

ASHOK VIHAR

Structure Query LanguageDatabase: A database is an organised collection of data.

DBMS:A collection of programs that enables you to store, modify and extract information from a database.

Relational Database: A database in which the data is stored in the form of relations(also called tables) is called a Relational Database.

RDBMS: A DBMS used to manage Relational Databases is called an RDBMS(Relational DabaBase Management System). Some popular RDBMS software available are Oracle, MySQL, Sybase, Ingress.

Relation: A relation is two dimension table.

Attributes. The coloumn of the table are called attributes

Tuples: The rows of the table are called tuples.

Degree: The number of attributes in a relation is called the degree of the relation.

Cardinality: The number of tuples(rows) in the relation is called its cardinality.

Primary Key: The group of one or more coloumn used to uniquely identify each row of a relation is called its Primary Key.

Candidate Key: A coloumn or a group of columns which can be used as the primary key of relation is called a candidate key.

Alternate key: A candidate key of table which is not made its primary key is called its Alternate key.

Foreign Key: A foreign key is a column in a table where that column is a primary key of another table.

Categories of SQL:

(i) DDL(Data Definition Language): This is the category of SQL commands. All the commands which are used to create, destory or resturcture databases and tables come under this category. Examples of DDL commands are -CREATE, DROP, ALTER.

(ii) DML(Data Manipulation Language): This is a category of SQL Command. All the commands which are used to manipulae data within tables comes under this category. Examples: Insert,Update,Delete.

(iii)DCL(Data Control Language): All the commands which are used to control the access to databases and tables fall under this category. Examples -Grant, Revoke.

Benefits of using a DBMS are: Redundancy can be controlled, Inconsistence can be avoided, Data can be shared, Security restrictions can be applied.

Q1Write SQL commands for (a) to (f) and write the outputs for (g) on the basis of table HOSPITAL. [D 1998]

Table : HOSPITAL

No Name Age Department Charges Sex1 Arpit 62 21/01/98 300 M2 Zarina 22 12/12/97 250 F3 Kareem 32 19/02/98 200 M4 Arun 12 11/01/98 300 M

SQL

Page 2: Structure Query Language - GAPS · Structure Query Language ... S4 Chocolate Amul 250 10 Mumbai S5 Sauce 300 36 Jaipur ... Add a new coloumn ‘Total’ of integer type width is 5

2

GAPS

ASHOK VIHAR

5 Zubin 30 12/01/98 250 M6 Ketika 16 24/02/98 250 F7 Ankita 29 20/02/98 800 F8 Zareen 45 22/02/98 300 F9 Kush 19 13/01/98 800 M10 Shilpa 23 21/02/98 400 F

(a) To select all the information of patients of cardiology department.(b) To list the names of female patients who are in ENT department.(c) To list names of all patients with their date of admission in ascending order.(d) To display patient’s name, charges, age for only female patients.(e) To count the number of patient with Age < 30.(f) To insert a new row in the HOSPITAL table with the following data:11, “Aftab”, 24, “Surgery”. {25/02/98}, 300, “M”(g) Give the output of following SQL statements:(i) SELECT COUNT(DISTINCT charges) FROM hospital(ii) SELECT MIN(age) FROM hospital WHERE sex = “F”(iii) SELECT SUM(charges) FROM hospital WHERE department = “ENT”:(iv) SELECT AVG(charges) FROM hospital WHERE dateofadm<{12/02/98}

Q2 Consider the following tables EMPLOYEE. Write SQL commands for the statements (a) to (f)

Table : EmployeeNo Name Age Department Salary Sex1 Juagal 34 Computer 12000 M2 Sharmila 31 History 20000 F3 Sandeep 32 Maths 25000 M4 Sangeeta 35 History 30000 F5 Rakesh 42 Maths 21000 M

a. To display the name of all employees who are in the area of south.b. To display name and age of all employees whose age > 40.c. To display list of all employees whose salary >= 30000 .d. To display the employee names in ascending order of age.e. To display all information of all employees those name starts with “A”.f. To display name and age of all male employees those salary is between 20000 and 25000.

Q3 Write the SQL commands for the questions from (a) to (j) on the basis of table FAMILY.No Name FemalMe

mbersMaleMembers Income Occupation

1 Mishra 3 2 67000 Service2 Gupta 4 1 150000 Business3 Khan 6 3 48000 Mixed4 Chaddha 2 2 125000 Business5 Yadav 7 2 Mixed

(a) To select income of family whose occupation is mixed.(b) To show all information where female members are more than 3.(c) To list the name of family with income in ascending order.(d) To display family name , male members and occupation those income greater than 50000.(e) To count the mumber of family whose income is less than 110000(f) To insert a new record in the family table with the following data

SQL

Page 3: Structure Query Language - GAPS · Structure Query Language ... S4 Chocolate Amul 250 10 Mumbai S5 Sauce 300 36 Jaipur ... Add a new coloumn ‘Total’ of integer type width is 5

3

GAPS

ASHOK VIHAR

9,’D souza’, 2,1,115000,’Service’(g)To show all information in descending order of name(h) To show all information whose name end with “C”(i) To list the name of family where male members are less than 2(j) To list the information whose income is null

Q4 Write the SQL command for (a) to (j) Table : StudentNo Name Age Department Fees1 Pankaj 24 Computer 3002 Shalini 21 History 2003 Sanjay 22 Hindi 3004 Sudha 25 History 2505 Rakesh 20 Hindi 210

(a) To Show all information about the student of History Department.(b) To list the names of students who are in Hindi department.(c) To list name of all students with their age in ascending order(d) To display student’s name, charges, age of all students.(e) To display all the information of students in descending order of fees.(f) Show all information those name ends with S.(g) To count the no of students those age<22(h) Delete all records those marks fees less than 300(i) Show the structure of table student(j) Increase all fees with 200

Q5 Write the SQL commands for the questions from (i) to (x) on the basis of table student. Rollno Name Marks Grade Fees Subject1 Mishra 30 C 6000 Commerce2 Gupta 48 B 15000 Arts3 Khan 66 A 4800 Science4 Chaddha 24 C 12500 Commerce5 Yadav A 10000 Arts

(i) To select all the information of student whose subject is commerce.(ii) To list the name of student where marks greater than 40.(i) To list the name of student with grade in ascending order.(ii) To display name , marks and grade of Arts students.(iii) To count the number of students whose fees is less than 11000(iv) To insert a new record in the student table with the following data 6,’souza’, 90,’A’,11500,’Science’(vii) To show all information in descending order of name(viii) To show all information whose name start with “K”(ix) To list the name of student where grade is “A”(x) To list the information whose marks is null

Q6 Write the SQL commands for (a) to (f) and write the output of (g) on the basis of table:Table : Product

SQL

Page 4: Structure Query Language - GAPS · Structure Query Language ... S4 Chocolate Amul 250 10 Mumbai S5 Sauce 300 36 Jaipur ... Add a new coloumn ‘Total’ of integer type width is 5

4

GAPS

ASHOK VIHAR

No. Name Price Supplier Stock11 Motherboard 7000 Intel 202 Keyboard 1000 TVSE 703 Mouse 500 Logitech 604 Soundcard 600 Samsung 505 Speaker 600 Samsung 256 Monitor 3000 Philips 227 CD ROM 2800 Creative 328 Printer 7900 HP 10

(a) Display data for the entire item sorted by their name.(b) Display the name and price from the table item in descending order of their stock.(c) List all name and price wth price between 3000 and 7000.(d) Write the command to set the price field of all product to 1200 corresponding to Name=”keyboard”(e) Write the SQL command to delete rows with stock between 20 to 40.(f) To count the number of products with stock<5(g)Give the output of the following SQL command:(i) Select Sum(stock) from Product.(ii) Select AVG(stock) from Product where stock>20;(iii)Select count(Distinct stock) from product.(iv) Select max(price) rom product.

Q7 Write SQL Commands for the questions from (a) to (j) on the basis of table SUPPLIER.Table : SupplierSno Pname Sname Qty Price CityS1 Bread Britannia 150 8 DelhiS2 Cake Britanni 250 20 MumbaiS3 Coffee Nescafe 170 45 DelhiS4 Chocolate Amul 250 10 MumbaiS5 Sauce 300 36 Jaipur

a. Display data for all products whose quantity is between 170 and 370.b. Display data for all products sorted by their quantity. c. Give Sname for that entire product whose name starts with “C”.d. To list Sname,Pname,Price for all the products whose quantity is <200.e. To display Sno,Pname,Sname,Qty in descending order of quantity from the SUPPIER table.f. Delete the records those price less than 20.g. Increase the price by 5 of all products.h. Add a new coloumn ‘Total’ of integer type width is 5.i. Display all information in descending order of Pnamej. Delete the table physically.

Q8 Consider the following table ACTIVITY .Write MYSQL commands for the statements (i) to (x) Table: ACTIVITY

ACode ActivityName ParticipantsNum PrizeMoney ScheduleDate1001 Relay 100x4 16 10000 23-Jan-20041002 High jump 10 12000 12-Dec-20031003 Shot Put 12 8000 14-Feb-20041005 Long Jump 12 9000 01-Jan-20041008 Discuss Throw 10 19-Mar-2004

SQL

Page 5: Structure Query Language - GAPS · Structure Query Language ... S4 Chocolate Amul 250 10 Mumbai S5 Sauce 300 36 Jaipur ... Add a new coloumn ‘Total’ of integer type width is 5

5

GAPS

ASHOK VIHAR

(i) To display the name of all activities with their Acodes in descending order.(ii) Show all the data of all activities those prizemoney is NULL (iii)To display the activity name and ACodes in descending order of ACode from the table Activity.(iv) To display the content of the Activity table whose ScheduleDate earliar than 01/01/2004 in ascending order of ParticipantNum.(v) To display all information of Activity whose Activity name starts with A.(vi)Show acode whose Participants num is 10(vii)Delete all records those Acode is less than 1003.(viii)Count the no of records of each participantsNum.(ix)Increae the prizemoney by 1000 of all activities(x)Show all information those Acode is 1001

Q9Consider the following tables PREPAID. Write SQL commands for the statement (i) to (iv) and give out-puts for SQL queries (v) to (viii)[Compt 2005]

Table : PREPAIDS.No. Cname Model Connection Activation_Date Validity

(in days)Amount(paid in Rs)

1 Sita Nokia Airtel 04/06/2004 365 33002 Ritesh Nokia Hutch 12/02/2005 60 30303 Reena Samsung Hutch 14/05/2003 365 30304 Meetali Nokia Indicom 24/09/2004 180 8005 Ramanju Samsung Idea 16/10/2004 180 9006 Anupam LG Hutch 18/01/2005 60 6007 Sai Ram Nokia Indicom 10/12/2004 180 8008 Deepa LG Reliance 08/03/2005 30 3009 Karan Sony Idea 30/01/2005 90 80010 Jayant Siemen Airtel 02/02/2005 60 560

(i) To display Cname and Amout of customers having “Samsung” model and “Hutch” connection, arranged in descending order of Amount.(ii) To display different types of connection available.(iii) To display sum off validity for each type of connection grouping.(iv) to display cname,connection and date of expiry. Where date of expiry is date of activation (of connec-tion) + validity(number of days valid).(v) Select count(distinct Model) from PREPAID(vi) Select Max(Activation), Min(Activation) from PREPAID(vii)Select avg(Validity) from PREPAID where Model=”LG”(viii)Select Max(amount) from PREPAID where connection =”IDEA”

Q10 Consider the following table: [compt 2004]Table:SchoolBus

Rtno area_covered capacity noofstudents distance Transporter charges1 Vasant Kunj 100 120 10 Shivam Travel 1000002 Hauz Khas 80 80 10 Anand Travel 850003 Pitampura 60 55 30 Anand Travel 600004 Rohini 100 90 35 Shivam Travel 750005 Yamuna Vihar 50 60 45 Anand Travel 450006 Krishna Nagar 70 80 30 Yadav Co. 800007 Vasundhara 80 110 20 Yadav Co. 1000008 Paschim Vihar 100 40 20 Speed Travels 55000

SQL

Page 6: Structure Query Language - GAPS · Structure Query Language ... S4 Chocolate Amul 250 10 Mumbai S5 Sauce 300 36 Jaipur ... Add a new coloumn ‘Total’ of integer type width is 5

6

GAPS

ASHOK VIHAR

9 Saket 120 120 10 Speed Travels 10000010 Janak Puri 100 100 20 Kisan Tours 95000

Write the SQL commands for the following:(i) To show all records students more than the capacity in order of RtNo.(ii)to show area_covered for buses covering more than 20 km but charges less than 80000.(iii) To show Transporter wise total no. of students traveling.(iv) To show Rtno, area_covered and average cost per student for all routes.(v) Add a new coloumn drivername in the school bus relation.(vi) Add a new record with the following data:(11,”Moti Bagh”,35,32,10,”Kisan Tours”,35000)(vii) Give the output(a) Select sum(distance) from schoolbus where transporter=”Yadav Travels”(b) Select min(noofstudents) from schoolbus(c) Select avg(charges) from schoolbus where transporter=”Anand Travels”(d) Select distinct transporter from schoolBus.

Q11 (a)Write SQL commands for (i) to (vii) on the basis of the table STUDENT

TABLE : STUDENT

StudentNo. Class Name Game GGrade SupW SGrade10 7 Sameer Cricket B Photography A11 8 Sujit Tennis A Gardening C12 7 Kamal Swimming B Photography B13 7 Veena Tennis C Cooking A14 9 Archna Basket Ball A Literature A15 10 Arpit Cricket A Gardening C

(i) Display the names of the students who are getting grade ‘C’ in either Game or SupW.(ii) Display the number of students getting grade ‘A’ in Cricket.(iii) Display the different games offered in the school.(iv) Display the SUPW taken up by the students, whose name starts with „A‟.(v) Add a new column named ‘Marks’.(vi) Assign a value 200 for Marks for all those who are getting Grade ‘B’ or above in Game.(vii) Arrange the whole table in the alphabetical order of SUPW.(b) If R1 is a relation with 5 rows and R2 is a relation with 3 rows, how many rows will the Cartesian product of R1 and R2 have ?

Q12 Table : Directory

No Fname Lname Phone Address1 Arpit Kumar 7045634 Rohini2 Ram Sharma 5563412 Vikas Puri3 Vikas Malhotra 7865467 PitamPura4 Rohit Arora 2235434 Preet Vihar5 Kisan kaushik 5567845 Pashim Vihar6 Rahul Verma 7057456 Rohini7 Rakesh Gulati 7026519 Pitam Pura

.

(a) To select all the information of employees of Rohini area.

SQL

Page 7: Structure Query Language - GAPS · Structure Query Language ... S4 Chocolate Amul 250 10 Mumbai S5 Sauce 300 36 Jaipur ... Add a new coloumn ‘Total’ of integer type width is 5

7

GAPS

ASHOK VIHAR

(b) Update the database set the phone no as 7047645 where phone number is 7057456.(c) To display the data for Arpit,Rahul and Kisan.(d) To delete the rows where the address is Rohini.(e) To delete the table Physically.(f) Give the output(i) Select count(Distinct address) from Directory(ii) Select Fname,Lname fromDirectory where phone=7026519(iii) Select Phone from Directory where address like ‘V%’(iv)Select Fname,Phone from Directory where Fname like ‘R _ _ _ _’

Q13(i) What is the need for normalization ? Define first, second and third normal forms.(ii)Write SOL commands for (b) to (e) and write the outputs for (g) on the basis of table CLUB.[ D 2K]

TABLE : CLUB

Coach ID CachNAME AGE SPORTS Dateofapp PAY SEX1 KUKERJA 35 KARATE 27/03/1996 1000 M2 RAVINA 34 KARATE 20/ 01/1998 1200 F3 KARAN 34 SQUASH 19/02/1998 2000 M4 TARUN 33 BASKETBALL 01/01/1998 1500 M5 ZUBIN 36 SWIMMING 12/01/1998 750 M6 KETAKI 36 SWIMMING 24/02/1998 800 F7 ANKITA 39 SQUASH 20/02/1998 2200 F8 ZAREEN 37 KARATE 22/02/1998 1100 F9 KUSH 41 SWIMMING 13/01/1998 900 M10 SHAILYA 37 BASKETBALL 19/02/1998 1700 M

(b) To show all information about the swimming coaches in the club.(c) To list name of all coaches with their date of appointment (DATEOFAPP) in descending order.(d) To display a report, showing coachname, pay, age and bonus (15% of pay) for all the coaches.(e) To insert a new row in the CLUB table with the following data :11, “PRAKASH”, 37, “SQUASH”, {25/02/98}, 2500, “M”(f) Give the output of following SQL statements :(i) SELECT COUNT (DISTINCT sports) FROM club;(ii) SELECT MIN (age) FROM club WHERE sex = “F”;(iii)SELECT AVG(pay) FROM club WHERE sports =”KARATE”;(iv) SELECT SUM(pay) FROM club WHERE dateofapp > {31/01/98};(g) Assume that there is one more table COACHES in the database as shown below:

TABLE:COACHES

SPORTS PERSON SEX COACH-NOAJAY M 1SEEMA F 2VINOD M 1TANEJA F 3

What will be the output of the following query :

SQL

Page 8: Structure Query Language - GAPS · Structure Query Language ... S4 Chocolate Amul 250 10 Mumbai S5 Sauce 300 36 Jaipur ... Add a new coloumn ‘Total’ of integer type width is 5

8

GAPS

ASHOK VIHAR

SELECT sportsperson, coachname FROM club, coaches WHERE coach id = coach no;

Q14Consider the following tables Consignor and Consignee. Write SQL commands for the statements (i) to (iv) and give outputs for SQL queries (v) to (viii). [Al 2007]

TABLE : CONSIGNORCnorlD CnorName CnorAddress CityND01 R Singhal 24, , ABC Enclave New DelhiND02 Amit Kumar 123, Palm Avenue New DelhiMU15 R Kohli 5/A, South Street MumbaiMU50 S Kaur 27-K, Westend Mumbai

TABLE : CONSIGNEECneelD CnorlD CneeName CneeAddress CneeCityMU05 ND01 Rahul Kishore 5, Park Avenue , Park Avenue

ND08 ND02 P Dhingra 16/J, Moore Enclave New DelhiKO19 MU15 A P Roy 2A Central Avenue KolkataMU32 ND02 S Mittal P 245, AB Colony Mumbai ND48 MU50 B P Jain 13, Block D, A Vihar New Delhi

(i) To display the names of all Consignors from Mumbai.’(ii) To display the CneelD, CnorName, CnorAddress, CneeName, CneeAddress for every Consignee.(iii) To display consignee details in ascending order of CneeName.(iv) To display number of consignors from each city,(v) SELECT DISTINCT City FROM CONSIGNEE;(vi) SELECT A.CnorName, B.CneeName FROM Consignor A, Consignee B WHERE A.CnorID = B.CnorlD AND B.CneeCity = ‘Mumbai’;(vii) SELECT CneeName, CneeAddress FROM Consignee WHERE CneeCity NOT IN (‘Mumbai’, ‘Kol-kata’);(viii) SELECT CneelD, CneeName FROM Consignee WHERE CnorID=’MU15’ OR CnorID=’ND01’;

Q15 Consider the following tables EMPLOYEE and SALGRADE and answer (b) and ( c ) parts of this questions:

TABLE :EMPLOYEEECODE NAME DESC SGRADE DOJ DOB11 AMIT EXECUTIVE S003 23-03-2003 13-01-198012 RAM SHARMA IT HEAD S002 12-02-2010 22-07-198713 CHITRA RECEPTIONIST S003 24-06-2009 24-07-198314 NARESH GM S002 11-08-2006 03-03-198418 PRIYA CEO S001 29-12-2004 19-01-1982

TABLE :SALGRADESGRADE SALARY HRAS001 56000 18000S002 32000 12000S003 24000 8000

(b)Write SQL commands for the following statements: 1.To display NAME and DESC of those EMPLOYEEs, whose SALGRADE is either S002 or S003.

SQL

Page 9: Structure Query Language - GAPS · Structure Query Language ... S4 Chocolate Amul 250 10 Mumbai S5 Sauce 300 36 Jaipur ... Add a new coloumn ‘Total’ of integer type width is 5

9

GAPS

ASHOK VIHAR

2.To display the details of all EMPLOYEEs in descending order of DOJ.3.To display the content of the EMPLOYEEs table, whose DOJ is in between ’09-02-2006’ and ’08-08-2009’.4.To add a new row with the following:19,” Harish’, “IT HEAD”,’S002’,’09-09-2007’, ’21-04-1983’.(c) Give the output of the following SQL queries: I.SELECT COUNT(SGRADE),SGRADE FROM EMPLOYEE GROUP BY SGRADE;II.SELECT NAME ,SALARY FROM EMPLOYEE E,SALGRADE S WHERE E.SGRADE=S.SGRADE AND E.ECODE<13;III.SELECT SGRADE,SALARY+HRA FROMSALGRADE WHERE SGRADE=’S002’;IV.SELECT MIN(DOB),MAX(DOJ)FROM EMPLOYEE;

Q16 Consider the following tables Sender and Recipient. Write SQL commands for the statements (i) to (iv) and give outputs for SQL queries (v) to (viii) [D 07]

TABLE:SENDERSenderlD SenderName SenderAddress SenderCiryND01 R Jain 2, ABC Appts New DelhiMU02 H Sinha 12, Newtown MumbaiMU15 S Jha 27/A, Park Street MumbaiND50 T Prasad 122-K, SDA New Delhi

TABLE : RECIPIENT RecID SenderlD RecName RecAddress RecCiryKO05 ND01 R Bajpayee 5, Central Avenue KolkataND08 MU02 S Mahajan 116, A Vihar New DelhiMU19 ND01 H Singh 2A, Andheri East MumbaiMU32 MU15 P K Swamy B5, C S Terminus MumbaiND48 ND50 S T ripathi 13, B1 D, Mayur Vihar New Delhi

(i) To display the names of all Senders from Mumbai(ii) To display the RecID), SenderName, SenderAddress, RecName, RecAddress for every Recipient(iii) To display Recipient details in ascending order of RecName(iv) To display number of Recipients from each city(v) SELECT DISTINCT SenderCity FROM Sender;(vi) SELECT A. SenderName, B.RecName FROM Sender A, Recipient B WHERE A. SenderlD = B.SenderlD AND B.RecCity = ‘Mumbai’;(vii) SELECT RecName, RecAddress FROM Recipient WHERE RecCity NOT IN (‘Mumbai’, ‘Kolkata’);(viii) SELECT RecID, RecName FROM Recipient WHERE SenderID=’MU02’ ORSenderID=’ND50’;

Q17.(a) What do you understand by Degree and Cardinality of a table? Consider the following tables ACTIVITY and COACH and answer(b) and (c) parts of this question:

Table: ACTIVITYA Code ActivityName Stadium Participants

NumPrize Money Schedule Date

1001 Relay 100x4 Star Annex 16 10000 23-Jan-20041002 High jump Star Annex 10 12000 12-Dec-20031003 Shot Put Super Power 12 8000 14-Feb-20041005 Long Jump Star Annex 12 9000 01-Jan-20041008 Discuss Throw Super Power 10 15000 19-Mar-200412

Table: COACH

SQL

Page 10: Structure Query Language - GAPS · Structure Query Language ... S4 Chocolate Amul 250 10 Mumbai S5 Sauce 300 36 Jaipur ... Add a new coloumn ‘Total’ of integer type width is 5

10

GAPS

ASHOK VIHAR

PCode Name Acode1 Ahmad Hussain 10012 Ravinder 10083 Janila 10014 Naaz 1003

(b) Write SQL commands for the flowing statements: (i) To display the names of all activities with their Acodes in descending order.(ii) To display sum of PrizeMoney for the Activities played in each of the Stadium separately.(iii) To display the coach’s name and ACodes in ascending order of ACode from the table COACH(iv) To display the content of the Activity table whose ScheduleDate earlier than 01/01/2005 in ascending order of ParticipantsNum.(c) Give the output of the following SQL queries: (i) SELECT COUNT(DISTINCT ParticipantsNum) FROM ACTIVITY;(ii) SELECT MAX(ScheduleDate),MIN(ScheduleDate) FROM ACTIVITY;(iii) SELECT Name,ActivityName FROM ACTIVITY A,COACH C WHERE A.Acode=C.Acode AND A.ParticipantsNum=10;(iv) SELECT DISTINCT Acode FROM COACH;

Q18 (a) What are candidate keys in a table? Give a suitable example of candidate keys in a table. (b) Consider the following tables GARMENT and FABRIC. Write SQL commands for the statements (i) to (iv) and give outputs for SQL queries (v) to (viii) [ D 09]

Table: GARMENTGCODE DESCRIPTION PRICE FCODE READYDATE10023 PENCIL SKIRT 1150 F03 19-DEC-0810001 FORMAL SHIRT 1250 F01 12-JAN-0810012 INFORMAL SHIRT 1550 F02 06-JAN-0810024 BABY TOP 750 F03 07-APR-0710090 TULIP SKIRT 850 F02 31-MAR-0710019 EVENING GOWN 850 F03 06-JUN-0810009 INFORMAL PANT 1500 F02 20-OCT-0810007 FORMAL PANT 1350 F01 09-MAR-0810020 FROCK 850 F04 09-SEP-0710089 SLACKS 750 F03 31-OCT-08

Table: FABRICFCODE TYPEF04 POLYSTERF02 COTTONF03 SILKF01 TERELENE

(i) To display GCODE and DESCRIPTION of each GARMENT in descending orderof GCODE(ii) To display the details of all the GARMENTs, which have READYDATE in between 08-DEC-07 and 16-JUN-08 (inclusive of both the dates).(iii) To display the average PRICE of all the GARMENTs, which are made up of FABRIC with FCODE as F03.(iv) To display FABRICwise highest and lowest price of GARMENTs from GARMENT table. (Display FCODE of each GARMENT along with highest and lowest price).(v) SELECT SUM(PRICE) FROM GARMENT WHERE FCODE=‟F01‟;(vi) SELECT DESCRIPTION, TYPE FROM GARMENT, FABRIC WHERE GARMENT.FCODE =FAB-RIC.FCODE AND GARMENT.PRICE > = 1260;

SQL

Page 11: Structure Query Language - GAPS · Structure Query Language ... S4 Chocolate Amul 250 10 Mumbai S5 Sauce 300 36 Jaipur ... Add a new coloumn ‘Total’ of integer type width is 5

11

GAPS

ASHOK VIHAR

(vii) SELECT MAX(FCODE) FROM FABRIC;(viii) SELECT COUNT (DISTINCT PRICE) FROM GARMENT;

Q19(a) What do you understand by Union & Cartesian Product operations inrelational algebra? Consider the following tables WORKER and PAYLEVEL and answer (b) and (c)parts of this question:Table: WORKER [D 11]

ECODE NAME DESIG PLEVEL DOJ DOB11 Radhe Shyam Supervisor P001 13-Sep-2004 23-Aug-198112 Chander Nath Operator P003 22-Feb-2010 12-Jul-198713 Fizza Operator P003 14-Jun-2009 14-Oct-198315 Ameen Ahmed Mechanic P002 21-Aug-2006 13-Mar-198418 Sanya Clerk P002 19-Dec-2005 09-Jun-1983

Table: PAYLEVELPLEVEL PAY ALLOWANCEP001 26000 12000P002 22000 10000P003 12000 6000

b) Write SQL commands for the following statements: (i) To display the details of all WORKERs, descending order of DOB.(ii) To display NAME and DESIG of those WORKERs whose PLEVEL is either P001 or P002.(iii)To display the content of all the WORKERs table, whose DOB is in between „19-JAN-1984‟ and 18-JAN-1987‟.(iv) To add a new row with the following :19, „Days Kishore‟, „Operator‟, „P003‟. „19-Jun-2008‟, „11-Jul-1984‟(c) Give the output of the following SQL queries : 2(i) SELECT COUNT (PLEVEL), PLEVEL FROM WORKER GROUP BY PLEVEL;(ii) SELECT MAX(DOB), MIN(DOJ) FROM WORKER;(iii) SELECT Name, Pay FROM WORKER W, PAYLEVEL P WHERE W.PLEVEL = S.PLEVEL AND P.ECODE<13;(iv) SELECT PLEVEL, PAY+ALLOWANCE FROM PAYLEVEL WHERE PLEVEL = „P003‟;

Q20 Consider the following tables STORE and SUPPLIERS and answer (b1) and(b2) parts of this question. [ D 10]

Table: STOREltem No Item Scode Qty Rate LastBuy2005 Sharpener 23 60 8 31-jun-092003 Ball 22 50 25 01-Feb-102002 Gel Pen Premium 21 150 12 24-Feb-102006 Gel Pen Classic 21 250 20 11-Mar-092001 Eraser Small 22 220 6 19-Jan-092004 Eraser Big 22 110 8 02-Dec-092009 Ball Pen 0.5 21 180 18 03-Nov-09

Table :SUPPLIERSscode Sname21 Premium Stationers

SQL

Page 12: Structure Query Language - GAPS · Structure Query Language ... S4 Chocolate Amul 250 10 Mumbai S5 Sauce 300 36 Jaipur ... Add a new coloumn ‘Total’ of integer type width is 5

12

GAPS

ASHOK VIHAR

23 Soft Plastics22 Tera Supply

b1) Write SQL commands for the following statements :(i) To display details of all the items in the Store table in ascending order of LastBuy.(ii) To display ItemNo and Item name of those items from Store table Whose Rate is more than 15 Rupees.(iii)To display the details of those items whose Suppliers code (Scode) is 22 or Quantity in Store (Qty) is more than 110 from the table Store.(iv) To display Minimum Rate of items for each Supplier individually as per Scode from the table Store.(b2) Give the output of the following SQL queries:(i) SELECT COUNT (DISTINCT Scode) FROM Store;(ii) SELECT Rate*Qty FROM Store WHERE ItemNO = 2004;(iii)SELECT Item, Sname FROM Store S, Suppliers P WHERE S.Scode = P.Scode AND Item No = 2006 ;(iv) SELECT MAX (LastBuy) FROM Store ;

Q21. a.What is an Alternate Key? b.Study the following tables DOCTOR and SALARY and write SQL commands for the questions (i) to (iv) and give outputs for SQL queries (v) to (vi): [D 06]TABLE : DOCTORID NAME DEPT SEX EXPERIENCE101 John ENT M 12104 Smith ORTHOPEDIC M 5107 George CARDIOLOGY M 10114 Lara SKIN F 3109 K George MEDICINE F F 9105 Johnson ORTHOPEDIC M 10117 Lucy ENT F 3111 Bill MEDICINE F 12130 Morphy ORTHOPEDIC M 15

TABLE : SALARY1D BASIC ALLOWANCE CONSULTATION101 12000 1000 300104 23000 2300 500107 32000 4000 500114 12000 5200 100109 42000 1700 200105 18900 1690 300130 21700 2600 300

i.Display NAME of all doctors who are in “MEDICINE” having mo than 10 years experience from the table DOCTOR. ii.Display the average salary of all doctors working in “ENT” department using the tables DOCTOR and SALARY. Salary = BASIC + ALLOWANCE iii.Display the minimum ALLOWANCE of female doctors. iv.Display the highest consultation fee among all male doctors. v.SELECT count( * ) from DOCTOR where SEX “F” vi.SELECT NAME, DEPT, BASIC from DOCTOR, SALARY where DEPT = “ENT” and DOCTOR.ID = SALARY.ID

SQL

Page 13: Structure Query Language - GAPS · Structure Query Language ... S4 Chocolate Amul 250 10 Mumbai S5 Sauce 300 36 Jaipur ... Add a new coloumn ‘Total’ of integer type width is 5

13

GAPS

ASHOK VIHAR

Q22(a) What do you understand by normalization ? What is First Normal Form ?

Given the following tables for a database LIBRARY [D 04]

Table : Books

Book_Id Book_Name Author_Name Publishers Price Type QuantityF0001 The Tears William Hopkins First Publ. 750 Fiction 10F0002 Thunderbolts Anna Roberts First Publ. 700 Fiction 5T0001 My First C++ Brian & Brooke EPB 250 Text 10T0002 C++ Brainworks A.W. Rossaine TDH 325 Text 5C0001 Fast Cook Lata Kapoor EPB 350 Cookery 8

Table : Issued

Book_Id Quantity_IssuedF0001 3T0001 1C0001 5

Write SQL queries for (b) to (g) :

(b) To show Book name, Author name and Price of books of EPB publishers (c) To list the names of books of Fiction Type (d) To display the names and price of the books in descending order of their price (e) To increase the price of all books of First Publ. by 50 (f) To display the Book_Id, Book_Name and Quantity_Issued for all books which have been issued. (The query will require contents from both the tables) (g) To insert a new row in the table Issued having the following data : “F0002”, 4(h) Give the output of the following queries based on the above tables : (i) SELECT COUNT(DISTINCT Publishers) FROM Books(ii) SELECT SUM(Price) FROM Books WHERE Quantity >5(iii) SELECT Book_Name, Author_Name FROM Books WHERE Price<500(iv) SELECT COUNT (*) FROM Books

Q23Consider the following tables EMPLOYEES and EMPSALARY. Write SQL commands for the state-ments (i) to (iv) and give outputs for SQL queries (v) to (viii). [ D 05]

EMPLOYEESEMPID FIRSTNAME LASTNAME ADDRESS CITY010 George Smith 83 First Street Howard105 Mary Jones 842 Vine Ave. Losantiville152 Sam Tones 33 Elm St. Paris215 Sarah Ackerman 440 U.S. 110 Upton244 Manila Sengupta 24 Friends Street New Delhi300 Robert Samuel 9 Fifth Cross Washington335 Henry Williams 12 Moore Street Boston400 Rachel Lee 121 Harrison St. New York441 Peter Thompson 11 Red Road Paris

EMPSALARYEMPID SALARY BENEFITS DESIGNATION010 75000 15000 Manager

SQL

Page 14: Structure Query Language - GAPS · Structure Query Language ... S4 Chocolate Amul 250 10 Mumbai S5 Sauce 300 36 Jaipur ... Add a new coloumn ‘Total’ of integer type width is 5

14

GAPS

ASHOK VIHAR

105 65000 15000 Manager152 80000 25000 Director215 75000 12500 Manager244 50000 12000 Clerk300 45000 10000 Clerk335 40000 10000 Clerk400 32000 7500 Salesman441 28000 7500 Salesman

(i) To display Firstname, Lastname, Address and City of all employees living in Paris from the table EM-PLOYEES.(ii) To display the content of EMPLOYEES table in descending order of FIRSTNAME.(iii) To display the Firstname, Lastname, and Total Salary of all Managers from the tables EMPLOYEES and EMPSALARY, where Total Salary is calculated as Salary + Benefits.(iv) To display the Maximum salary among Managers and Clerks from the table EMPSALARY.(v) SELECT FIRSTNAME, SALARY FROM EMPLOYEES, EMPSALARY WHERE DESIGNATION =‘Salesman’ AND EMPLOYEES.EMPID=EMPSALARY.EMPID;(vi) SELECT COUNT (DISTINCT DESIGNATION)FROM EMPSALARY;(viI) SELECT DESIGNATION, SUM(SALARY) FROM EMPSALARY GROUP BY DESIGNATION HAVING COUNT (*)>2;(viii) SELECT SUM (BENEFITS) FROM EMPLOYEES WHERE DESIGNATION = ’Clerk’;

Q24 Consider the following tables Item and Customer. Write SQL commands for the statements (i) to (iv) and give outputs for SQL queries (v) to(viii). [D 08]

Table : ItemI_ID Itemname Manufacture PricePC01 Personal Computer ABC 35000LC05 Laptop ABC 55000PC03 Personal Computer XYZ 32000PC06 Personal Computer COMP 37000LC03 Laptop PQR 57000

Table : CustomerC_ID Customer name City I_ID01 N Roy Delhi LC0306 H Singh Mumbai PC0312 R Panday Delhi PC0615 C Sharma Delhi LC0316 K Agarwal Banglore PC01

(i) To display the details of those Customers whose City is Delhi(ii) To display the details of Items whose Price is in the range of 35000 to 55000 (Both values included)(iii) To display the CustomerName, City from table Customer and ItemName and Price from table Item, with their corresponding matching I-Id(iv) To increase the Price of all Items by 1000 in the table Item(v) SELECT DISTINCT City FROM Customer;(vi) SELECT ItemName, MAX(Price), Count(*) FROM Item GROUP BY ItemName;(vii) SELECT CustomerName, Manufacturer FROM Item, Customer WHERE Item.Item_Id=Customer.Item.I_Id(viii) SELECT ItemName, Price * 100 FROM Item WHERE Manufacturer =’ABC’;

SQL


Recommended