SQL
SQL
SQL
Write SQL commands for the statement (i) to (iv) and give outputs for SQL queries (v) to (viii) Table: PRODUCT P_ID TP01 FW05 BS01 SH06 FW12 Table: CLIENT C_ID 01 06 12 15 16 (i) To display the details of those Clients whose city is Delhi. Ans: Select all 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 all 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. Ans: 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 Ans: Update Product Set Price=Price +10 (v) SELECT DISTINCT Address FROM Client. Ans: ( The above question may consist DISTINCT City. If it is DISTINCT City, the following is the answer) City ----Delhi Mumbai Bangalore (vi) SELECT Manufacturer, MAX(Price), Min(Price), Count(*) FROM Product GROUP BY Manufacturer; Ans: Manufacturer LAK ABC XYZ Max(Price) 40 55 120 Min(Price) 40 45 95 Count(*) 1 2 2 Client Name TalcomPowder Face Wash Bath Soap Shampoo Face Wash City Delhi Mumbai Delhi Delhi Banglore P_ID FW05 BS01 SH06 FW12 TP01 Product Name TalcomPowder Face Wash Bath Soap Shampoo Face Wash Manufacturer LAK ABC ABC XYZ XYZ Price 40 45 55 120 95
(vii) SELECT ClientName, ManufacturerName FROM Product, Client WHERE Client.Prod_Id=Product.P_Id; Ans: ClientName Cosmetic Shop Total Health Live Life Pretty Woman Dreams ManufacturerName ABC ABC XYZ XYZ LAK
(viii) SELECT ProductName, Price * 4 FROM Product. ProductName Talcom Poweder Face Wash Bath Soap Shampoo Face Wash Price*4 160 180 220 480 380
Outside Delhi 2008: 5.b) Consider the following tables Item and Customer. Write SQL commands for the statement (i) to (iv) and give outputs for SQL queries (v) to (viii) Table: ITEM C_ID PC01 LC05 PC03 PC06 LC03 Table: CUSTOMER C_ID 01 06 12 CustomerName N.Roy H.Singh R.Pandey City Delhi Mumbai Delhi P_ID LC03 PC03 PC06 ItemName Personal Computer Laptop Personal Computer Personal Computer Laptop Manufacturer ABC ABC XYZ COMP PQR Price 35000 55000 32000 37000 57000
15 16
C.Sharma K.Agarwalh
Delhi Banglore
LC03 PC01
(i) To display the details of those Customers whose city is Delhi.Ans: Select all 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 all from Item Where Price>=35000 and Price <=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 Personal Computer Laptop Max(Price) 37000 57000 Count(*) 3 2
(vii) SELECT CustomerName, Manufacturer FROM Item, Customer WHERE Item.Item_Id=Customer.Item_Id; Ans: CustomerName N.Roy H.Singh R.Pandey C.Sharma K.Agarwal Manufacturer Name PQR XYZ COMP PQR ABC
(viii) SELECT ItemName, Price * 100 FROM Item WHERE Manufacturer = ABC; Ans: ItemName Personal Computer Laptop Outside Delhi 2007: 5.b) Consider the following tables Consignor and Consignee. Write SQL command for the statements(i)to(iv) And give outputs for the SQL quries (v) to ( viii). 6 TABLE : CONSIGNOR CnorID ND01 ND02 MU15 MU50 TABLE : CONSIGNEE CneeID MU05 ND08 KO19 MU32 ND48 (i) To display the names of all consignors from Mumbai. Ans: Select CnorName from Consignor where city=Mumbai; (ii) To display the cneeID, cnorName, cnorAddress, CneeName, CneeAddress for every Consignee. Ans: Select CneeId, CnorName, CnorAddress, CneeName, CneeAddress from Consignor,Consignee where Consignor.CnorId=Consignee.CnorId; (iii) To display the consignee details in ascending order of CneeName. Ans: Select * from Consignee Orderby CneeName Asc; (iv) To display number of consignors from each city. Ans: Select city, count(*) from Consignors group by city; (v) SELECT DISTINCT City FROM CONSIGNEE; Ans: CneeCity Mumbai New Delhi Kolkata (vi) SELECT A.CnorName A, B.CneeName B FROM Consignor A, Consignee B WHERE A.CnorID=B.CnorID AND B.CneeCity=Mumbai; (vii) SELECT CneeName,CneeAddress FROM Consignee WHERE CneeCity Not IN (Mumbai, Kolkata); Ans: CnorID ND01 ND02 MU15 ND0 2 MU5 0 CneeName RahulKishore P Dhingr a A P Roy S mittal B P jain CneeAddress 5,Park Avenue 16/j,Moore Enclave 2A,Central/ avenue P 245, AB Colony 13,Block d,a,viha CneeCity Mumbai New Delhi Kolkata Mumbai New Delhi CnorName R singhal AmitKumar R Kohil S Kaur CnorAddress 24,ABC Enclave 123,Palm Avenue 5/A,South,Street 7-K,Westend City New Delhi New Delhi Mumbai Mumbai Price*100 3500000 5500000
(viii) SELECT CneeID, CneeName FROM Consignee WHERE CnorID = MU15 OR CnorID = ND01; senderCity New Delhi Mumbai Ans: CneeID CneeName MU05 Rahul Kishore KO19 A P Roy
Delhi 2007: 5.b) Consider the following tables. Write SQL command for the statements (i)to(iv)and give outputs for the SQL quries (v) to (viii). 6 TABLE : SENDER SenderID ND01 MU02 MU1 5 ND5 0 TABLE :RECIPIENT RecID KO05 ND08 MU19 MU32 ND48 (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 recipt. Ans: 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. 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; CnorName R singhal Amit Kumar SenderID ND01 MU0 2 ND01 MU1 5 ND50 ReCName RBajpayee S Mahajan H sing PK Swamy S Tripathi RecAddress 5,Central Avenue 116, A Vihar 2A,Andheri East B5, CS erminus 13, B1 D,Mayur Vihar ReCCity Kolkata NewDelhi Mumbai Mumbai NewDelhi SenderName R jain H sinha S haj T Prasad Sender Address 2,ABC Appts 12, Newton 27/ A,Park Street 122-K,SDA Sender City New Delhi Mumbai New Delhi Mumbai
(v) SELECT DISTINCT SenderCity FROM Sender; Ans: (vi) SELECT A.SenderName A, B.RecName FROM Sender A, Recipient B WHERE A.SenderID=B. SenderID AND B.RecCity=Mumbai; Ans: SenderName R.Jain S.Jha RecName H.Singh P.K.Swamy
(vii) SELECT RecName,RecAddress FROMRecipient WHERE RecCity Not IN (Mumbai,Kolkata); Ans: RecName Mahajan S Tripati RecAddressS 116, A Vihar 13, B1 D, Mayur Vihar
(viii) SELECT RecID, RecName FROM Recipient WHERE SenderID = MU02 OR SenderID = ND50; Ans: RecID ND08 ND48 RecName S Mahajan S Tripathi
5.b) Study the following tables FLIGHTS and FARES and write SQL commands for the questions (i) to (iv) and give outputs for SQL quires (v) to(vi). TABLE: FLIGHTS FL_NO IC301 IC799 MC101 IC302 AM812 IC899 AM501 MU499 IC701 TABLE:FLIGHTS FL_NO IC701 MU499 AM501 IC899 IC302 IC799 MC101 AIRLINES INDIAN AIRLINES SAHARA JET AIRWAYS INDIAN AIRLINES INDIAN AIRLINES INDIAN AIRLINES DECCAN AIRLINES FARE 6500 9400 13450 8300 4300 1050 3500 TAX% 10 5 8 4 10 10 4 STARTING MUMBAI BANGALORE INDORE DELHI KANPUR MUMBAI DELHI MUMBAI DELHI ENDING DELHI DELHI MUMBAI MUMBAI BANGLORE KOCHI TRIVENDRUM MADRAS AHMEDABAD NO_ FLGHTS 8 2 3 8 3 1 1 3 4 NO_ STOPS 0 1 0 0 1 4 5 3 0
(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: (Children, Try this as an assignment) (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 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: (Children, Try this answer as an assignment) DELHI 2006: 5.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) : TABLE: DOCTOR ID 101 104 107 114 109 105 117 111 130 TABLE: SALARY ID 101 104 107 114 109 BASIC 12000 23000 32000 12000 42000 ALLOWANCE 1000 2300 4000 5200 1700 CONSULTAION 300 500 500 100 200 NAME Johan Smith George Lara K George Johnson Lucy Bill Murphy DEPT ENT ORTHOPEDIC CARDIOLOGY SKIN MEDICINE ORTHOPEDIC ENT MEDICINE ORTHOPEDIC SEX M M M F F M F F M EXPERIENCE 12 5 10 3 9 10 3 12 15
105 130
18900 21700
1690 2600
300 300
(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 ENTdepartment 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 Doctro,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: 4 (vi) SELECT NAME, DEPT , BASIC from DOCTOR, SALRY Where DEPT = ENT AND DOCTOR.ID = SALARY.ID Ans: Name Jonah Ent Dept Basic 12000
DELHI 2005: (5) Consider the following tables EMPLOYEES and EMPSALARY. write SQL commands for the Statements (i) to (iv) and give outputs for SQL quires (v) to (viii).
EMPID 010 105 152 215 244 300 335 400 441 EMPSALRAY
FIRSTNAME GEORGE MARY SAM SARAH MANILA ROBERT HENRY RACHEL PETER
LASTNAME Smith Jones Tones Ackerman Sengupta Samuel Williams Lee Thompson
ADDRESS 83 First Street 842VineAve 33 Elm st 440 U.S.110 24 FriendsStreet 9 Fifth Cross 12 Moore Street 121 Harrison 11 Red road
CITY Howard Losantiville Paris Upton New Delhi Washington Boston New York Paris
335
40000
10000
Clerk
400
32000
7500
Salesman
441
28000
7500
Salesman
(i) To display Firstname, Lastname, Address and City of all employees living in Paris from the table EMPLOYEES. Ans: Select Firstname,Lastname,Address,City from Employees where City=Paris (ii) To display the content of EMPLOYEES table in descending order of FIRSTNAME. Ans: Select * from Employees Order By Firstname Desc (iii) To display the Firstname, Lastname, and Total Salary of all managers from the tables, where Total Salary is calculated as Salary+Benifts. Ans: Select Firstname,Lastname,Salary+Benefits from Employees, Empsalary where Designation=Manager and Employees.EmpId=EmpSalary.EmpId (iv) To display the Maximum salary among Managers and Clerks from the table EMPSALARY. Ans: Select Designation,max(Salary) from EmpSalary where Designation=Manager or Designation=Clerk v) SELECT FIRSTNAME,SALARY FROM EMPLOYEES,EMPSALARY WHERE DESTINATION =SalesmanAND EMPOLYEES.EMPID=EMPSALARY.EMPID ; Ans: Firstname Rachel Peter Salary 32000 28000
(vi) SELECT COUNT (DISTINT DESIGNATION ) FROM EMPSALARY Ans: 4 (vii) SELECT DESIGNATION , SUM(SALARY) FROM EMPSALARY GROUP BY DESIGNATION HAVING COUNT(*)>2; Ans: Designation Manager Clerk Sum(Salary) 215000 135000
Ans: 32000 OUTSIDE DELHI 2005: 5) Consider the following tables WORKERS and DESIG. Write SQL commands for the statements (i) to (iv) and give outputs for SQL queries (v) to (viii). WORKERS W_ID 102 105 144 210 255 300 335 403 451 DESIG W_ID 102 105 144 210 255 300 335 400 451 SALARY 75000 85000 70000 75000 50000 45000 40000 32000 28000 BENEFITS 15000 25000 15000 12500 12000 10000 10000 7500 7500 DESIGINA TION Manager Director Manager Manager Clerk Clerk Clerk Salesman Salesman FIRSTNAME Sam Sarah Manila George Mary Robert Henry Ronny Pat LASTNAME Tones Ackerman Sengup ta Smith Jones Samuel Williams Lee Thomps on ADDRESS 33 Elm St. 44 U.S.110 24 Friends Street 83 First Street 842 Vine Ave. 9 Fifth Cross 12Moore Street 121 Harrison St. 11 Red Road CITY Paris NewYork New Delhi Howard Losantiville Washington Boston New York Paris
(i) To display W_ID Firstname, address andCity of all employees living in New York fromthe Table WORKERs Ans: select W_ID ,firstname,address,city from workers where city=New York (ii) To display the content of workers table in ascending order of LASTNAME. Ans:Select * from Worker Order By lastname Asc (iii) To display the FIRSTNAME, LASTNAME and Total Salary of all Clerks from the tables WORKERS And DESIG, where Total salary is calculated as Salary + benifts. Ans: Select firstname, lastname, salary+benefits where worker.w_id=desg.w_id and Designation=Clerk (iv) To display the minimum salary among managers and Clerks from the tables DESIG. Ans: (Try This Answer) (v) SELECT FIRSTNAME, SALARY FROM WORKERS, DESIG WHERE DESIGINATION = MANAGER AND WORKERS.W_ID = DESIGN.W_ID Ans: FIRSTNAME Sam Manila George SALARY 75000 70000 75000
(vi)SELECT COUNT(DISTINCT DESIGNATION) FROM DESIGN ; Ans: 4 (vii) SELECT DESIGNATION, SUM(SALARY) FROM DESIG GROUP BY DESIGNATION HAVING COUNT (*) < 3; Ans: Designation Director Salesman Sum(Salary) 85000 60000
(viii) SELECT SUM(BENIFTS) FROM DESIG WHERE DESIGINATION =salesman; Ans: 15000 2004: 5. Give the following table for database a LIBRARY. TABLE : BOOKS BOOK_ID F0001 F0002 T0001 T0002 C001 TABLE:ISSUED BOOK_ID F0001 T0001 QUANTITY_ISSUED 3 1 BOOK_NAME The Tears Thund erbolts My first C+ + C++ Brain works Fast Cook AUTHONAME William Hopkins Anna Roberts Brains & Brooke A.W.Ros saine Lata Kapoore PUBLISHER First Publ First Publ. EPB TDH EPB PRICE 750 700 250 325 350 TYPE Fiction Fiction Text Text Cookery QUANTITY 10 5 10 5 8
C0001 Write SQL queries from b to g. (b)To show Book name, Author name and Price of books of EPB publisher. Ans: select Book_name,Author_name, price from books where Publisher =EPB (c) To list the names of the books of FICTIONS type. Ans: Select Book_name from books where type=FICTION (d) To display the names and prices of the books in descending order of their price. Ans: Select Book_name, price from books order by price desc; (e) To increase the price of all books of First Pub.by 50. Ans: update books set price= price+50 where publishers = First Publ (f) To Display the Book_ID, Book_Name and Quantity Issued for all books Which have been issued. Ans:Select Book_ID, Book_Name, Quantity_Issued from Books,Issued where Books.BookId=Issued.BookId; (g) To insert a new row in the table Issued having the following data: F0002,4 Ans: insert into Issued values(F0002,4) (h) Give the output of the following queries on the above tables: (i) Select Count(Distinct Publishers) From Books Ans: 3 (ii) Select Sum(Price) From Books Where Quantity>5 Ans: 1350. (iii) Select Book_Name,Author_Name From Books Where Price<500.
Ans: Book_Name Author_Name My First C++ Brian & Brooks C++ Brainworks A.W.Rossaine Fast Cook Lata Kapoor. (iv) Select Count(*) From Books. Ans: 5 2003: 5.b Write SQL commands for (b) to (g) and write the outputs for (h) on the basis of tables TNTERIORS and NEWONES. TABLE: INTERIORS NO 1 2 3 4 5 6 7 8 9 10 TABLE:NEWONES NO 11 12 13 ITEMNAME White wood James007 Tom look TYPE Doublebed Sofa Baby cot DATEOFSTOCK 23/03/03 20/02/03 21/02/03 PRICE 20000 15000 7000 DISCOUNT 20 15 10 ITEM NAME Red rose Soft touch Jerryshome Rough wood Comfort zone Jerry look Lion king Royal tiger Park sitting Dine paradise TYPE DoubleBed Baby cot Baby cot Office Table Double Bed Baby cot Office Table Sofa Sofa DinningTable DATEOFSTOCK 23/02/02 20/01/02 19/02/02 01/01/02 12/01/02 24/02/02 20/02/02 22/02/02 13/12/01 19/02/02 PRICE 32000 9000 8500 20000 15000 7000 16000 30000 9000 11000 DISCOUNT 15 10 10 20 20 19 20 25 15 15
(b) To show all information about the sofas from the INTERIORS table. Ans: Select * from INTERIORS where type= sofa. (d) To list ITEMNAME and TYPE of those items, in which DATEOFSTOCK is before 22/01/02 from the INTERIORS table in descending order of ITEMNAME. Ans: Select Itemname,Type From Interiors Where Dateofstock<{22/01/02} order by Itemname (e) To display ITEMNAME and DATEOFSTOCK of those items in which the Discount percentage is more than 15 from INTERIORS. Ans: Select Itemname,Dateofstock from Interiors Where Discount>15 (f) To count the number of items whose type is Double bed; Ans: Select Count(*) from Interiors Where Type=Double Bed (g) To insert new row in the NEWONES table with the following data:14, True Indian , Office Table , {28/03/03},15000,20. Ans: Insert into Newones values(14,True Indian,Office Table,{28/03/03},15000,20). (h) Give the outputs for the following SQL statements. (i) Select COUNT (distinct TYPE) from INTERIORS; Ans: 5 (ii) Select AVG(DISCOUNT)from INTERIORS where TYPE =Baby cot; Ans: 13 (iii) Select SUM(price)from INTERIORS where DATEOFSTOCK<{12/02/02}; Ans: 53000
2002: 5. Given the following Teacher Relation. Write SQL Commands fro (b) to (g) No 1 2 3 4 5 6 7 Name Raja Sangita Ritu Kumar Venka traman Sindhu Aishwarya Department Computer History Sociology Linguistics History Computer Sociology DateofJoining 21/5/98 21/5/97 29/8/98 13/6/96 31/10/99 21/5/86 11/1/1998 Salary 8000 9000 8000 10000 8000 14000 12000 Sex M F F M M M F
(b) To select all the information of teacher in computer department. Ans: Select * from Teacher where Department=Computer (c ) To list the name of female teachers in History Department. Ans: Select Name from Teacher Where Sex=F And Department=History. (d) To list all names of teachers with date of admission in ascending order. Ans: Select Name from Teacher Order By Dateofjoining Asc (e) To display Teacher's Name, Department, and Salary of female teachers Ans: Select Name,Department,Salary from Teacher Where Sex=F (f)To count the number of items whose salary is less than 10000 Ans: Select Count(*) from Teacher Where Salary<10000. (g) To insert a new record in the Teacher table with the following data: 8,Mersha,Computer, (1/1/2000),12000,M.Ans: Insert into Teacher values ,Mersha, Computer,{1/1/2000),12000,M); 2001: 5.b) Write the SQL commands for (i) to (vii) on the basis of the table SPORTS TABLE: SPORTS Studno 10 11 12 13 14 15 Class 7 8 7 7 9 10 Name Smeer Sujit Kamala Veena Archana Arpit Game1 Criket Tennis Swimming Tennis Basketball Cricket Grade1 B A B C A A Game2 Swimming Skating Football Tennis Cricket Athletics Grade2 A C B A A C
(i) Display the names of the students who have grade C in either Game1 or Game2 or both. Ans: Select Name From Sports Where Grade1=C OR Grade2=C (ii) Display the number of students getting grade A in Cricket. Ans: Select Count(*) from Sports Where (Game1=Cricket and Grade1=A) or (Game2=Cricket and Grade2=A) (iii) Display the names of the students who have same game for both game1 and game2 Ans: Select Name From Sports Where Game1=Game2 (iv) Display the games taken up by the students, whose name starts with A. Ans: Select Game1,Game2 From Sports Where Name Like A% (v) Add a new column named marks. Ans: Alter Table Sports Add Marks Number(5); (vi) Assign a value 200 for marks for all those who are getting grade B or A in both Game1 and Game2. Ans: (Children, Try This Answer as an assignment) (vii) Arrange the whole table in the alphabetical order of name. Ans: Select * from Sports Order By Name 2000 : 5. Write SQL commands for the (b) to (e) and write the outputs for (g) on thse basis of table CLUB. TABLE: CLUB COACH ID 1 2 3 4 5 6 COACH NAME KUKREJA RAVINA KARAN TARUN ZUBIN KETAKI AGE 35 34 34 33 36 36 SPORTS KARATE KARATE SQUASH BASKET BAL SWIMMING SWIMMING DATE OF APP 27/03/96 20/01/98 19/01/98 01/01/98 12/01/98 24/02/98 PAY 1000 1200 2000 1500 750 800 SEX M F M M M F
7 8 9 10
39 37 41 37
F F M M
(b) To show all information about the swimming coaches in the club. Ans: Select * from Club (c) To list names of all coaches with their date of appointment (DATOFAPP) in descending order. Ans: Select Coachname from Club order by Dataofapp desc (d) To display a report, showing coachname, pay, age and bonus(15% of pay) for all coaches. Ans: Select Coachname,Pay,Age,Pay*0.15 from Club (e) To insert a new row in the CLUB table with following data: 11,PRAKASH,37,SQUASH, {25/02/98},2500,M Ans: Insert into Club Values (11,PRAKASH,37,SQUASH,{25/02/98}, 2500,M) (f) Give the output of the following SQL statements: (i) select COUNT (distinct SPORTS)from CLUB; Ans: 4 (ii) select MIN(AGE) from CLUB where SEX =F; Ans: 34 (iii) select AVG(PAY) fromCLUB where SPORTS = KARATE; Ans: 1100 (iv) select SUM(PAY) from CLUB where DATAOFAPP>{31/01/98}; Ans: 7800 G) Assuming that there is one more table COACHES in the database as shown below: TABLE:COACHES SPORTS PERSON AJAY 1 SEEMA VINOD TANEJA SEX M F M F COACH_NO 1 2 1 3
What will be the output of the following query: SELECT SPORTS PERSON, COACHNAME FROM CLUB,COACHES WHERE COACH_ID=COACH_NO Ans: SPORTS PERSON AJAY 1 SEEMA VINOD TANEJA 1999: 5.) Given the following Teacher relation: Write SQL commands for questions (b) to (g). TEACHER NO 1 2 3 4 5 6 7 NAME RAJA SANGITA RITU KUMAR VENKAT SINDU ASHWARYA DEPARTMENT COMPUTER History MATHS HISTORY MATHS HISTORY MATHS DATEOFJOING 21/5/98 21/5/97 29/8/98 13/6/96 31/10/99 21/5/86 11/1/98 SALARY 8000 9000 8000 10000 8000 14000 12000 SEX M F F M M F F COACHNAME KUKREJA RAVINA KUKREJA KARAN
(b) To show all information about the teachers of history department. Ans:select *from teacher where department=history; (c) To list names of female teacher who are in math department. Ans: select name from teacher where sex=male and department= maths; d) To list names of all teacher with their date of joining in ascending order. Ans:Select Name From Teacher order by dateofjoing; (f) To count the number of teachers with age >23. Ans: Select count(number of teachers) from ,teacher where age>23; (g) To insert a new row in the teacher table with the following data: 9, raja, 26,computer, {13/5/95 }, 2300, M. Ans: Insert into Teacher values(9,raja,26, computer, {13/05/95},2300,M);
1998: 5. Write SQL commands for (b) to (g) and write the outputs for (h) on the basis of table HOSPITAL. NO 1 2 3 4 5 6 7 8 9 10 NAME Arpit Zareena Kareem Arun Zubin Karin Ankita Zareen Kush Shilpa AGE 62 22 32 12 30 16 29 45 19 23 DEPARTMENT Surgery Ent Arthopedic Surgery Ent Ent cardiology Gynecology Cardiology Nuclear medicine DATEOFADM 21/1/98 12/12/97 19/2/98 11/1/98 12/1/98 24/2/98 22/2/98 22/2/98 13/1/98 21/2/98 CHARGES 300 250 200 300 250 250 800 300 800 400 SEX M F M M M F F F M F
(b) To select all the information of patients of all cardiology department. Ans: Select all from Hospital where department=Cardiology (c) To list the names of female patients who are in ent department. Ans:select name from Hospital where Department=Ent and Sex=F (d) To list names of all patients with their date of admission in ascending order. Ans: Select name,dateofadm from Hospital dateofadm. (e) To display patients name, charges, age, for only female patients. Ans: Select Name,Charges,age from Hospital where sex=F (f) To count the number of patients with age <30. Ans: Select count(*) from hospitals where age<30 (g) To insert the new row in the hospital table with the following data: 11, aftab, 24, surgery, {25/2/98}, 300, M. Ans: insert into Hospital values(11, aftab, 24, surgery, {25/02/98}, 300, M) (h) Give the output of the following SQL statements: (i) Select count (distinct charges)from hospital; Ans: 5 (ii) Select min(age) from hospital where sex = f; Ans: 16 (iii) Select sum(charges) from hospital where department = ent; Ans: 750 (iv) Select avg(charges) from hospital where date of admission is <{12/02/98}; Ans: 380