Final-MySQL - Revision
Final-MySQL - Revision
CLASS: XII
SUB: COMPUTER SCIENCE(083)
MySQL
SQL(15 marks)
• Do not forget to put semicolon at the end of each SQL Query.
• While giving outputs, do write the column headers mentioned in SELECT command.
• Do not forget to put character and date values in quotes( Both quotes are acceptable ‘’
and “ “)
• Date format is ‘YYYY-MM-DD’ or ‘YYYY/MM/DD’
• 0 means value and NULL means no value (both are different)
• Is null and is not null used to check for blank entries or any value respectively.
• Do not get confuse between substr and instr.
(Substr gives substring and instr gives position of the first occurrence of the
substring)
• While giving length of string, do count all the spaces leading, trailing and middle
( space is also counted as character)
• Now() and curdate() does not accept any parameters, rest all methods do accept one or
two
parameters.
• Single row gives single output for single row and multiple rows gives single output for
multiple rows
• Count(*) gives total rows including null values but count(col) gives only non-null values
• Year(), month(), day() gives integer value, Like year(dob)=2002 where dob is date
value.
Section - A
Each question carries 1 mark.
1. Expand the following: DDL , DML
2. Name 4 Data types used to store Numeric data in Databases.
3. What are SQL Joins?
4. Fill in the blank:
command is used to add a new column in a table in SQL.
a) update b) remove c) alter d)drop
Write syntax.
5. Which of the following commands can remove all the data from a table in aMYSQL database?
Write complete command.
a) DELETE b) DROP c) REMOVE d) ALTER
6. Fill in the blank:
A candidate key, which is not the primary key of a table, is a/an .
a) Primary Key b) Foreign Key c) Candidate Key d) Alternate Key
7. Select the correct statement, with reference to SQL:
a) Aggregate functions ignore NULL
b) Aggregate functions consider NULL as zero or False
c) Aggregate functions treat NULL as a blank string.
d) NULL can be written as 'NULL' also.
8. Which function is used to display the total number of data values (exceptNULL) from a
[2]
column in a table?
a) sum() b) total() c) count() d) IS NOT NULL
9. A table, Students has been created in a database with thefollowing fields:
StudentID, NAME, DOB, CITY, MARKS
10.Give the SQL command to change the datatype of CITY fromVarchar(30) to Char(30).
11. Which of the following is a DML/DDL command ? Categorize it/
SELECT…FROM, CREATE TABLE, INSERT, UPDATE
12. Given the following table Structure, write create command :
Table Name: Saragam
Field Name Data Type Data Limit Constraint
Albumid Numeric 4 PRIMARY
Album Text 20 NOT NULL
Cost numeric 3 DEFAULT 0
Quantity Numeric 3 Greater than 0
Singer Text 10 ___________
13. Differentiate between char and varchar data type. Give example.
14. The LastName column of a table "Directory" is given below:
Based on this information, find the output of the following
queries:
a) SELECT lastname FROM Directory WHERE lastname like "_a%";
15. Pooja, a students of class XI, created a table "Book". Price is a column of this table. To find the
details of books whose prices have not been entered she wrote the following query:
Select * from Book where Price = NULL;
The query shows error, rectify it.
Section - B
Each question carries 2 marks.
1. A resultset is extracted from the Customer table using the cursorobject (that has been already
created) by giving the following statement.
Rec_Data=cursor.fetchall()
(a) How many records of the table will be returned by fetchall()method?
(b) What will be the datatype of Rec_Data object after thegiven command is executed?
[3]
2. Write output of the queries (i) to (iv) based on table Carmarket.
3. Write the output of the queries (a) to (d) based on the tableSportstars, given below:
[6]
I) SELECT TID, TNMAE AS “TRAINER NAME” FROM TRAINER
WHERE CITY NOT IN
(‘DELHI’, ‘MUMBAI’);
II) SELECT CITY ,COUNT(*) FROM TRAINER GROUP BY CITY
HAVING COUNT(*)>=2;
III) SELECT(HIREDATE), MAX(SALARY), MIN (CITY) FROM
TRAINER;
IV) SELECT COUNT(DISTINCT CITY) FROM TRAINER;
14. In a database there are two tables ‘cd’ and type as shown below:
Table: CD Table: TYPE
Code Title Duration Singer category
101 Sufi songs 50 min Zaki faiz 12 Category description
102 Eureka 45 min Shyama 12
mukherjee 1 Jazz
12 Classical
103 Nagmey 23 min Sonvi kumar 77 40 Country side
104 dosti 35 min Bobby 1
78 Pop
i) Identify the primary key and the foreign key in the CD table.
ii) Check every value in CATEGORY column of both the tables. Do you find any discrepancy?
State the discrepancy.
15. Consider the table Projects given below. Write commands in SOL for i) and ii)
Section – C
Each question carries 3 marks.
3. Consider the table RESULT given below. Write commands in MySql for (i) to (ii)
(i) To list the names of those students, who have obtained Division as FIRST in the 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) What is used in the SELECT clause to return all the columns in the table?
4. (a) Write the outputs of the SQL queries (i) to (iv) based on the relationsTeacher and
Placement given below:
Table : Teacher
T_ID Name Age Department Date_of_join Salary Gender
1 Arunan 34 Computer Sc 2019-01-10 12000 M
2 Saman 31 History 2017-03-24 20000 F
3 Randeep 32 Mathematics 2020-12-12 30000 M
4 Samira 35 History 2018-07-01 40000 F
5 Raman 42 Mathematics 2021-09-05 25000 M
6 Shyam 50 History 2019-06-27 30000 M
7 Shiv 44 Computer Sc 2019-02-25 21000 M
8 Shalakha 33 Mathematics 2018-07-31 20000 F
Table : Placement
P_ID Department Place
1 History Ahmedabad
2 Mathematics Jaipur
3 Computer Sc Nagpur
SECTION E
Each question carry 5 marks.
1. Write queries (a) to (e) based on the tables Patient and Department given below:
Table: PATIENT
PCODE TESTID NAME PHONE DTADMIT
6473 T102 Amit Sharma 912356899 19-Jun-17
7134 T101 Rose Mathew 886744536 12-Nov-17
8786 T102 Tina Sharma Arora 889088765 06-Dec-17
6477 T502 Vijay Shah 714567445 07-Dec-17
7658 T101 Venkat Fazal 865545343 31-Dec-17
Table: TEST
TESTID TESTNAME COST
T101 Platelet Count 200
T102 Hemogram 190
T301 Malaria Detection 350
T502 Glucose Test 150
2. Consider the following “employee” table and write the queries of the following :
EmpNo Name Job Gender Salary DeptNo Commission
101 Reena President F 30000 10 300
102 Shankar Manager M 18000 10 400
103 Meena Manager F 15000 20 300
104 Payal Clerk F 5000 30 100
105 Rony Manager M 23000 20 500
(i) Display the name of those employees whose name contains ‘ee’.
(ii) Display the name, salary and deptno of those employees whose salary is in the range
10000 and 30000. Order the query output in ascending order of name.
(iii) Display all the details of Female Manager and President.
(iv) Double the commission of those employees working in deptno 20 and 30.
(v) Give output of:
Select name, gender from employee where commission >400;
3. A School in Delhi uses database management system to store student details. The school
maintains a database 'school_record' under which there are two tables.
Student Table : Maintains general details about every student enrolled in school.
StuLibrary Table : To store details of issued books. BookID is the unique identification number
issued to each book. Minimum issue duration of a book is one Day.
i Identify the SQL Query which displays the data of StuLibrary table in ascending order of
Student-ID.
i) Select * from StuLibrary Order By BookID
ii) Select * from StuLibrary Order By StuID
iii) Select * from StuLibrary Order By StuID ASC
[10
]
iv) Select * from StuLibrary Order By StuID DESC
Choose the correct option:
a. Both Query i) and iv) will display the desired data.
b. Both Query i) and ii) will display the desired data.
c. Both Query iii) and iv) will display the desired data.
d. Both Query ii) and iii) will display the desired data.
iii. Which of the following SQL Query will fetch ID of those issued books which have not been
returned?
a. SELECT BookID from StuLibrary where BookID is NULL;
b. SELECT BookID from StuLibrary where StuID is NULL;
c. SELECT BookID from StuLibrary where Issued_date is NULL;
d. SELECT BookID from StuLibrary where Return_date is NULL;
v. Which of the following SQL Query will display dates on which number of issued books is
greater than 5?
a. SELECT Issued_date from StuLibrary GROUP BY Issued_date where COUNT(*)>5;
b. SELECT Issued_date from StuLibrary GROUP BY Return_date having count(*)>5
c. SELECT Issued_date from StuLibrary GROUP BY Issued_date having count(*)>5
d. SELECT Issued_date from StuLibrary GROUP BY Return_date where COUNT(*)>5
Solutions:
I. d) Both Query ii) and iii) will display the desired data.
II. c) BookID,Issued_date
III. d) SELECT BookID from StuLibrary where Return_date is NULL;
IV. c) StuAadhar
V. c) SELECT Issued_date from StuLibrary GROUP BY Issued_date having count(*)>5
4. Tejasvi Sethi, a car dealer has stored the details of all cars in her showroom in a table called
CARMARKET.
[11
]
The table CARMARKET has attributes CARCODE which is a primary key, CARNAME,
COMPANY, COLOR, COST (in lakh rupees) of the car and DOM which is the Date of
Manufacture of the car.
Answer all questions based on the table CARMARKET from the below mentioned questions.
Table:
CARMARKE
T CARNAM COMPAN COLOR COST DOM
CARCODE E Y
C01 BALENO SUZUKI BLUE 5.90 2019-11-07
CO2 INDIGO TATA SILVER 12.90 2020-10-15
C03 GLC MERCEDE WHITE 62.38 2020-01-20
S
C04 A6 AUDI RED 58.55 2018-12-29
i. Display the carname along with the charges rounded off to 1 digit after decimal
place.
a. Select carname,round(cost) from carmarket;
b. Select carname,round.cost(1) from carmarket;
c. Select carname,round.cost() from carmarket;
d. Select carname, round(cost,1) from carmarket;
CORRECT ANSWER – d
ii. Display the carname, color and position of the character ‘E’ in the color of all the cars.
a. select carname,color from carmarket where color like “%E%”;
b. select carname,color,instr(color,'E') from carmarket;
c. select carname,color from carmarket where color = “%E%”;
d. select carname,color,substr(color,1,’E’) from carmarket;
CORRECT ANSWER – b
iii. Display the carname ,name of the company in lower case of all cars whose year (of dom) is
2020.
a. select carname,lcase(company) from carmarket where year(dom) = 2020;
b. select carname,lcase(company) from carmarket where yearof(dom) like '2020%';
c. select carname,lower(company) from carmarket where dom from '2020-01-01' to
'2020-12-31';
d. select carname,lower(company) from carmarket where yearfrom(dom) = 2020;
CORRECT ANSWER - a
[12
]
CORRECT ANSWER - b
SECTION D
Each question carry 4 marks :
1.Consider the following ‘Student’ table. 4
(i) What will be the most suitable datatype for the grade column and why?
(ii) Write a command to insert Suman’s record with the data as shown in the table.
(iii) show details of “IP” students.
(iv) List rollno, name , grade of students having “U” anywhere in their name.
2. A Departmental store ‘Iconic’ is planning to automate its system so that they can store all the
records on computer. They contacted a Software Company to make the software for the same.
The company suggested that there is need of a front end and back-end software. The major
challenge was to keep the record of all the items available in the store. To overcome the problem,
the software company has shown the glimpses of the database and table required to resolve their
problem:
Database Name: Iconic
Table Name: Garment
Attributes of the table: Gcode – Numeric, Gname – Character 25, Size - Character 5, Colour –
Character 10, Price – Numeric
Consider the following records in ‘Garment’ table and answer the given questions:
Table:
GARMENT
GCODE GNAME SIZE COLOUR PRICE
111 Tshirt XL Red 1400.00
112 Jeans L Blue 1600.00
113 Skirt M Black 1100.00
[13
]
114 Jacket XL Blue 4000.00
115 Trousers L Brown 1500.00
116 LadiesTop L Pink 1200.00
c.
GNAME PRICE
Jacket 4000.00
Jeans 1600.00
Trousers 1500.00
Tshirt 1400.00
LadiesTop 1200.00
Skirt 1100.00
d.
GNAME PRICE
Jacket 4000.00
[14
]
Jeans 1600.00
LadiesTop 1200.00
Skirt 1100.00
Trousers 1500.00
Tshirt 1400.00
3. Consider the table TEACHER given below. Write commands in SQL for (1) to (3) and output for (4)
[15
]