SQL
SQL
Consider the following table named SBOP with details of account holders. Write commands of MySql for (i) to (iv)
and output for (v) to (vii).
TABLE SBOP
(i) To display Accountno, Name and DateOfopen of account holders having transactions more than 8.
(ii) To display all information of account holders whose transaction value is not mentioned.
(iii) To add another column Address with datatype and size as VARCHAR(25).
(iv) To display the month day with reference to DateOfopen for all the account holders. (Delhi2014)
(v) SELECT COUNT (*) FROM SBOP;
(vi) SELECT Name, Balance FROM SBOP WHERE Name LIKE “%i”;
(vii) SELECT ROUND (Balance,-3) FROM SBOP WHERE Accountno="SB-5” ;
Answer:
(i) SELECT Account no, Name, DateOfopen FROM SBOP
WHERE Transaction > 8:
(ii) SELECT * FROM SBOP
WHERE Transaction IS NULL;
(iii) ALTER TABLE SBOP
ADD Address VARCHAR(25);
(iv) SELECT DAY0FM0NTH(DateOfopen), Name
FROM SBOP:
Question 2.
Consider the following table named EXAM with details of marks. Write command of MySQL for (i) to (iv) and
output for (v) to (vii).
Table EXAM
Page 1 of 10
(i) To display all information of the students of humanities in descending order of percentage.
(ii) To display Adno, Name, Percentage and Stream of those students whose name is less than 6 characters long.
(iii) To add another column Bus_Fees with datatype and size as Decimal(8,2).
(iv) To increase percentage by 2% of all the Humanities students. (All India 2014)
(v) SELECT COUNT(*) FROM EXAM;
(vi) SELECT SName, Percentage FROM EXAM WHERE Name LIKE “N%”;
(vii) SELECT ROUND (Percentage ,0) FROM EXAM WHERE Adno=“R005”;
Answer:
(i) SELECT * FROM EXAM WHERE Stream = „Humanities‟ ORDER BY Percentage DESC;
(ii) SELECT Adno, SName, Percentage, Stream FROM EXAM WHERE LENGTH(SName)<6;
(iii) ALTER TABLE EXAM ADD Bus_Fees DECIMALS, 2) ;
(iv) UPDATE EXAM SET Percentage = Percentage + 2 WHERE Stream = „Humanities‟;
(vi) The given query will result in an error as there is no column named Name in table EXAM.
Question 3.
Consider the table SUPPLIER given below. Write commands in MySQL for (i) to (iv) and output for (v) to (vii). (i)
TABLE SUPPLIER
Answer:
(i) SELECT Pname FROM SUPPLIER WHERE Pname LIKE „B%‟ ORDER BY Price;
(ii) SELECT Scode, Pname, City FROM SUPPLIER WHERE Qty < 150;
(iii) SELECT COUNT(DISTINCT City) FROM SUPPLIER;
(iv) INSERT INTO SUPPLIER VALUES (110,„Bournvita‟, „ABC‟, 170, „Delhi‟, 40.00);
Page 2 of 10
Question 4.
Consider the table SHOPEE given below. Write commands in MySQL for (i) to (iv) and output for (v) to (vii).
TABLE SHOPEE
(i) To display names of the product, whose name starts with „C‟ in ascending order of Price.
(ii) To display code, product name and City of the products whose quantity is less than 100.
(iii) To count distinct Company in the table.
(iv) To insert a new row in the table SHOPEE.
110, „Pizza‟, „Papa Jones‟, 120, „Kolkata‟, 50.00 (All India 2012)
(v) SELECT Pname FROM SHOPEE WHERE Pname IN („Jam‟, „Coffee‟);
(vi) SELECT COUNT(DISTINCT City) FROM SHOPEE;
(vii) SELECT MAX(Qty) FROM SHOPEE WHERE City = „Mumbai‟
Answer:
(i) SELECT Pname FROM SH0PEE WHERE Pname LIKE „C%‟ ORDER BY Price;
(ii) SELECT Scode, Pname, City FROM SHOPEE WHERE Qty < 100;
(iii) SELECT COUNT (DISTINCT Surname) FROM SHOPEE;
(iv) INSERT INTO SHOPEE VALUES (110, „ Pi zza‟, „ Papa Jones ‟ , 120, „Kolkata‟, 50.00);
Question 5.
Consider the table PERSONS given below. Write commands in SQL for (i) to (iv) and write output for
(v) to (viii).
TABLE PERSONS
Page 3 of 10
(i) Display the SurName, FirstName and City of people residing in Udhamwara city.
(ii) Display the Person IDs (PID), Cities and Pincode of persons in descending order of Pincode.
(iii) Display the FirstName and City of all the females getting Basic salaries above 40000.
(iv) Display FirstName and BasicSalaries of all the persons whose first name start with „G‟. (Delhi 2012c)
(v) SELECT SurName FROM PERSONS WHERE BasicSalary>= 50000;
(vi) SELECT SUM (BasicSalary) FROM PERSONS WHERE Gender = „F‟;
(vii) SELECT Gender, MIN (BasicSalary) FROM PERSONS GROUP BY Gender;
(viii) SELECT Gender, COUNT (*) FROM PERSONS
GROUP BY Gender;
Answer:
(i) SELECT SurName, FirstName, City FROM PERSONS WHERE City = „Udhamwara‟;
(ii) SELECT PID, City. PinCode FROM PERSONS ORDER BY Pincode DESC;
(iii) SELECT FirstName, City FROM PERSONS WHERE Gender = „F‟ AND BasicSalary > 40000;
(iv) SELECT FirstName, BasicSalary FROM PERSONS WHERE FirstName LIKE „G%‟;
Question 6.
Consider the table EXAM given below. Write commands in MySQL for (i) to (iv) and output for (v) to (vii).
TABLE EXAM
(i) To list the Names of those students, who have obtained Division as FIRST in ascending order of Name.
(ii) To display a report listing Name, Subject and Annual Stipend received assuming that the Stipend column has
monthly Stipend.
(iii) To count the number of students, who have either Accounts or Informatics as Subject.
(iv) To insert a new row in the table EXAM.
6, „Mohan‟, 500, „English‟, 73, „SECOND‟ (HOTS; Delhi 2011)
(v) SELECT AVG(Stipend) FROM EXAM WHERE Division = „THIRD‟;
(vi) SELECT COUNT (DISTINCT Subject) FROM EXAM;
(vii) SELECT MIN(Average) FROM EXAM WHERE Subject = „English‟;
Page 4 of 10
Answer:
(i) SELECT Name FROM EXAM WHERE Division=„FIRST‟ ORDER BY Name;
(ii) SELECT Name, Subject, Sti pend*12 FROM EXAM;
(iii) SELECT COUNT(*) FROM EXAM WHERE Subject =„Accounts‟ OR Subject = „Informatics‟;
(iv) INSERT INTO EXAM VALUES (6, „Mohan‟,500, „English‟, 73, „SECOND‟);
Question 7.
Consider the table DOCTOR given below. Write commands in SQL for (i) to (iv) and output for (v) to (vii).
TABLE DOCTOR
(i) Display the names and dates of joining of doctors of Oncology department.
(ii) Display the names and salaries of doctors in descending order of salaries.
(iii) Display the names and salaries of all the female‟s doctors who are getting salary above $ 50000.
(iv) Display names of each department along with total salary being given to doctors of that department. (Delhi 2011)
(v) SELECT Department FROM DOCTOR WHERE Salary >= 55000;
(vi) SELECT SUM (Salary) FROM DOCTOR WHERE Department = „Surgery‟;
(vii) SELECT Doc_Name FROM DOCTOR WHERE Doc_Name LIKE ‟J ‟%;
Answer:
(i) SELECT Doc_Name,Date_0f_join FROM DOCTOR WHERE Department =„Oncology‟;
(ii) SELECT Doc_Name, Salary FROM DOCTOR ORDER BY Salary DESC;
(iii) SELECT Doc_Name, Salary FROM DOCTOR WHERE Gender = „F‟AND Salary > 50000;
(iv) SELECT Department, SUM(Salary) FROM DOCTOR GROUP BY Department;
Page 5 of 10
Question 8.
Consider the table LIBRARY given below. Write commands in MySQL for (i) to (iv) and output for (v) to (vii).
TABLE LIBRARY
Answer:
(i) SELECT BookTitle FROM LIBRARY WHERE Type = „FND‟;
(ii) SELECT BookTitle, Type, Price FROM LIBRARY ORDER BY Price DESC;
(iii) SELECT COUNT(*) FROM LIBRARY WHERE Type = 'FND';
(iv) INSERT INTO LIBRARY VALUES (7, „Windows 8 Basics', „ FND‟ , „McGraw‟,7,150);
Question 9.
Consider the table STUDENT given below. Write commands in MySQL for (i) to (iv) and output for (v) to (vii).
(HOTS)
TABLE STUDENT
Question 10.
Consider the table PRODUCT given below. Write commands in MySQL for (i) to (iv) and output for (v) to (vii).
TABLE PRODUCT
Answer:
(i) SELECT ProductName FROM PRODUCT WHERE BrandName = „Maharaja‟;
(ii) SELECT ProductName, BrandName, Pri ce FROM PRODUCT ORDER BY Price DESC;
(iii) SELECT COUNT (*) FROM PRODUCT;
(iv) INSERT INTO PRODUCT VALUES (6,„Gas Stove‟, „Maharaja‟,7, 6150);
Page 7 of 10
Question 11.
Consider the table SPORTS given below. Write commands in MySQL for (i) to (iv) and output for (v) to (vii).
TABLE SPORTS
Answer:
(i) SELECT Name FROM SPORTS WHERE Game = „Cricket‟;
(ii) SELECT Name, Game,Grade . FROM SPORTS ORDER BY Name;
(iii) SELECT COUNT (*) FROM SPORTS;
(iv) INSERT INTO SPORTS VALUES (7, „Rishabh‟, 9, „Cricket‟, „A‟);
Question 12.
Consider the table INTERIORS given below. Write commands in MySQL for (i) to (iv) and output for (v) to (vii).
TABLE INTERIORS
Page 8 of 10
(i) To list the ItemName whose Type is Office Table.
(ii) To display a report listing ItemName, Type and Price in descending order of Price.
(iii) To count the number of ItemName.
(iv) To insert a new row in the table INTERIORS.
7, „Comfort Zone‟,‟Double Bed‟,‟23/03/2011′, 15000
(v) SELECT ItemName, Type FROM INTERIORS WHERE ItemName LIKE „R%‟;
(vi) SELECT COUNT (DISTINCT Type) FROM INTERIORS;
(vii) SELECT ItemName, Type FROM INTERIORS WHERE DateofStock>„15/ll/2010‟;
Answer:
(i) SELECT ItemName FROM INTERIORS WHERE Type = „Office Table‟;
(ii) SELECT ItemName, Type, Price FROM INTERIORS ORDER BY Price DESC;
(iii) SELECT COUNT(*) FROM INTERIORS;
(iv) INSERT INTO INTERIORS VALUES (7, „Comfort Zone‟, „Double Bed‟,„23/03/2011‟,15000);
Question 13.
Consider the table STUDENTS given below. Write commands in MySQL for (i) to (iv) and output for (v) to (vii).
TABLE STUDENTS
Answer:
(i) SELECT StudentName FROM STUDENTS WHERE Class = IX‟ ;
(ii) SELECT StudentName, Class, Score FROM STUDENTS ORDER BY Score DESC;
(iii) SELECT COUNT(* ) FROM STUDENTS;
(iv) INSERT INTO STUDENTS VALUES (7, „Divya Prakash‟, „Science‟, 355, „C‟);
Page 9 of 10
Question 14.
Consider the following table FITNESS with details about fitness products being sold in the store. Write command of
SQL for (i) to (iv) and output for (v). (HOTS)
TABLE FITNESS
(i) To display the names of all the products with price more than 20000.
(ii) To display the names of all products by the manufacturer „Aone‟.
(iii) To change the price data of all the products by applying 25% discount reduction.
(iv) To add a new row for product with the details: „P7‟, „Vibro Exerciser‟, 28000, „Aone‟.
(v) SELECT * FROM FITNESS WHERE MANUFACTURER LIKE ‟%e‟;
Answer:
(i) SELECT PNAME FROM FITNESS WHERE PRICE > 20000;
(ii) SELECT PNAME FROM FITNESS WHERE MANUFACTURERS „Aone‟;
(iii) UPDATE FITNESS SET PRICE=PRICE-PRICE* 25/100;
(iv) INSERT INTO FITNESS VALUES („P7‟, „Vibro Exerciser‟, 28000, „Aone‟);
Page 10 of 10