Final-MySQL - Revision

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Revision for CBSE Board Exam(2022-23)

CLASS: XII
SUB: COMPUTER SCIENCE(083)
MySQL

Tips for Computer Science Board Exam

• Read and understand the concern of question carefully.


• Write correct Section and Serial number against the attempted question.
• Write your answers in presentable form with proper example and output of each
SQLcommand.
• Check your paper before submitting that you have attempted all answers and
review it
carefully.

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)

• Do remember the syntax of SELECT command:


• SELECT colnames FROM tablename WHERE condition Order by colname
asc/desc
• Where clause can contain single row methods not multiple row
• Where clause can be used by group by but not after it

• SELECT colnames FROM tablename WHERE condition GROUP BY colname


• SELECT colnames FROM tablename WHERE condition GROUP BY colname
ORDER BY colname
• SELECT colnames FROM tablename WHERE condition
GROUP BY colname HAVING condition
• SELECT colnames FROM tablename WHERE condition
GROUP BY colname HAVING condition ORDER BY colname
• Identification of group by clause: - as per , according to , wise, by each
• Having clause can be used after group by but not before it.
• Having can contain only the column given in group by or any aggregate method

• For multiple ordering:


[1]
• SELECT colnames FROM tablename WHERE condition Order by colname1
asc/desc,colname2 asc/desc;

• Increase and decrease any value- use update command


• Only display in calculation not permanent change- select expression as ‘alias’

• 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%";

b)SELECT lastname FROM Directory WHERE lastname not 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.

16. Suggest a free software for managing the database


a. Oracle
b. MySQL
c. Sybase
d. Microsoft SQL Server
Answer: b

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.

(i) SELECT Company, Cost, Carname


FROM Carmarket
WHERE Cost BETWEEN 8 AND 15 ORDER BY Company, Cost DESC;
(ii) SELECT Company, Carname, DOM
FROM Carmarket
WHERE DOM=(SELECT MIN(DOM) FROM Carmarket);
(iii) SELECT COUNT( DISTINCT Company)
FROM Carmarket;
(iv) SELECT Carname ,Cost*3 'Total Cost', MIN(DOM)
FROM Carmarket WHERE Carname LIKE '%go';

3. Write the output of the queries (a) to (d) based on the tableSportstars, given below:

a) SELECT COUNT(*) FROM Sportstars WHERE Medals>=4;


b) SELECT SUM(Medals) FROM Sportstars WHEREClass=12;
c) SELECT MAX(DOB) FROM Sportstars;
d) SELECT DOB FROM Sportstars WHERE Admn_No LIKE"SM%”

4. a)Write short note with example on GROUP BY command.


b) Assume the two tables Saleman and Item are related with foreign key constraint.
Table: Salesman
Scode ItemCod Sname Address Dojoin Sales Area
e
100 T101 Amit Delhi 29-09-2017 5000.9 East
[4]
101 T102 Sushant Gurgaon 01-01-2018 8000.62 East
102 T101 Priya Noida 01-01-2018 3450.45 West
103 T103 Mohit Delhi 03-11-2018 6000.5 North
104 T102 Priyanshi Delhi 15-12-2019 8000.62 North
Table: Item
ItemCode ItemName Price
T101 Flair Gelpen 1.1 10
T102 Apsara Pencil 3.2 7
T103 Camelin Box 35
T104 Camelin Ink Red 32
a) Can a record in table Salesman be assigned T110 as its ItemCode. Justify your answer.
b) Which fields are the candidate key in the table “Salesman” ? Justify your answer.

5. a) Do the following queries: DESIG


W_ID SALARY BENEFITS DESIGNATION
102 75000 15000 MANAGER
105 85000 25000 DIRECTOR
144 70000 15000 MANAGER
210 75000 12500 MANAGER
255 50000 12000 CLERK
300 45000 10000 CLERK
335 40000 10000 CLERK
403 32000 7500 SALESMAN
451 28100 7500 SALESMAN
a) Create the table DESIG , with suitable constraints according to the values given in the
table.
b) Increase the BENEFITS of all Salesmen by 10% in table DESIG.
6. Consider the Table Customers given below:

(a) Identify the degree and cardinality of the table.


[5]
Which field should be made the primary key? Justify youranswer.
7. Write the output of the queries (a) to (d) based on the tableSMARTWATCH, given below:
Table: SMARTWATCH

a) SELECT * FROM SMARTWATCH WHERE PRICE>=21000 ANDQTY_STORE>100;

b) SELECT SUM(QTY_STORE) FROM SMARTWATCH WHERE NAME IN ("Fitbit


Versa", "Fossil Hybrid");
c) SELECT MAX(PRICE)+ MIN(PRICE) FROM SMARTWATCH;
d) SELECT AVG(PRICE) FROM SMARTWATCH;

8. Explain the use of ‘Foreign Key’ in a Relational Database.


Give an example tosupport your answer.
9. Differentiate between alter and update command with example for both.

10. Consider the following two commands with reference to a table,


named Students, having a column named Section:
(a) Select count(Section) from Students;
(b) Select count(*) from Students;
If these two commands are producing different results,
(i) What may be the possible reason?
(ii) Which command, (a) or (b), might be giving higher value?
11. Name the aggregate functions which work only with numeric data, and
those that work with any type of data.
12. Differentiate between DDL and DML with respect to databases.
13.
Write the output of the queries (a) to (d) based on the table, trainer given below:
Trainer
TID TNAME CITY HIREDATE SALARY
101 SUNAINA MUMBAI 15-10-1998 90000
102 ANAMIKA DELHI 24-12-1994 80000
103 DEEPTI CHANDIGARH 21-12-2001 82000
104 MEENAKSHI DELHI 25-12-2002 78000
105 RICHA MUMBAI 12-01-1996 95000
106 MENIKA CHENNAI 12-12-2001 69000

[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)

i. To display all information about projects of"Medium" ProjSize


ii. To list the ProjSize of projects whose ProjName ends with LITL.

Section – C
Each question carries 3 marks.

1. i) WorkerId is the primary key in a table Worker. Write command to remove


primary key constraint of the table. 1
ii) Differentiate between ALTER and UPDATE commands in sql(with example). 2

2.Consider the following structure of table Product:


Table: Product
Field Name Datatype & size Remarks
P_Id CHAR(4)
Pname VARCHAR(20)
Manufacturer VARCHAR(2)
Price Decimal(8,2) 100 by default
[7]
Write commands to:
i. Create the table Product with above structure and appropriate constraint. 1
ii. Increase size of Manufacturer attribute to 25. 1
iii. Add an attribute Discount of integer data type which can accommodate 3
digits and 2 digits as decimal in it. 1

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

(i) SELECT Department, max(salary) FROM TeacherGROUP BY


Department;
(ii) SELECT MAX(Date_of_Join), MIN(Date_of_Join)
[8]
FROMTeacher;
(iii) SELECT Name, Salary, T.Department, Place FROMTeacher T, Placement P
WHERE T.Department = P.Department AND P.Department='History';
(iv) SELECT Name, Place FROM Teacher natural joinPlacement where
Gender='F';
(b) Write the command to view all the databases in an RDBMS.

5. a) What is primary key?


b) Differentiate between DEFAULT and CHECK constraint of table with example.

6. Answer the question based on the table given below:


TABLE : HOSPITAL
No. Name Age DEPARTMENT DAREOFADD CHARGES SEX
1 ARPIT 62 SURGERY 12/12/2001 2000 M
2 ZAREEN 34 NEURO 11/11/2002 1500 M
3 ARUN 26 ORTHO 02/01/2010 1500 M
4 ANKITA 45 ENT 10/05/2005 450 F
5 KUSH 22 CARDIOLOGY 07/01/2009 6000 M
6 SHILPA 39 ENT 12/04/2006 450 F
• To list the names all the patients admitted after 15/01/98.
• To list the names of female patients who are in ENT department.
• To list names of all patients with their date of admission in ascending order.

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

a) Write command to create table Patient with proper constraints.


[9]
b)To display Names of Patients, TESTID and Test names for those Patients who were
admitted between '01-DEC-2017' and '15-DEC-2017' (both dates inclusive).
c) To display Names of Patients, Test names and Cost of Test for those Patients who have
"ma" in their name.
d) To increase the cost of all tests by 100.00
e) Display the number of patients by test wise.

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.

ii. The Primary Key for StuLibrary Table is/are …….


a. BookID
b. BookID,StuID
c. BookID,Issued_date
d.Issued_date

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;

iv. The Alternate Key for Student Table will be ……….


a. StuName
b. StuContact
c. StuAadhar
d. StuClass

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

iv. Display the number of cars manufactured each year.


a. select count(*),year(dom) from carmarket where year(dom) = distinct;
b. select count(*),year(dom) from carmarket group by year(dom);
c. select count(carmarket),year(dom) from carmarket group by year(dom);
d. select count(distinct *),year(dom) from carmarket group by year(dom);

[12
]
CORRECT ANSWER - b

v. What is the cardinality and degree of the table CARMARKET?


a.Cardinality = 8 and Degree = 6
b. Cardinality = 6 and Degree = 7
c. Cardinality = 7 and Degree = 6
d. Cardinality = 7 and Degree = 8
CORRECT ANSWER – c

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

i.Choose the correct command for the following purpose.


To delete the record with GCode as 116
a. Delete * from GARMENT where GCode=’116’;
b. Delete from GARMENTS where GCode =116;
c. Delete from GARMENT where GCode =116;
d. Delete from GARMENT where GCode is ’116’;
Answer: b

ii. Give the output of :


Select GName, Price from GARMENT
order by Price Desc;
a.
GNAME PRICE
Tshirt 1400.00
Jeans 1600.00
Skirt 1100.00
Jacket 4000.00
Trousers 1500.00
LadiesTop 1200.00
b.
GNAME PRICE
Skirt 1100.00
LadiesTop 1200.00
Tshirt 1400.00
Trousers 1500.00
Jeans 1600.00
Jacket 4000.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

iii.Choose the correct command for the following purpose.


To change the colour of GARMENT with code as 116 to “Orange”.
a. Update GARMENTS set COLOUR=”Orange” where Gcode=116;
b. Update GARMENT set COLOUR =”Orange” where Gcode=116;
c. Update GARMENT set COLOUR =”Orange” where Gcode is ”116”;
d. Update GARMENT where Gcode=116 set COLOUR =Orange;
Answer: b

iv. What is the degree and cardinality of ‘GARMENT’ table?


a. Degree=5 & Cardinality=6
b. Degree=6 & Cardinality=5
c. Degree=5 & Cardinality=7
d. Degree=7 & Cardinality=5
Answer: a

3. Consider the table TEACHER given below. Write commands in SQL for (1) to (3) and output for (4)

i. To display all information about teachers of PGT category.


ii. To list the names of female teachers of Hindi department.
iii. To list names, departments and date of hiring of all the teachers in ascending order of date of joining
iv. SELECT DISTINCT(category) FROM teacher;

***All the Best***

[15
]

You might also like