0% found this document useful (0 votes)
75 views154 pages

Database, MySQL & Connectivity Q & A

hi

Uploaded by

Athul Bijukumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views154 pages

Database, MySQL & Connectivity Q & A

hi

Uploaded by

Athul Bijukumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 154

OUR OWN HIGH SCHOOL, AL WARQA’A

Computer Science SQL and Databases


Grade 12
SQL
(Questions from CBSE Board Exam- from 1998 to 2023 and CBSE Sample QP-2023-2024)

YEAR: 1998(Outside Delhi)

Q 5 (a) What is need for normalization? Define First, Second and Third Normal Form.
Ans. Need for Normalization: The basic objective of normalization is to reduce redundancy, which means
that data to be stored only once. Because storing data many times lead to wastage of space and increase
access time. So relation/table are to be normalized so that we can alter them at any time without doing
much changes.

First Normal Form: A table / relation is said to be in First Normal Form (1NF) if and only if all underlying
domains of the relation contain atomic (individual) values.

Second Normal Form: A table / relation is said to be in Second Normal Form (2NF) if and only if it is in First
Normal Form and every non-key attribute is functionally dependent on the Primary Key.

Third Normal Form: A table / relation is said to be in Third Normal Form (1NF) if and only if it is in Second
Normal From and every non-key attribute is non-transitively dependent upon the primary key.

NOTE : Write SQL commands for(b) to (g) and write the output for (h) on the basis of table HOSPITAL.
TABLE : HOSPITAL
No Name Age Department Datofadm Charges Sex
1 Sandeep 65 Surgery 23/02/98 300 M
2 Ravina 24 Orthopedic 20/01/98 200 F
3 Karan 45 Orthopedic 19/02/98 200 M
4 Tarun 12 Surgery 01/01/98 300 M
5 Zubin 36 ENT 12/02/98 250 M
6 Ketaki 16 ENT 24/02/98 300 F
7 Ankita 29 Cardiology 20/02/98 800 F
8 Zareen 45 Gynecology 22/02/98 300 F
9 Kush 19 Cardiology 13/01/98 800 M
10 Shaliya 31 Nuclear 19/02/98 400 M
Medicine
(b) To show all information about the patients of cardiology department.
Ans: SELECT * FROM hospital WHERE department=’Cardiology’;

(c ) To list the names of female patients who are in orthopedic dept.


Ans: SELECT name FROM hospital WHERE sex=’F’ AND department=’Orthopedic’;

(d) To list names of all patients with their date of admission in ascending order.
Ans.: SELECT name, dateofadm FROM hospital ORDER BY dateofadm;

(e) To display Patient’s Name, Charges, age for male patients only.
Ans: SELECT name, charges, age FROM hospital WHERE sex=’M’;
Page 1 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

(f) To count the number of patients with age >20.


Ans.: SELECT COUNT(age) FROM hospital WHERE age>20;
(g) To insert a new row in the HOSPITAL table wit the following .
11,”mustafa”,37,”ENT”,(25/02/98},250,”M”
Ans.: INSERT INTO hospital VALUES (11, ‘Mustafa’, 37, ‘ENT’, ‘25/02/98’, 250, ‘M’);
(h) Give the output of following SQL statement:
(i) Select COUNT(distinct departments) from HOSPITAL;
Ans: COUNT(DISTICNTDEPARTMEN)
--------------------------------------------
6
(ii) Select Max (Age) from HOSPITAL where SEX = “M”;
Ans: MAX(AGE)
---------------
65
(iii) Select AVG(Charges) from HOSPITAL where SEX = “ F”;
Ans.: AVG(CHARGES)
----------------------
400
(iv) Select SUM(Charges) from HOSPITAL where Datofadm<{12/02/98}
Ans.: SUM(CHARGES)
----------------------
1300
YEAR 1998(Delhi)
5(a) What is a relation? What is the difference between a tuple and an attribute?

Note: Write SQL commands for (b) to (g) and write the output for (h) on the basis of table HOSPITAL.
Table: HOSPITAL

No Name Age Department Dateofadm Charges Sex


1 Arpit 62 Surgery 21/10/98 300 M
2 Zarina 22 ENT 12/12/97 250 F
3 Kareem 32 Orthopedic 19/02/98 200 M
4 Arun 12 Surgery 11/01/98 300 M
5 Zubin 30 ENT 12/01/98 250 M
6 Ketika 16 ENT 24/02/98 250 F
7 Ankita 29 Cardiology 20/02/98 800 F
8 Zareen 45 Gynecology 22/02/98 300 F
9 Kush 19 Cardiology 13/01/98 800 M
10 Shilpa 23 Nuclear Medicine 21/02/98 400 F

(b) To select all the information of patients of Cardiology department.


(c) To list the names of female patients who are in ENT department.
(d) To list names of all patients with their date of admission in ascending order.
(e) To display patient’s name, charges, age for only female patients.
(f) To count the number of patients with age<30.
(g) To insert a new row in the HOSPITAL table with the following data:
Page 2 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
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;
(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};

Que 2 --YEAR : 1999

5.a) What is normalization ? Define third normal form


Ans.: Normalization: The normalization is the process of transformation of the conceptual schema (logical
data structure) of the database into a computer representable from..

Third Normal Form: A table / relation is said to be in Third Normal Form (1NF) if and only if it is in Second
Normal From and every non-key attribute is non-transitively dependent upon the primary key.

Given the following Employee Form :


No Name Age Department Dateofrt Salary Sex
d
1 Pankaj 54 Engg. 10/01/97 1200 M
2 Shalini 41 Estbl 24/03/98 2000 F
3 Sanjay 32 Engg. 12/12/96 3500 M
4 Sudha 25 Science 01/07/99 4700 F
5 Rakesh 32 Engg. 05/09/97 2500 M
6 Shakeel 40 Language 27/06/98 3000 M
7 Surya 44 Estbl. 25/02/97 2100 M
8 Shikha 33 Science 31/07/97 2600 F
Write SQL commands for (b) to (g) and write output for(h).
(b) To show all information about the employees of Engg. Branch
Ans. SELECT * FROM employee WHERE branch= ‘Engg.’;

(c ) To list the names of female employees who are in Science branch


Ans.: SELECT name FROM employee WHERE sex= ‘F’ AND branch= ‘Science’;

(d) To list the names of all employees with their date of retirement in ascending order.
Ans.: SELECT name, dateofrtd FROM employee ORDER BY dateofrtd;

(e) To display Employee’s name , Salary ,Age for male employees only
Ans.: SELECT name, salary, age FROM employee WHERE sex= ‘M’;

(f) To count the number of employees with AGE > 33.


Ans.: SELECT COUNT(age) FROM employee WHERE age>33;

(g) To insert a new row in the EMPLOYEE table with the following data:
9,”Rohit”,46,”language”,{22/06/98},2300,”M”
Page 3 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
Ans.: INSERT INTO employee VALUES (9, ‘Rohit’, 46, ‘Language’,’22/06/98’,2300, ‘M’);

(h) Give the output of the following SQL statements:


(i) Select COUT(distinct department) from EMPLOYEE;
Ans: COUNT(DISCTINCTDEPARTMENT)
------------------------------------------------
4
(ii) Select MAX(Age) from EMPLOYEE where SEX =”F”;
Ans.: MAX(AGE)
---------------
41

(v) Select AVG(Fee) form EMPLOYEE where SEX = “M”;


Ans.: No such column ‘Fee’

(iv) Select SUM(Fee) from EMPLOYEE where Dateofrtd<{01/01/98};


Ans.: No such column ‘Fee’

Que 3 –YEAR : 2000

(a) Differentiate between SQL commands DROP TABLE and DROP VIEW . Define Second Normal Form.
Ans.:
Drop Table Drop View
Removes the table physically from the storage Deletes the view from the database.
device.
The condition for dropping a table is that it must be Deletion of the row is not necessary.
an empty table.

Second Normal Form: A table / relation is said to be in Second Normal Form (2NF) if and only if it is in First
Normal Form and every non-key attribute is functionally dependent on the Primary Key.

Note: Write SQL commands for(b) to (e) and write the outputs for (f) on the basis of table GRADUATE.
TABLE : GRADUATE

S No NAME Stipend Subject Average Div


1 Karan 400 Physics 68 1
2 Divakar 450 Computers 68 1
3 Divya 300 Chemistry 62 2
4 Arun 350 Physics 63 1
5 Sabina 500 Mathematics 70 1
6 John 400 Chemistry 55 2
7 Robert 250 Physics 64 1
8 Rubina 450 Mathematics 68 1
9 Vikas 500 Computers 62 1
10 Mohan 300 Mathematics 57 2
(b) List the names of those students who obtained DIV 1 sorted by NAME .
Ans.: SELECT name FROM graduate WHERE div=1 ORDER BY name;
Page 4 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

(c )Display a report, listing NAME , STIPEND , SUBJCT and amount of stipend received in a year assuming
that the STIPEND is paid every month.
Ans.: SELECT name, stipend, subject, stipend *12 FROM graduate;

(d) To insert a new row in the GRADUATE table :


11,”KAJOL”,300,” COMPUTERS”,75,1
Ans.: INSERT INTO graduate VALUES (11, ‘Kajol’, 300, ‘Computers’, 75,1);

(h) Give the output of the following SQL statements based on table GRADUATE :
(i) Select MIN(AVERAGE ) from GRADUATE where SUBJECT=”PHYSICS”;
Ans. MIN(AVERAGE)
----------------------
63
(ii)Select SUM(STIPEND) from GRADUATE where DIV=1;
Ans.: SUM(STIPEND)
---------------------
1000
(iii)Select AVG(STIPEND) from GRADUATE where AVERAGE >=65;
Ans.: AVG(STIPEND)
----------------------
450
(vi) Select COUNT( distinct SUBJECT) from GRADUATE;
Ans.: COUNT(DISCTINCTSUBJECT)
------------------------------------------
4
Assume that there is one more table GUIDE in the database as shown below:
Table : GUIDE
MAINAREA ADVISOR
PHYSICS VINOD
COMPUTER SC ALOK
CHEMISTRY RAJAN
MATHEMATICS MAHESH
What will be the output of the following query:
SELECT NAME, ADVISOR
FROM GRADUATE, GUIDE
WHERE SUBJECT=MAINAREA;
Ans.:
NAME ADVISOR
--------- -------------
Karan VINOD
Divakar ALOK
Divya RAJAN
Arun VINOD
Sabina MAHESH
John RAJAN
Robert VINOD
Page 5 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
Rubina MAHESH
Vikas ALOK
Mohan MAHESH

Que 4 – YEAR : 2001


(a) When is a relation said to be in second normal form?
Ans.: Second Normal Form: A table / relation is said to be in Second Normal Form (2NF) if and only if it is in
First Normal Form and every non-key attribute is functionally dependent on the Primary Key.

(b) Write SQL commands for (I) to ( vii) on the basis of the table SPORTS
S no. Class Name Game1 Grade1 Game2 Grade2
10 7 Sameer Cricket B Swimming A
11 8 Sujit Tennis A Skating C
12 7 Kamal Swimming B Football B
13 7 Veena Tennis C Tennis A
14 9 Archana Basketball A Cricket A
15 10 Arprit Cricket A Athletics C

(j) 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 greade1= ‘A’ or 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’.
And.: SELECT game1,game2 FROM sports WHERE name LIKE ‘A%’;

(v) Add a new column named ‘ Marks’.


Ans.: ALTER TABLE sports ADD (marks NUMBER(2));

(vi) Assign a value 200 for marks for all those who are getting grade ‘B’ or grade ‘ A’ in both Game1
and game2.
Ans.: UPDATE sports SET marks=200 WHERE grade1= ‘A’ OR garde2= ‘A’ or grade1= ‘B’ OR garde2= ‘B’;

(vii) Arrange the whole table in the alphabetical order of Name.


Ans.: SELECT * FROM sports ORDER BY name;

Explain Cartesian product of two relations:


Ans.: Cartesian product of two relations: The Cartesian Product of two relations is the combination of tuples
/ records belonging to the two tables / relations. The Cartesian Product is a binary operation and is denoted
by (X). The degree of new relation is the sum of the degrees of two relations on which Cartesian Product is
performed. The number of tuples of the new relation is equal to the product of the number of tuples of the
two relations on which Cartesian Product is performed.
Page 6 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

Que 5—YEAR : 2002

(a) Differentiate between Data Definition Language and Data Manipulation Language.
Ans.:
DDL DML
1. Data Definition Language 1. Data Manipulation Language
2. It provides statements for creation and deletion 2. It provides statements fro manipulation of the
of database. database.
3. Examples: CREATE TABLE, ALTER TABLE 3. Examples: INSERT, DELETE, UPDATE

(b) Given the following Teacher relation : Write SQL command for question (b) to (g)
No Name Department Dteofjoining Salary Sex
1 Raja Computer 21/05/98 8000 M
2 Sangita History 21/05/97 9000 F
3 Ritu Sociology 29/08/98 8000 F
4 Kumar Linguistics 13/06/96 10000 M
5 Venkat History 31/10/99 8000 M
6 Sidhu Computer 21/05/86 14000 M
7 Aishwarya Sociology 11/01/88 12000 F

(c) To select all the information of teacher in computer department.


Ans.: SELECT * FROM teacher WHERE department= ‘Computer’;

(d) To list the name of female teachers in History department.


Ans.: SELECT * FROM teacher WHWRE sex= ‘F’ AND department = ‘History’;

(e) To list all names of teachers with date of admission in ascending order.
Ans.: SELECT name, dateofjoining FROM teacher ORDER BY dateofjoining;

(f) TO display Teacher’s name, Department, and Salary of female teacher.


Ans.: SELECT name, department, salary FROM teacher WHERE sex= ‘F’;

(g) To count the number of items whose salary is less than 10,000.
Ans. SELECT COUNT(*) FROM teacher WHERE salary<10000;

(h) To insert a new record in the Teacher table with thefollowing data:
8,”Mersha”,”computer”,{1/1/2000},12000,”m”.
Ans.: INSERT INTO teacher VALUES (8, ‘Mersha’, ‘Computer’, ‘01/01/2000’,12000, ‘M’);

(i) Give the output of the following SQL command:


1.SELECT MIN(DISTINCT Salary) FROM Teacher;
Ans.: MIN(DISTINCTSALARY)
---------------------------------
8000
2. SELECT MIN (Salary) FROM Teacher WHERE Sex = ”M”;
Ans.: MIN(SALARY)
Page 7 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
--------------------
8000
3. SELECT SUM(Salary) FROM Teacher WHERE Department = ”HISTORY”;
Ans.: SUM(SALARY)
---------------------
17000
4. SELECT AV(Salary) FROM Teacher WHERE Dateofjoining < {1/1/98}
Ans. AVG(SALARY)
--------------------
11250

Que 6—YEAR 2003


(a) What is primary key in a table? Define first normal form.
Ans.: Primary Key: A Primary Key is a set of one or more attributes that can uniquely identify tuples /
records within a relation / table.

First Normal Form: A table / relation is said to be in First Normal Form (1NF) if and only if all underlying
domains of the relation contain atomic (individual) values.

NOTE : Write SQL commands for(b) to (g) and write the outputs for(h) on the basis of table INTERIORS
TABLE : INTERIORS

No. Itemname Type Dateofstock Price Discount


1 Red_rose Double Bed 23/02/02 32000 15
2 Soft touch Baby cot 20/01/02 9000 10
3 Jerry's home Baby cot 19/02/02 8500 10
4 Rough wood Office Table 01/01/02 20000 20
5 Comfort zone Double Bed 12/01/02 15000 20
6 Jerry look Baby cot 24/02/02 7000 19
7 Lion king Office Table 20/02/02 16000 20
8 Royal tiger Sofa 22/02/02 30000 25
9 Park sitting Sofa 13/12/01 9000 15
10 Dine Paradise Dining Table 19/02/02 11000 15
11 White wood Double Bed 23/03/03 20000 20
12 James 007 Sofa 20/02/03 15000 15
13 Tom look Baby cot 21/02/03 7000 10
(b) TO show all information about the Sofas from the INTERIORS table
Ans.: SELECT * FROM interiors WHERE type= ‘Sofa’;

(c) To list the ITEMNAME which are priced at more than 10000 from the INTERIORS TABLE;
Ans.: SELECT itemname FROM interiors WHERE price>10000;

(d) To list ITEMNAME and TYPE of those items, in which DATEOFSTOCK is be 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 DESC;

(e) TO display ITEMNAME and DATEOFSTOCK of items whose discount is more than 15
Page 8 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
Ans. SELECT itemname, dateofstcok FROM interiors WHERE discount>15;

(f) TO count the number of items, whose type is “ DOUBLE BED” from INTERIORS table
Ans.: SELECT COUNT(type) FROM interiors WHERE type= ‘Double Bed’;

(g) TO insert a new row in the NEWONES table with the following data:
14,” True Indian” , ”Office Table”,{28/03/03},15000,20
Ans.: INSERT INTO interiors VALUES (14, ‘True Indian’, ‘Office Table’, ‘28/03/03’, 15000, 20);

(h) Give the output of following SQL statements :


NOTE : Outputs of the below mentioned queries should be based on the original data given in both the
tables i.e. without considering the insertion done in (g) part of this question.

(i) Select COUNT(distinct TYPE) from INTERIORS;


Ans.: COUNT(DISTICNTTYPE)
----------------------------------
5
(ii) Select AVG(DISCOUNT) from INTERIORS where TYPE = “ Baby Cot”;
Ans.: AVG(DISCOUNT)
-----------------------
13
(iii) Select SUM(Price) from INTERIORS where DATOFSTOCK < ( 12/02/02};
Ans.: SUM(PRICE)
-----------------
53000

Year : 2004
5(a) What is do you understand by normalization? Define First normal form
Ans.: Normalization: The normalization is the process of transformation of the conceptual schema (logical
data structure) of the database into a computer representable from..

First Normal Form: A table / relation is said to be in First Normal Form (1NF) if and only if all underlying
domains of the relation contain atomic (individual) values.

Table: Books
Book_Id Book_Name Author_Name Publishers Price Type Quantity
F001 The Tears William Hopkins First Publ. 750 Fiction 10
F002 Thunderbolts Anna Roberts First Publ. 700 Fiction 5
T001 My First C++ Brian & Brooke EPB 250 Text 10
T002 C++ Brainworks A.W.Rossaine TDH 325 Text 5
C001 Fast Cook Lata Kapoor EPB 350 Cookery 8

Table: Issued
Book_Id Quantity_Issued
F001 3
T001 1
C001 5
Page 9 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

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


b) To show Book name, Author Name and price of books of EPB publishers.
Ans.: SELECT Book_name, author_name, price FROM books WHERE publishers= ‘EPB’;

c) To list the name of books of Fiction type


Ans.: SELECT book_name FROM books WHERE type= ‘Fiction’;

d) To display the name and price 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 Publ. 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. (The
query will require contents from both tables)
Ans.: SELECT books.Book_id, book_name, quantity_issued FROM books, issued WHERE books.book_id =
issued.book_id;

g) To insert a new row in the table Issued having the following data:
“F001”, 4
Ans.: INSERT INTO issued VALUES (‘F002’,4);

h) Give the output of the following queries based on the above tables.
i) Select count(distict publichers) from books;
i) COUNT(DISTINCTPUBLISHERS)
-------------------------------------------
3
ii) Select sum(price) from books where quantity>5;
ii) SUM(PRICE)
-----------------
1350
iii) Select book_name, author_name from books where price<500;
iii) BOOK_NAME AUTHOR_NAME
------------------ -----------------------
My First C++ Brain & Brooks
C++ Brainworks A.W. Rossaine
Fast Cook Lata Kapoor

iv) Select count(*) from books;


iv) COUNT(*)
-------------
5
Que 7—CBSE 1
(a) What is the purpose of normalization? Define primary key.
Answer: Purpose of Normalization:
➢ It reduces data redundancies

Page 10 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
➢ It helps in eliminating data anomalies
➢ It helps in linking tables
Primary Key: It is set of one or more attributes that can uniquely identify tuples or records within a table or
relation.

NOTE: Write SQL commands for (b) to(g) and write the outputs for(h) on the basis of tables FURNITURE.
TABLE : FURNITURE

No. Itemname Type Dateofstock Price Discount


1 White lotus Double Bed 23/02/02 30000 25
2 Pink feather Baby cot 20/01/02 7000 20
3 Dolphin Baby cot 19/02/02 9500 20
4 Decent office table 01/01/02 25000 30
5 Comfort Zone Double bed 12/01/02 25000 25
6 Donald Baby cot 24/02/02 6500 15
7 Royal finish Office table 20/02/02 18000 30
8 Royal tiger Sofa 22/02/02 31000 30
9 Econo sitting Sofa 13/12/01 9500 25
10 Eating Dining table 19/02/02 11500 25
paradise
11 WoodComfor Double Bed 23/03/03 25000 25
t
12 Old Fox Sofa 20/02/03 17000 20
13 Micky Baby cot 21/02/03 7500 15
(b) To show all information about the Baby cots from the FURNITURE table

SELECT * FROM furniture WHERE type= “Baby cot”;

(c) To list the ITEMNAME which are priced at more than 15000 from the FURNITURE table.
1
SELECT itemname FROM furniture WHERE price>15000;

(d) To list ITEMNAME and TYPE of those items, in which date of stock is before 22/01/02 from the
FURNITURE table in the descending order of ITEMNAME
Select itemname, type from furniture where dateofstock < {22/01/02} order by itemname;

(e) To display ITEMNAME and DATAOFSTOCK of those items, whose TYPE is “ Sofa” from FURNITURE table

Select itemname , dateofstock from furniture where type=’Sofa’;

(f) To insert a new row in the ARRIVALS table with the following data:
14,”Velvet touch” , ”Double Bed”,{25/03/03},25000,30
Insert into furniture values (14, ‘Velvet touch’, ‘Double Bed’, {25/03/03}, 25000, 30);

(g) Give the output of following SQL statement:


NOTE : Outputs of the above mentioned queries should be based on original data given in both the tables
i.e. without considering the insertion done in (g) part of this question
Page 11 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

(j) Select COUNT (distinct TYPE) from FURNITURE;


COUNT(DISTINCT TYPE)
------------------------------------
5
(ii) Select MAX(DISCOUNT) from FURNITURE ;
MAX(DISCOUNT)
------------------------
30

(iii) Select AVG(DISCOUNT) from FURNITURE where TYPE = “ Baby cot”;


AVG(DISCOUNT)
------------------------
17.5

(iv) Select SUM(Price) from FURNITURE where DATEOFSTOCK<{12/02/02}


SUM(PRICE)
-----------------

Que 8 – CBSE 2
(a) What is primary key in a table? What is first normal from a database?
Primary Key: It is set of one or more attributes that can uniquely identify tuples or records within a table or
relation.

First Normal Form: A table / relation is said to be in First Normal Form (1NF) if and only if all underlying
domains of the relation contain atomic (individual) values.

NOTE: Write SQL commands for (b) to (g) and write the outputs for (h) on the basis of tables INTERIORS and
NEWONES
The rest as same as question no. 6 .

(SAME AS BOVE)

CBSE-I: 2004
(a) What do you understand by Degree and Cardinality of a table?
Ans: Degree of a table is total number of attributes or fields or columns.
Cardinality of a table is total number of rows/records/tuples.

(b). Consider the following tables ACTIVITY and COACH.


Write SQL commands for the statements (i) to (iv) and give the
The outputs for the SQL queries (v) to (viii)

Page 12 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
Table: ACTIVITY
ACode ActivityName ParticipantsNum PrizeMoney Schedul
eDate
1001 Relay 100X4 16 10000 23-Jan-2004
1002 High Jump 10 12000 12-Dec-2003
1003 Shot Put 12 8000 14-Feb-2004
1005 Long Jump 12 9000 01-Jan-2004
1008 Discuss Throw 10 15000 19-Mar-2004

Table: COACH
PCode Name ACode
1 Ahmed Hussain 1001
2 Ranvinder 1008
3 Janila 1001
4 Naaz 1003

(i) To display the name of all activities with their Acodes in


descending order.
Ans. SELECT ActivityName, Acode FROM activity ORDER
BY Acode DESC;

(ii) To display sum of prizemoney for each of the number of


participants groupings (as shown in column ParticipantsNum 10,12,16)
Ans.: SELECT SUM(PrizeMoney), ParticipantsNum FROM
activity GROUP BY ParticipantsNum;

(iii) To display the coach’s name and ACodes in acending order of


ACode from the table COACH.
Ans: SELECT Name,ACode FROM coach ORDER BY ACode;

(iv) To display the content of the Activity table whose ScheduleDate


is earlier than 01/01/2004 in ascending order of ParticipantsNum
Ans: SELECT * FROM activity WHERE ScheduleDate<{01/01/2004}
ORDER BY ParticipantsNum;

(v) SELECT COUNT(DISTINCT ParticipantsNum) FROM Activity;


Ans: 3

(vi) SELECT MAX(ScheduleDate), MIN(ScheduleDate) FROM


activity;
MAX(ScheduleDate) MIN(ScheduleDate)
--------------------------- --------------------------
19/03/2004 12/12/2003

(vii) SELECT SUM(PrizeMoney) FROM activity;


Page 13 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
SUM(PRIZEMONEY)
----------------------------
54000

(viii) SELECT DISTINCT ParticipantsNum FROM activity;


DISTINCTPRTICIPANTNUM
---------------------------------------
16
10
12

CBSE-II: 2004

(a) What do you understand by Primary Key and Candidate Key? 2


Answer: Primary Key: An attribute or set of attributes, which are used to identify a tuple
(record / row) uniquely is known as Primary Key.
Candidate Key: If a table has more than one such attributes which uniquely identify a tuple and are eligible
to be the primary key, all such attributes are known as Candidate Keys.

(b) Consider the following tables GAMES and PLAYER. Wtite SQL Commands for the statements (i) to (iv)
and give outputs for SQL queries (v) to (viii) 6
Table: GAMES
Gcode GameName Number PrizeMoney ScheduleDate

101 Carom Board 2 5000 23-Jan-2004

102 Badminton 2 12000 12-Dec-2003

103 Table Tennis 4 8000 14-Feb-2004


105 Chess 2 9000 01-Jan-2004
108 Lawn Tennis 4 25000 19-Mar-2004

Table: PLAYER
PCode Name GCode
1 Nabi Ahmad 101
2 Ravi Sahai 108
3 Jatin 101
4 Nazneen 103

(i) To display the name of all Games with their Gcodes.


SELECT GameName, Gcode FROM games;

(ii) To display details of those games which are having prizemoney more than 7000.
Page 14 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
SELECT * FROM games WHERE PrizeMoney>7000;
(iii) To display the contents of the GAMES table in ascending order of ScheduleDate
SELECT * FROM games ORDER BY ScheduleDate;

(iv) To display sum of PrizeMoney for each of the number of participation.


SELECT SUM(PrizeMoney), Number FROM games GROUP BY number;

(v) SELECT COUNT(DISTINCT number) FROM games;


COUNT(DISTCINT)
--------------------------
2

(vi) SELECT MAX(SheduleDate), MIN(ScheduleDate) FROM games;


MAX(SHEDULEDATE) MIN(SCHEDULEDATE)
------------------------------- ---------------------------------
19-Mar-2004 12-Dec-2003

(vii) SELECT SUM(PrizeMoney) FROM games;


SUM(PRIZEMONEY)
-----------------------------
59000

(viii) SELECT DISTICNT gcode FROM player;


DISTICNT GOCDE
-------------------------
101
108
103
Year: 2005 (Outside Delhi)

5 (a) What do you understand by the terms Candidate Key and Cardinality of a relation in a relational
database? 2
Ans.: (a) Candidate Key: The attribute (Column) or set of attributes (Columns)
which can identify a tuple/row uniquely are known as Candidate Key(s).
OR
Candidate Key: The attribute (Column) or set of attributes (Columns),
which are capable of acting as candidate for primary key.
Cardinality of a relation: Number of rows in a table form cardinality of a
relation.

Page 15 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
(1 Mark each for giving correct definition)
OR
(1 Murk each for explaining the concept using suitable example)

(b)Consider the following tables WORKERS and DESIG. Write SQL.com.mands for the statements (i) to (iv)
and give outputs of SQL queries (v) to (viii)

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
400 32000 7500 Salesman
451 28000 7500 Salesman

WORKERS

W_ID FIRSTNAME LASTNAME ADDRESS CITY


102 Sam 'Tones 33 Elm St. Paris
105 Sarah Ackerman 440 U.S. 110 New York
144 Manila Sengupta 24 Friends Street New Delhi
210 George Smith 83 First Street Howard
255 Mary Jones 842 Vine Ave. Losantiville
300 Robert Samuel 9 Fifth Cross Washington
335 Henry Williams 12 Moore Street Boston
403 Ronny Lee 121 Harrison St. New York
451 Pat Thompson 11 Red Road Paris

(i) To display W_ID, Firstname, Address and City of all' employees living in
New York from the table .WORKERS.
(b) (i) SELECT W_ID,FIRSTNAME,ADDRESS,CITY FROM
WORKERS WHERE CITY='New York';
(½ Mark for correct SELECT FROM)
(½ Mark for correct WHERE clause)
Page 16 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

(ii) To display the content of WORKERStabieiri ascending order of LASTNAME.

(ii) SELECT * FROM WORKERS ORDER BY LASTNAME;


(½ Mark for correct SELECT FROM)
(½ Mark for correct ORDER BY clause)

(iii) To display the Firstname, Lastname, and Total Salary of ail Clerks from
the tables WORKERS and DESIG, where Total Salary is calculated as Salary. + Benefits.
(iii) SELECT FIRSTNAME, LASTNAME, SALARY+BENEFITS
FROM WORKERS.DESIG WHERE DESIGNATION=’CLERK’
.
AND WORKERS,W_ID=DESIG
W_ID;
OR
SELECT FIRSTNAME,LASTNAME,SALARY+BENEFITS AS
TOTAL SALARY FROM WORKERS.DESIG WHERE
DESIGNATION=’CLERK’ AND
.
WORKERS.W_ID=DESIG
W_ID;
(½ Mark for correct SELECT FROM)
(½ Mark for correct WHERE clause)

(iv) To display the Minimum salary am~mg Managers and Clerks from. the table
DESIG.
(iv) SELECT MIN(SALARY), DESIGNATION FROM DESIG WHERE
DESIGNATION IN ('Manager'.'Clerk') GROUP BY DESIGNATION;
OR
SELECT MIN(SALARY), DESIGNATION FROM DESIG WHERE
DESIGNATION= ‘Manager’ OR DESIGNATION='Clerk' GROUP BY
DESIGNATION;
OR
SELECT MIN(SALARY) FROM DESIG WHERE DESIGNATION=
‘Manager’ OR DESIGNATION='Clerk';
OR
SELECT MIN(SALARY) FROM DESIG WHERE DESIGNATION IN
(‘Manager’,‘Clerk’);
(½ Mark for correct SELECT FROM)
(½ Mark for correct MIN function and WHERE clause)

(v) SELECT FIRSTNAME, SALARY


FROM WORKERS, DESIG
WHERE DESIGNATION = 'Manager' AND workers.w-id=desig.w-id;

Page 17 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

(v)
FIRSTNAME SALARY
Sam 75000
Manila 70000
George 75000
(½ Mark for the correct output)

(vi) Select count(distinct designation) from design;

(vi)
COUNT(DISTINCT DESIGNATION)
4
(½ Mark for the correct output)

(vii) select designation, sum(salary) from design

group by designation having count(*)<3;

(vii)
DESIGNATION SUM(SALARY)
Director 85000
Salesman 60000
(½ Mark for the correct output)

(viii) select sum( benefits) from design where

designation=’Salesman’;
(viii)
(½ Mark for mentioning the error)
OR
(½ Mark for attempting this part of the question)
OR
(½ Mark for correctly attempting any two parts of the SQL question)

Year: 2005 (Delhi)

5(a) What do you understand by the terms Primary Key and Degree of a relation in
relational database? 2

(a) Primary Key: The attribute (Column) or set of


attributes (Columns) which is used to identify
a tuple/row uniquely are known as Primary Key.
Degree of a relation: Number of attribute or column

Page 18 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
in a table form cardinality of a relation.
[1 Mark each for giving correct definition]

OR
[1 Mark each for explaining the concept using
suitable example]

(b)Consider the following tables EMPLOYEES and


EMPSALARY. Write SQL

commands for the statements (i) to (iv) and give


outputs for SQL queries (v) to (viii). 6
EMPLOYEES

EMPID FIRSTNAME LASTNAME ADDRESS CITY


010 George Smith 83 First Street Howard
105 Mary Jones 842 Vine Ave. Losantiville
152 Sam Tones 33 Elm St. Paris
215 Sarah Ackerman 440 U.S 110 Upton
244 Manila Sengupta 24 Friends Street New Delhi
300 Robert Samuel 9 Fifth Cross Washington
335 Henry Williams 12 Moore Street Boston
400 Rachel Lee 121 Harrison St. New York
441 Peter Thompson 11 Red Road Paris

EMPSALARY
EMPID SALARY BENEFITS DESIGNATION
010 75000 15000 Manager
105 65000 15000 Manager
152 80000 25000 Director
215 75000 12500 Manager
244 50000 12000 Clerk
300 45000 10000 Clerk
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.

(b) (i)
Select FIRSTNAME, LASTNAME, ADDRESS, CITY From
EMPLOYEES
Where CITY= ‘Paris’;

Marks for each part (here parts are separated into lines for
Page 19 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
convenience) of correct SQL Command]

(ii) To display the content of EMPLOYEES table in descending order of


FIRSTNAME.

(ii)
Select * From EMPLOYEES
Order By FIRSTNAME;
[½ Marks for each part (here parts are separated into lines for
convenience) of correct SQL Command]

(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.

(iii)
Select FIRSTNAME, LASTNAME, SALARY From EMPLOYEES,
EMPSALARY
Where EMPLOYEES.EMPID=EMPSALARY.EMPID;

Marks for each part (here parts are separated into
lines for
convenience) of correct SQL Command]

(iv) To display the Maximum salary among Managers and


Clerks from the table EMPSALARY.

(iv)
Select Max(SALARY) From EMPSALARY

Where DESIGNATION = ‘Manager’ OR DESIGNATION =


‘Clerk’;
[½ Marks for each part (here parts are separated into
lines for
convenience) of correct SQL Command]

(v)
FIRSTNAME, SALARY
SELECT
FROM EMPLOYEES, EMPSALARY
WHERE DESIGNATION = ‘Salesman’ AND
EMPLOYEES.EMPID=EMPSALARY.EMPID;

Page 20 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

v)
FIRSTNAME SALARY
Rachel 32000
Peter 28000
[½ Mark for correct result]
Note: Heading is Optional

(vi)
SELECT COUNT(DISTINCT DESIGNATION)FROM EMPSALARY;

(vi)
COUNT (DISTINCT DESIGNATION)
4
Note: Heading is Optional
[½ Mark for correct result]
(viI)
SELECT DESIGNATION, SUM(SALARY)
FROM EMPSALARY
GROUP BY DESIGNATION HAVING COUNT(*)>2;

(vii)
DESIGNATION SUM(SALARY)
Manager 215000
Note: Heading is Optional
Clerk 135000
[½ Mark fdr correct result]

(viii)
SELECT SUM(BENEFITS)
FROM EMPLOYEES
WHERE DESIGNATION = ’Clerk’;
(viii)
(½ Mark for mentioning the error)
OR
(½ Mark for attempting this part of the question)
OR
(½ Mark for correctly attempting any two part of the
SQL question)

YEAR 2006 ( OUTSIDE DELHI)

Page 21 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
5.(a) What are DDL and DML? [2]
(a) DDL – Data Definition Language
DML – Data Manipulation Language

(1 Mark each for correct full form OR correctly explaining with the
help of examples)

(b) Study the following tables FLIGHTS and FARES and


write SQL commands for the questions (i) to (iv) and give outputs for
SQL queries (v) to (vi)
TABLE: FLIGHTS
FL_NO STARTING ENDING NO_FLIGHTS NO_STOPS
IC301 MUMBAI DELHI 8 0
IC799 BANGALORE DELHI 2 1
MC101 INDORE MUMBAI 3 0
IC302 DELHI MUMBAI 8 0
AM812 KANPUR BANGALORE 3 1
IC899 MUMBAI KOCHI 1 4
AM501 DELHI TRIVANDRUM 1 5
MU499 MUMBAI MADRAS 3 3
IC701 DELHI AHMEDABAD 4 0

TABLE: FARES
FL_NO AIRLINES FARE TAX%
IC701 Indian Airlines 6500 10
MU499 Sahara 9400 5
AM501 Jet Airways 13450 8
IC899 Indian Airlines 8300 4
IC302 Indian Airlines 4300 10
IC799 Indian Airlines 10500 10
MC101 Deccan Airlines 3500 4

(i)Display FL_NO and NO_FLIGHTS from “KANPUR” to “BANGALORE” from the table FLIGHTS.
(b) (i)
SELECT FL_NO,NO_FLIGHTS FROM FLIGHTS
WHERE STARTING=’KANPUR’ AND ENDING=’BANGALORE’;
(1/2 Mark for using SELECT and FROM correctly)
(1/2 Mark for correct WHERE clause)

(ii) Arrange the contents of the table FLIGHTS in the ascending order of FL_NO.

(ii)
SELECT * FROM FLIGHTS ORDER BY FL_NO;
(1/2 Mark for using SELECT and FROM correctly)
(1/2 Mark for correct ORDER BY clause [ASC is optional])

Page 22 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
(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 be paid=FARE+FARE*TAX%/100
(iii)
SELECT FLIGHTS.FL_NO, FARE+FARE*TAX/100
FROM FLIGHTS, FARES WHERE FLIGHTS.STARTING=’DELHI’ AND
FLIGHTS.ENDING=’MUMBAI’ AND FLIGHTS.FL_NO=FARES.FL_NO;
*Assuming TAX% as TAX
(Full 1 Mark for correctly attempting any part of 5 (b))

(iv) Display the minimum fare “Indian Airlines” is offering from the tables FARES.
(iv)
SELECT MIN(FARE) FROM FARES WHERE AIRLINES=’INDIAN AIRLINES’;
(1/2 Mark for using SELECT and FROM with MIN function correctly)
(1/2 Mark for correct WHERE clause)

(v) SELECT FL_NO, NO_FLIGHTS, AIRLINES FROM FLIGHTS, FARES WHERE STARTING = “DELHI” AND
FLIGHTS.FL_NO=FARES.FL_NO.

(v)
FL_NO NO_FLIGHTS AIRLINES
IC302 8 Indian Airlines
AM501 1 Jet Airways
IC701 4 Indian Airlines
(1 Mark for correct output, Ignore First header line)

(vi) SELECT COUNT(DISTINCT ENDING) FROM FLIGHTS. [6]


(vi) 7
(1 Mark for correct output)

YEAR 2006 (DELHI)

5 (a) What is an Alternate Key ? 2


(a) Candidate key(s), which is not selected as Primary Key, is known as
Alternate key(s).
(2 marks for any equivalent correct definition)

(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): 6

TABLE : DOCTOR
ID NAME DEPT SEX EXPERIENCE

Page 23 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
101 SMITH ORTHOPEDIC M 5
107 GEORGE CARDIOLOGY M 10
114 LARA SKIN F 3
109 K GEORGE MEDICINE F 9
105 JOHNSON ORTHOPEDIC M 10
117 LUCY ENT F 3
111 BILL MEDICINE F 12
130 MURPHY ORTHOPEDIC M 15

TABLE : SALARY
ID BASIC ALLOWANCE CONSULTATION
101 12000 1000 300
104 23000 2300 500
107 32000 4000 500
114 12000 5200 100
109 42000 1700 200
105 18900 1690 300
130 21700 2600 300

(i) Display NAME of all doctors who are in “MEDICINE” having more than 10
years experience from the table DOCTOR.
(i)
SELECT NAME FROM DOCTOR
WHERE DEPT = ‘MEDICINE’ AND EXPERIENCE >10;
(½ mark for correct Select statement)
(½ mark for correct Where clause)

(ii) Display the average salary of all doctors working in “ENT” department using
the tables DOCTOR and SALARY. Salary = BASIC + ALLOWANCE
(ii)
SELECT AVERAGE(S.BASIC + S.ALLOWANCE)
FROM DOCTOR D, SALARY S
WHERE D.DEPT = ‘ENT’ AND D.ID = S.ID;
OR
SELECT AVERAGE(BASIC + ALLOWANCE)
FROM DOCTOR, SALARY
WHERE DEPT = ‘ENT’ AND DOCTOR.ID = SALARY.ID;
(1/2 mark for correct Select statement)
(1/2 mark for correct Where clause)
OR
(1 mark for students who have correctly attempted any two parts of Q5b)

(iii) Display the minimum ALLOWANCE of female doctors,


(iii)
Page 24 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
SELECT MIN(S.ALLOWANCE)
FROM DOCTOR D, SALARY S
WHERE D.SEX = ‘F’ AND D.ID = S.ID;
OR
SELECT MIN(ALLOWANCE)
FROM DOCTOR, SALARY
WHERE SEX = ‘F’ AND DOCTOR.ID = SALARY.ID;
(½ mark for correct Select statement)
(½ mark for correct Where clause)

(iv) Display the highest consultation fee among all male doctors,
(iv)
SELECT MAX(S.CONSULTATION)
FROM DOCTOR D, SALARY S
WHERE D.SEX = ‘M’ AND D.ID = S.ID;
OR
SELECT MAX(CONSULTATION)
FROM DOCTOR , SALARY
WHERE SEX = ‘M’ AND DOCTOR.ID = SALARY.ID;
(1/2 mark for correct Select statement)
(1/2 mark for correct Where clause)

(v) SELECT count(*) from DOCTOR where SEX = “F”


(v)

4
(1 mark for correct answer)

(vi) SELECT NAME, DEPT, BASIC from DOCTOR, SALARY


where DEPT = “ENT” and DOCTOR.ID = SALARY.ID

(vi)
NAME DEPT BASIC
John ENT 12000
(1 mark for correct answer)

YEAR 2007 (OUTSIDE DELHI)

5(a) Differentiate between primary key and alternate key. 2


(a) All candidate keys, which are not the primary key of the table are called the
alternate keys.
OR
Primary Key:
An attribute/ column used to identify each record in a table
Alternate Key:
All such attributes/columns, which can act as a primary key
but are not the primary key in a table.

Page 25 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
(2 Mark for any valid difference/relation between Primary Key and
Alternate Key)
OR
(1 Mark for correct explanation of Primary Key)
(1 Mark for correct explanation of Alternate Key)

(b) Consider the following tables. Write SQL commands for the statements
(i) to (iv) and give outputs for SQL queries (v) to (viii) 6
TABLE:SENDER

SenderlD SenderName SenderAddress SenderCiry


ND01 R Jain 2, ABC Appts New Delhi
MU02 H Sinha 12, Newtown Mumbai
MU15 S Jha 27/A, Park Street Mumbai
ND50 T Prasad 122-K, SDA New Delhi

TABLE : RECIPIENT

RecID SenderlD RecName RecAddress RecCity


KO05 ND01 R Bajpayee 5, Central Avenue Kolkata
ND08 MU02 S Mahajan 116, A Vihar New Delhi
MU19 ND01 H Singh 2A, Andheri East Mumbai
MU32 MU15 P K Swamy B5, C S Terminus Mumbai
ND48 ND50 S Tripathi 13, B1 D, Mayur Vihar New Delhi

(i) To display the names of all Senders from Mumbai


(b) (i)
SELECT SenderName from Sender WHERE City =
‘Mumbai’;

(ii) To display the RecID), SenderName, SenderAddress, RecName,


RecAddress for every Recipient
(ii)
SELECT R.RecID, S.SenderName, S.SenderAddress,
R.RecName, R.RecAddress FROM Sender S,
Recipient R
WHERE S.SenderID = R.SenderID;

(iii) To display Recipient details in ascending order of RecName


(iii)
SELECT * FROM Recipient ORDER BY RecName;

Page 26 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
(iv) To display number of Recipients from each city
(iv)
SELECT COUNT(*) FROM Recipient GROUP BY RecCity

(v) SELECT DISTINCT SenderCity FROM Sender;


(v)
SenderCity
Mumbai
New Delhi

(vi) SELECT A. SenderName, B.RecName


FROM Sender A, Recipient B
WHERE A. SenderlD = B.SenderlD AND B.RecCity = ‘Mumbai’;

(vi)
A.SenderName B.RecName
R Jain H Singh
S Jha P K Swamy

(vii) SELECT RecName, RecAddress


FROM Recipient
WHERE RecCity NOT IN (‘Mumbai’, ‘Kolkata’);
(vii)
RecName RecAddress
S Mahajan 116, A Vihar
S Tripathi 13, Bl D, Mayur Vihar

(viii) SELECT RecID, RecName


FROM Recipient
WHERE SenderID=’MU02' ORSenderID=’ND50';
(viii)
RecID RecName
ND08 S Mahajan
ND48 S Tripathi
(Part (i) to (iv) - 1 Mark for each correct query)
(Part (v) to (viii) - ½ Marks for each correct output)
Note:
• Column headings for the output questions to be ignored.
• Since in part (iv) the fieldname RecCity is not mentioned
specifically, so full 1 mark to be given if any part of 5 (b) is
answered correctly.

Page 27 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

YEAR 2007 (DELHI)

5(a) What is the importance of a Primary Key in a table ? Explain with a suitable
example. 2
(a) The Primary Key is an attribute/set of attributes that identifies a tuple/ row/
record uniquely.
Example:
Rollnumber in the table STUDENT
OR
AccessionNumber in the table LIBRARY
OR
EmpNumber in the table EMPLOYEE
OR
PanNumber in the table INCOMETAX
OR
MemberNumber in the table MEMBER
OR
AccNumber in the table BANK
OR
Any other suitable example
(1 Mark for correct definition/explanation of Primary Key)
(1 Mark for suitable example)

(b) Consider the following tables Consignor and Consignee. Write SQL
commands for the statements (i) to (iv) and give outputs for SQL queries (v)
to (viii). 6
TABLE : CONSIGNOR
CnorlD CnorName CnorAddress City
ND01 R Singhal 24, ABC Enclave New Delhi
ND02 Amit Kumar 123, Palm Avenue New Delhi
MU15 R Kohli 5/A, South Street Mumbai
MU50 S Kaur 27-K, Westend Mumbai

TABLE : CONSIGNEE
CneelD CnorlD CneeName CneeAddress CneeCity
MU05 ND01 Rahul Kishore 5, Park Avenue Mumbai
ND08 ND02 P Dhingra 16/J, Moore Enclave New Delhi
KO19 MU15 A P Roy 2A, Central Avenue Kolkata
MU32 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.’

Page 28 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
(b) (i)
SELECT CnorName FROM CONSIGNOR WHERE
City=’Mumbai’;
(½ Mark for correct use of SELECT and FROM)
(½ Mark for correct use of WHERE clause)

(ii) To display the CneelD, CnorName, CnorAddress, CneeName,


CneeAddress for every Consignee.
(ii)
SELECT B.CneeID, A.CnorName, A.CnorAddress,
B.CneeName , B.CneeAddress
FROM Consignor A, Consignee B
WHERE A.CnorID=B.CnorID;
OR
SELECT Consigner.CneeID, CnorName, CnorAddress,
CneeName, neeAddress
FROM Consignor, Consignee
WHERE Consignor.CnorID= Consignee.CnorID;
(½ Mark for correct use of SELECT and FROM)
(½ Mark for correct use of WHERE clause)

(iii) To display consignee details in ascending order of CneeName.


(iii)
SELECT * FROM CONSIGNEE ORDER BY CneeName;
(½ Mark for correct use of SELECT and FROM)
(½ Mark for correct use of ORDER BY clause)

(iv) To display number of consignors from each city,

(iv)
SELECT City,Count(CnorID) FROM CONSIGNOR Group
By City;
OR
SELECT City,Count(*) FROM CONSIGNOR Group By
City;
(½ Mark for correct use of SELECT and FROM)
(½ Mark for correct use of GROUP BY clause)

(v) SELECT DISTINCT City FROM CONSIGNEE;


(v)
DISTINCT City
Mumbai
New Delhi
Kolkata
(½ Mark for correct output)

Page 29 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
OR
(½ Mark for mentioning Error as CITY not present in the table
CONSIGNEE)

(vi) SELECT A.CnorName, B.CneeName


FROM Consignor A, Consignee B
WHERE A.CnorID = B.CnorlD AND B.CneeCity = ‘Mumbai’;
(vi)
A.CnorName B.CneeName
R Singhal Rahul Kishore
Amit Kumar S Mittal
(½ Mark for correct output)
OR
(½ Mark for any 2 correct output out of (v),(vii)and (viii) even if
part (vi) not attempted)

(vii) SELECT CneeName, CneeAddress


FROM Consignee
WHERE CneeCity NOT IN (‘Mumbai’, ‘Kolkata’);
(vii)
CneeName CneeAddress
P Dhingra 16/J,Moore Enclave
B P Jain 13,Block D,A Vihar
(½ Mark for correct output)

(viii) SELECT CneelD, CneeName


FROM Consignee
WHERE CnorID=’MU15' OR CnorID=’ND01';

(viii)
CneeID CneeName
MU05 Rahul Kishore
KO19 A P Roy
(½ Mark for correct output)
Note: Column Headings for all Outputs may be ignored

YEAR 2008 (OUTSIDE DELHI)

5 a) Differentiate between Candidate Key and Alternate Key in context of


RDBMS. 2
Ans: Candidate Key: It is the one that is capable of becoming primary key i.e., a
column or set of columns that identifies a row uniquely in the relation.
Alternate Key: A candidate key that is not selected as a primary key is called
an Alternate Key.

Page 30 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

(1 Mark each for correct definition/explanation of Candidate Key and


Alternate Key)
OR

(Full 2 Marks for illustrating the concept of Candidate and Alternate key with
appropriate example)
.
(b) 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) 6

TABLE : ITEM

TABLE : CUSTOMER

(i) To display the


details of those
Customers
whose City is
Delhi
Ans:
SELECT * FROM
CUSTOMER
WHERE
City=’Delhi’ ;
(½ Mark for
Page 31 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
correct use of SELECT and FROM)
(½ Mark for correct use of WHERE clause)

(ii) To display the details of Items whose Price is in the range of 35000 to
55000 (Both values included)
Ans:
SELECT * FROM ITEM WHERE PRICE BETWEEN 35000 AND 55000;
OR
SELECT * FROM ITEM WHERE PRICE>=35000 AND PRICE<=55000;
(½ Mark for correct use of SELECT and FROM)
(½ Mark for correct use of WHERE clause)
371

(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 CUSTOMER C, ITEM I WHERE I. I_Id=C.I_ID;
OR
SELECT CustomerName, City, ItemName, Price
FROM CUSTOMER, ITEM WHERE CUSTOMER.I_Id=ITEM.I_ID;
OR
SELECT C. CustomerName, C.City, I.ItemName, I.Price
FROM CUSTOMER C, ITEM I WHERE C.I_Id=I.I_ID;
OR
SELECT CUSTOMER.CustomerName, CUSTOMER.City,
ITEM. ItemName, ITEM. Price
FROM CUSTOMER, ITEM WHERE CUSTOMER.I_Id=ITEM.I_ID;
(½ Mark for correct use of SELECT and FROM)
(½ Mark for correct use of WHERE clause)

(iv) To increase the Price of all Items by 1000 in the table Item

Ans:
UPDATE ITEM SET PRICE=PRICE+1000;
(½ Mark for cbrrect use of UPDATE)
(½ Mark for correct use of SET)

(v) SELECT DISTINCT City FROM Customer;

Ans:
DISTINCT City
Delhi
Mumbai
Bangalore
(½ Mark for correct output - ignore the order of City in the output &

Page 32 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
Column Header)

(vi) SELECT ItemName, MAX(Price), Count(*)


FROM Item GROUP BY ItemName;
Ans:
ItemName Max (Price) Count ( * )
Personal Computer 37000 3
Laptop 57000 2
(½ Mark for correct output - ignore the order of rows in the output & Column
Headers)

(vii) SELECT CustomerName, Manufacturer FROM Item, Customer


WHERE Item.Item_Id=Customer.Item.I_Id
Ans:
CustomerName Manufacturer
N Roy PQR
H Singh XYZ
R Pandey COMP
C Sharma PQR
K Agarwal ABC
(½ Mark for correct output - ignore the order of rows in the output & Column
Headers)
OR
(½ Mark for mentioning syntax error or error as the column Item_ld is not
present)

(viii) SELECT ItemName, Price * 100


FROM Item WHERE Manufacturer =’ABC’;
Ans:
ItemName Price*100
Personal Computer 3500000
Laptop 5500000
(½ Mark for correct output - ignore the order of rows in the output &
Column Headers)

YEAR 2008 (DELHI)

6 (a) Differentiate between Candidate Key and Primary Key in context of RDBMS. 2
Ans. Candidate Key:
All such attributes/columns, which can uniquely identify each

Page 33 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
row/record in a table

Primary Key: An attribute/column among the Candidate Keys which is used


to uniquely identify each row/record in a table

(2 Marks for any valid difference/relation between Candidate Key and Primary
Key)
OR
(1 Mark for correct explanation of Candidate Key)
(1 Mark for correct explanation of Primary Key)

(b) Consider the following tables Product and Client. Write SQL commands for the 6
statement (i) to (iv) and give outputs for SQL queries (v) to (viii)

(i) To display the details of those Clients whose City is Delhi


Ans :
SELECT * FROM Client WHERE City = ‘Delhi’;
(½ Mark for correct SELECT)
(½ Mark for correct WHERE clause)

Page 34 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

(ii) To dis pl ay the details of Products whose Price is in the range of 50 to


100 (Both values included)
Ans:
SELECT * FROM Product WHERE Price >=50 AND Price <=100;
OR
SELECT * FROM Product WHERE Price BETWEEN 50 AND 100;

(½ Mark for correct SELECT)


(½ Mark for correct WHERE clause)

(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, Client.P_ID FROM Client, Product WHERE Client.P_ID = Product.
P_ID;

(½ Mark for correct SELECT)


(½ Mark for correct WHERE clause)

(iv) To increase the Price of all Products by 10


Ans:
UPDATE Product SET Price = Price +10;

(½ Mark for correct SELECT)


(½ Mark for correct WHERE clause)
(v) SELECT DISTINCT Address FROM Client
Ans:
DISTINCT City
Bangalore
Delhi
Mumbai
(½ Mark for correct output)
OR
(½ Mark for mentioning Address is not a Column in the Table Client OR
mentioning ERROR)
(vi) SELECT Manufacturer, MAX(Price), Min(Price), Count(*)
FROM Product GROUP BY Manufacturer;

Ans:
Manufacturer MAX(Price) MIN(Price) Count(*)
ABC 55 45 2
LAK 40 40 1
XYZ 120 95 2
(½ Mark for correct output)
(vii) SELECT ClientName, ManufacturerName FROM Product, Client
WHERE Client.Prod_Id = Product.P_Id;

Page 35 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
Ans:
ClientName Manufacturer
Cosmetic Shop ABC
Total Health ABC
Live life XYZ
Pretty Woman XYZ
Dreams LAK
(½ Mark for correct output)
OR
(½ Mark for mentioning ManufactureName and Prod_ld are not valid Column
in the respective Tables)
(viii) SELECT ProductName, Price * 4 from Product;
Ans:
Product Name Price * 4
Talcom Powder 160
Face Wash 180
Bath Soap 220
Shampoo 480
Face Wash 380
(½ Mark for correct output)
NOTE:
For Parts (v) to (viii), Ignore the Column Header and order of output rows

CBSE SAMPLE PAPER 1 (2009)


(a) What do you understand by Degree and Cardinality of a table? 2

Answer:
Degree of a table is total number of attributes.
Cardinality of a table is total number of rows.
(1 mark for definition of Degree)
(1 mark for definition of Cardinality)

(b) Consider the following tables ACTIVITY and COACH. Write SQL commands for the statements (i) to
(iv) and give outputs for SQL queries (v) to (viii) 6

Table: ACTIVITY
ACode ActivityName ParticipantsNu PrizeMoney ScheduleDate
m
1001 Relay 100x4 16 10000 23-Jan-2004
1002 High jump 10 12000 12-Dec-2003
1003 Shot Put 12 8000 14-Feb-2004
1005 Long Jump 12 9000 01-Jan-2004
1008 Discuss Throw 10 15000 19-Mar-2004

Table: COACH
Page 36 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
PCode Name ACode
1 Ahmad Hussain 1001
2 Ravinder 1008
3 Janila 1001
4 Naaz 1003

(i) To display the name of all activities with their Acodes in descending order.
Answer:
SELECT ActivityName, ACode FROM ACTIVITY ORDER BY Acode DESC;

( ½ mark for correct SELECTion of columns)


( ½ mark for correct use of ORDER BY)

(ii) To display sum of PrizeMoney for each of the Number of participants groupings (as shown in
column ParticipantsNum 10,12,16)
Answer:
SELECT SUM(PrizeMoney),ParticipantsNum FROM ACTIVITY GROUP BY ParticipantsNum;

( ½ mark for correct SELECTion of columns)


( ½ mark for correct use of GROUP BY)

(iii) To display the coach’s name and ACodes in ascending order of ACode from the table COACH
Answer:
SELECT Name, ACode FROM COACH ORDER BY ACode;

( ½ mark for correct SELECTion of columns)


( ½ mark for correct use of ORDER BY)

(iv) To display the content of the ACTIVITY table whose ScheduleDate earlier than 01/01/2004 in
ascending order of ParticipantsNum.
Answer:
SELECT * FROM ACTIVITY WHERE ScheduleDate<’01-Jan-2004’ ORDER BY ParticipantsNum;

( ½ mark for correct SELECTion of columns)


( ½ mark for correct use of ORDER BY)

(i) SELECT COUNT(DISTINCT ParticipantsNum) FROM ACTIVITY;


Answer:
3

( ½ mark for correct output)

(vi)SELECT MAX(ScheduleDate),MIN(ScheduleDate) FROM ACTIVITY;


Answer:
19-Mar-2004 12-Dec-2003

Page 37 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
( ½ mark for correct output)

(vii) SELECT SUM(PrizeMoney) FROM ACTIVITY;


Answer:
54000

( ½ mark for correct output)

(viii) SELECT DISTINCT ParticipantsNum FROM ACTIVITY;


Answer:

16
10
12

( ½ mark for correct output)

CBSE SAMPLE PAPER 2 (2009)

(a) What do you understand by Primary Key & Candidate Keys?


2
Answer:
An attribute or set attributes which are used to identify a tuple uniquely is known as Primary Key. If a
table has more than one such attributes which identify a tuple uniquely than all such attributes are
known as Candidate Keys.

(b) Consider the following tables GAMES and PLAYER. Write SQL commands for the statements (i) to (iv)
and give outputs for SQL queries (v) to (viii) 6

Table: GAMES
GCode GameName Number PrizeMoney ScheduleDate
101 Carom Board 2 5000 23-Jan-2004
102 Badminton 2 12000 12-Dec-2003
103 Table Tennis 4 8000 14-Feb-2004
105 Chess 2 9000 01-Jan-2004
108 Lawn Tennis 4 25000 19-Mar-2004

Table: PLAYER
PCode Name Gcode
1 Nabi Ahmad 101
2 Ravi Sahai 108
3 Jatin 101

Page 38 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
4 Nazneen 103

(i) To display the name of all Games with their Gcodes


Answer:
SELECT GameName,Gcode FROM GAMES;
(1 mark for correct SELECTion of columns)

(ii) To display details of those games which are having PrizeMoney more than 7000.
Answer:
SELECT * FROM GAMES WHERE PrizeMoney>7000
( ½ mark for correct SELECTion of columns)
( ½ mark for correct use of WHERE)

(iii) To display the content of the GAMES table in ascending order of ScheduleDate.
Answer:
SELECT * FROM GAMES ORDER BY ScheduleDate;
( ½ mark for correct SELECTion of columns)
( ½ mark for correct use of ORDER BY)

(i) To display sum of PrizeMoney for each of the Number of participation groupings (as shown in
column Number 2 or 4)
Answer:
SELECT SUM(PrizeMoney),Number FROM GAMES GROUP BY Number;
( ½ mark for correct SELECTion of columns)
( ½ mark for correct use of GROUP BY)

(ii) SELECT COUNT(DISTINCT Number) FROM GAMES;


Answer:
2
( ½ mark for correct output)

(vi)SELECT MAX(ScheduleDate),MIN(ScheduleDate) FROM GAMES;


Answer:
19-Mar-2004 12-Dec-2003
( ½ mark for correct output)

(vii) SELECT SUM(PrizeMoney) FROM GAMES;


Answer:
59000
( ½ mark for correct output)

(viii) SELECT DISTINCT Gcode FROM PLAYER;


Answer:
101
103
108
Page 39 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
( ½ mark for correct output)

Year 2009 (Delhi)


5. (a) What are candidate keys in a table? Give a suitable example of candidate
keys in a table. 2
Ans A table may have more than one such attribute/group of attribute that identifies
a tuple uniquely, all such attribute(s) are known as Candidate Keys.
Table:Item

Ino Item Qty


I01 Pen 560
I02 Pencil 780
I04 CD 450
I09 Floppy 700
I05 Eraser 300
I03 Duster 200

(1 Mark for writing correct definition of Candidate Key)


(1 Mark for giving suitable example)
OR
(2 Marks for illustrating Candidate Key with/without showing it as a part of
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) 6
Table: GARMENT
GCODE DESCRIPTION PRICE FCODE READYDATE
10023 PENCIL SKIRT 1150 F03 19-DEC-08
10001 FORMAL SHIRT 1250 F01 12-JAN-08
10012 INFORMAL SHIRT 1550 F02 06-JUN-08
10024 BABY TOP 750 F03 07-APR-07
10090 TULIP SKIRT 850 F02 31-MAR-07
10019 EVENING GOWN 850 F03 06-JUN-08
10009 INFORMAL PANT 1500 F02 20-OCT-08
10007 FORMAL PANT 1350 F01 09-MAR-08
10020 FROCK 850 F04 09-SEPT-07
10089 SLACKS 750 F02 20-OCT-08

Table : FABRIC

FCODE TYPE
F04 POLYSTER
Page 40 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
F02 COTTON
F03 SILK
F01 TERELENE

(i) To display GCODE and DESCRIPTION of each GARMENT in descending


order of GCODE
Ans SELECT GCODE, DESCRIPTION FROM GARMENT
ORDER BY GCODE DESC;
(1 Mark for correct query)
(½ Mark for partially correct answer)
(ii) To display the details of all the GARMENTs, which have READYDA TE in
between 08-DEC-07 and 16-JUN-08(inclusive of both the dates).
Ans SELECT * FROM GARMENT WHERE READYDATE BETWEEN’ 08-
DEC-07’ AND , 16-JUN-08’ ;
OR
SELECT * FROM DRESS WHERE LAUNCHDATE >= ‘08-DEC-07’
AND LAUNCHDATE<=’16-JUN-08’;
(1 Mark for correct query)
(½ Mark for partially correct answer)
(iii) To display the average PRICE of all the GARMENTs, which are made up of
FABRIC with FCODE as F03.
Ans SELECT AVG (PRICE) FROM GARMENT WHERE FCODE = ‘F03’;
(1 Mark for correct query)
(½ Mark for partially correct answer)
(iv) To display FABRIC wise highest and lowest price of GARMENTs from
GARMENT table. (Display FCODE of each GARMENT along with highest
and lowest price)
Ans SELECT FCODE, MAX (PRICE), MIN(PRICE) FROM GARMENT
GROUP BY FCODE;

(1 Mark for correct query)


(½ Mark for partially correct answer)
(v) SELECT SUM (PRICE) FROM GARMENT WHERE FCODE = ‘F01’ ;
Ans SUM (PRICE)
26:10
(½ Mark for correct output)
(vi) SELECT DESCRIPTION, TYPE FROM GARMENT, FABRIC WHERE
GARMENT.FCODE = FABRIC.FCODE AND GARMENT. PRICE > =
1260 ;
Ans DESCRIPTION TYPE
INFORMAL SHIRT COTTON
INFORMAL PANT COTTON
FORMAL PANT TERELENE
(½ Mark for correct output)
(vii) SELECT MAX (FCODE) FROM FABRIC;
Ans MAX (FCODE)
F04

Page 41 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
(½ Mark for correct output)
(viii) SELECT COUNT (DISTINCT PRICE) FROM GARMENT ;
Ans COUNT(DISTINCT PRICE)
7
(½ Mark for correct output)

Year 2009 (Outside Delhi)

5 (a) What is the purpose of a key in a table? Give an example of a key in a Table. (2)

Ans An attribute/group of attributes in a table that identifies each tuple uniquely is


known as a Key.
OR
Any correct definition of Key / Primary Key / Candidate Key / Alternate Key
Table:Item

Ino Item Qty


I01 Pen 560
I02 Pencil 780
I04 CD 450
I09 Floppy 700
I05 Eraser 300
I03 Duster 200

(1 Mark for writing correct definition/purpose of any valid Key)


(1 Mark for giving suitable example)
OR
(2 Marks for illustrating the purpose of Key with/without showing it as a part
of a Table)

(b) Consider the following tables DRESS and MATERIAL. Write SQL commands for the statements (i) to (iv) and
give outputs for SQL queries (v) to (viii). 6

DCODE DESCRIPTION PRICE MCODE LAUNCHDATE


10001 FORMAL SHIRT 1250 M001 12-JAN-08
10020 FROCK 750 M004 09-SEP-07
10012 INFORMAL SHIRT 1450 M002 06-JUN-08
10019 EVENING GOWN 850 M003 06-JUN-08
10090 TULIP SKIRT 850 M002 31-MAR-07
10023 PENCIL SKIRT 1250 M003 19-DEC-08
10089 SLACKS 850 M002 20-OCT-08
10007 FORMAL PANT 1450 M001 09-MAR-08
10009 INFORMAL PANT 1400 M002 20-OCT-08
10024 BABY TOP 650 M003 07-APR-07

Table: MATERIAL
Page 42 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

MCODE TYPE
MOO1 TERELENE
MOO2 COTTON
MOO4 POLYESTER
MOO3 SILK

(i) To display DCODE and DESCRIPTION of each dress in ascending order of DCODE.
(ii) To display the details of all the dresses which have LAUNCHDATE in between 05-DEC-07 and 20-JUN-08
(inclusive of both the dates).
(iii) To display the average PRICE of all the dresses which are made of material with MCODE as M003
(iv) To display material-wise highest and lowest price of dresses from DRESS table. (Display MCODE of each
dress along with highest and lowest price).
(v) SELECT SUM(PRICE) FROM DRESS WHERE MCODE='M001';
(vi) SELECT DESCRIPTION, TYPE FROM DRESS, MATERIAL WHERE DRESS.MCODE=MATERIAL.MCODE AND
DRESS.PRICE>=1250;
(vii) SELECT MAX(MCODE) FROM MATERIAL;
(viii) SELECT COUNT(DISTINCT PRICE) FROM DRESS;

(i) To display DCODE and DESCRIPTION of each dress in ascending order of DECODE.

SQL> SELECT DCODE, DESCRIPTION FROM DRESS ORDER BY DCODE ASC;


(1 Mark for correct query)
(½ Mark for partially correct answer) :

DCODE DESCRIPTION
----------- ---------------
10001 FORMAL SHIRT
10007 FORMAL PANT
10009 INFORMAL PANT
10012 INFORMAL SHIRT
10019 EVENING GOWN
10020 FROCK
10023 PENCIL SKIRT
10024 BABY TOP
10089 SLACKS
10090 TULIP SKIRT

10 rows selected.

Page 43 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
(ii) To display the details of al the dresses which have LAUNCHDATE in between 05-DEC-07 and 20-JUN-08
(inclusive of both the dates).

SQL> SELECT * FROM DRESS WHERE LAUNCHDATE BETWEEN '05-DEC-07' AND '20-JUN-08';

OR
SELECT * FROM DRESS WHERE LAUNCHDATE >= ‘05-DEC-07’
AND LAUNCHDATE<= ’20-JUN-08’
(1 Mark for correct query)
(½ Mark for partially correct answer)

DCODE DESCRIPTION PRICE MCODE LAUNCHDATE


---------- --------------- ---------- ---- ---------
10001 FORMAL SHIRT 1250 M001 12-JAN-08
10012 INFORMAL SHIRT 1450 M002 06-JUN-08
10019 EVENING GOWN 850 M003 06-JUN-08
10007 FORMAL PANT 1450 M001 09-MAR-08

(iii) To display the average PRICE of all the dresses which are made of material with MCODE as M003.

SQL> SELECT AVG(PRICE) FROM DRESS WHERE MCODE='M003';

(1 Mark for correct query)


(½ Mark for partially correct answer)

AVG(PRICE)
----------
900

(iv) To display material-wise highest and lowest price of dresses from DRESS table. (Display MCODE of each
dress along with highest and lowest price).

Ans SELECT MCODE, MAX(PRICE), MIN (PRICE) FROM DRESS GROUP BY MCODE;
(1 Mark for correct query)
(½ Mark for partially correct answer)

MCOD MAX(PRICE) MIN(PRICE)


---- ---------- ----------
M001 1450 1250
M002 1450 850
M003 1250 650
M004 750 750

SQL> SELECT SUM(PRICE) FROM DRESS WHERE MCODE='M001';

Page 44 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

SUM(PRICE)
----------------
2700
(½ Mark for correct output)

SQL> SELECT DESCRIPTION, TYPE FROM DRESS, MATERIAL WHERE DRESS.MCODE=MATERIAL.MCODE AND
DRESS.PRICE>=1250;

DESCRIPTION TYPE
------------------- ---------
FORMAL SHIRT TERELENE
FORMAL PANT TERELENE
INFORMAL SHIRT COTTON
INFORMAL PANT COTTON
PENCIL SKIRT SILK
(½ Mark for correct output)

SQL> SELECT MAX(MCODE) FROM MATERIAL;

MAX(MCODE)
-------------------
M004
(½ Mark for correct output)

SQL> SELECT COUNT(DISTINCT PRICE) FROM DRESS;

COUNT(DISTINCTPRICE)
----------------------------------
6
(½ Mark for correct output)

SQL> SELECT DISTINCT PRICE FROM DRESS;

PRICE
----------
650
750
850
1250
1400
1450

6 rows selected.

Page 45 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

(½ Mark for correct output)

(vi) SELECT DESCRIPTION, TYPE FROM DRESS, MATERIAL WHERE


DRESS.DCODE = MATERIAL.MCODE AND DRESS.PRICE>=l250;

Ans DESCRIPTION TYPE


(NO OUTPUT)
(½ Mark for the above)
OR
(½ Mark for attempting the question)

Year 2010 (Outside Delhi)

5.(a) What do you understand by Primary Key ? Give a suitable example of Primary Key from a table containing
some meaningful data. 2
Ans An attribute/group of attributes in a table that identifies each tuple uniquely is known as a Primary Key.

Table:Item

Ino Item Qty


I01 Pen 560
I02 Pencil 780
I04 CD 450
I09 Floppy 700
I05 Eraser 300
I03 Duster 200

In above table Ino is the Primary Key.

(1 Mark for writing correct definition/purpose of Primary Key)


(1 Mark for giving suitable example)
OR
(2 Marks for illustrating the purpose of Key with/without showing it as a part
of a Table)

(c) Consider the following tables STOCK and DEALERS and answer (bi) and (b2) parts of this question

Page 46 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

Table: STOCK

ItemNo Item Dcode Qty UnitPrice StockDate


5005 Ball Pen 0.5 102 100 16 31-Mar-10
5003 Ball Pen 0.25 102 150 20 01-Jan-10
5002 Gel Pen Premium 101 125 14 14-Feb-10
5006 Gel Pen Classic 101 200 22 01-Jan-09
5001 Eraser Small 102 210 5 19-Mar-09
5004 Eraser Big 102 60 10 12-Dec-09
5009 Sharpener Classic 103 160 8 23-Jan-09

Table: DEALERS

Dcode Dname
101 Reliable Stationers
103 Classic Plastics
102 Clear Deals

(bl) Write,SQL commands for the following statements: 4


(i) To display details of all Items in the Stock table in ascending order of StockDate.

Ans:
SQL> SELECT * FROM Stock ORDER BY StockDate ASC;

ITEMNO ITEM DCODE QTY UNITPRICE STOCKDATE


---------- -------------------- ---------- ---------- ---------- ---------
5006 Gel Pen Classic 101 200 22 01-JAN-09
5009 Sharpener Classic 103 160 8 23-JAN-09
5001 Eraser Small 102 210 5 19-MAR-09
5004 Eraser Big 102 60 10 12-DEC-09
5003 Ball Pen 0.25 102 150 20 01-JAN-10
5002 Gel Pen Premium 101 125 14 14-FEB-10
5005 Ball Pen 0.5 102 100 16 31-MAR-10

7 rows selected.
Page 47 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

(ii) To display ItemNo and Item name of those items from Stock table whose UnitPrice is more than Rupees 10.

Ans.:
SQL> SELECT ItemNo, Item FROM Stock WHERE UnitPrice>10;

ITEMNO ITEM
---------- --------------------
5005 Ball Pen 0.5
5003 Ball Pen 0.25
5002 Gel Pen Premium
5006 Gel Pen Classic

(iii) To display the details of those items whose dealer code (Dcode) is 102 or Quantity in Stock (Qty) is more
than 100 from the table Stock.
Ans.:
SQL> SELECT * FROM Stock WHERE Dcode=102 OR Qty>100 ;

ITEMNO ITEM DCODE QTY UNITPRICE STOCKDATE


---------- -------------------- ---------- ---------- ---------- ---------
5005 Ball Pen 0.5 102 100 16 31-MAR-10
5003 Ball Pen 0.25 102 150 20 01-JAN-10
5002 Gel Pen Premium 101 125 14 14-FEB-10
5006 Gel Pen Classic 101 200 22 01-JAN-09
5001 Eraser Small 102 210 5 19-MAR-09
5004 Eraser Big 102 60 10 12-DEC-09
5009 Sharpener Classic 103 160 8 23-JAN-09

7 rows selected.

(iv) To display Maximum UnitPrice of items for each dealer individually as per Dcode from the table Stock.

Ans.:
SQL> SELECT Dcode, MAX(UnitPrice) FROM Stock GROUP BY Dcode;

DCODE MAX(UNITPRICE)
---------- --------------
101 22
102 20
103 8

(b2) Give the output of the following SQL queries 2


(i) SELECT COUNT (DISTINCT Dcode) FROM Stock;

Ans.:
COUNT(DISTINCTDCODE)
---------------------

Page 48 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
3

(ii) SELECT Qty*UnitPrice FROM Stock WHERE ItemNo=5006;

Ans.:
QTY*UNITPRICE
-------------
4400

(iii) SELECT Item, Dname FROM Stock S, Dealers D WHERE S.Dcode=D.Dcode AND ItemNo=5004;

Ans.:
ITEM DNAME
-------------------- --------------------
Eraser Big Clear Deals

(iv) SELECT MIN(StockDate) FROM Stock;

Ans.:
MIN(STOCKDATE)
---------
01-JAN-09

Q: Write a query to display the total unit price, average unit price, the highest unit price, the lowest unit price,
number of unit prices.

Ans.:
SELECT sum(UNITPRICE), avg(UNITPRICE),max(UNITPRICE), min(UNITPRICE), count(*) from STOCK;

***************

CBSE 2011

5 (a) What do you understand by Selection and Projection Operations in relational algebra?

Ans.
Select Operation (σ) Project Operation (∏)
Selection in relational algebra returns Projection in relational algebra returns
those tuples(records) in a relation that those columns in a relation that given in
fulfil a condition / predicate (Produce the attribute list (Produce table containing

Page 49 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
table containing subset of rows). subset of columns).
Notation − σp(r) Notation − ∏A1, A2, An (r)
Where σ stands for selection predicate Where A1, A2 , An are attribute
and r stands for relation. p is names of relation r.
prepositional logic formula which may Duplicate rows are
use connectors like and, or, and not. automatically eliminated, as relation
These terms may use relational is a set.
operators like − =, ≠, ≥, < , >, ≤.

For example − For example −


σ subject = "database"(Books) ∏subject, author (Books)
Output − Selects tuples from books Selects and projects columns
where subject is 'database'. named as subject and author from the
σsubject = "database" and price = "450"(Books) relation Books.

Output − Selects tuples from books


where subject is 'database' and 'price' is
450.
σsubject = "database" and price = "450" or year >
"2010"(Books)

Output − Selects tuples from books


where subject is 'database' and 'price' is
450 or those books published after 2010.

Consider the following tables EMPLOYEES and SALGRAD and answer (b) and (c) parts of
this question:
Tables EMPLOYEES
ECODE NAME DESIGN SGRADE DOJ DOB
101 Abdul Ahmed EXECUTIVE S03 2003-03-23 1980-01-13
102 Ravi Chander Head-IT S02 2010-02-12 1987-07-22
103 John Ken RECEPTIONIST S03 2009-06-24 1983-02-24
105 Nazar Ameen GM S02 2006-08-11 1984-03-03
108 Priyam Sen CEO S01 2004-12-29 1982-01-19

Tables SALGRAD
SGRADE SALARY HRA
S01 56000 18000
S02 32000 12000
S03 24000 8000

5 (b) Write SQL commands for the following statements: 4


(i) To display the details of all EMPLOYEEs in descending order of DOJ.

Page 50 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
(ii) To display NAME and DESIGN of those EMPLOYEEs, whose SGRADE is either S02 or
S03.

(iii) To display the content of all employees from the EMPLOYEEs table, whose DOJ is in
between ’09-Feb-2006’ and ’08-Aug-2009’.

(iv) To add a new row with the following:


109, ‘Harish Roy’, ‘HEAD-IT’, ‘S02’, '2007-09-09','1983-04-21'

Tables EMPLOYEES
ECODE NAME DESIGN SGRADE DOJ DOB
101 Abdul EXECUTIVE S03 2003-03-23 1980-01-13
Ahmed
102 Ravi Head-IT S02 2010-02-12 1987-07-22
Chander
103 John Ken RECEPTIONIST S03 2009-06-24 1983-02-24
105 Nazar GM S02 2006-08-11 1984-03-03
Ameen
108 Priyam Sen CEO S01 2004-12-29 1982-01-19

Tables SALGRAD
SGRADE SALARY HRA
S01 56000 18000
S02 32000 12000
S03 24000 8000

5 (c) Give the output of the following SQL queries:

(i) SELECT COUNT(SGRADE), SGRADE FROM EMPLOYEES GROUP BY SGRADE;

(ii) SELECT MIN(DOB), MAX(DOJ) FROM EMPLOYEES;

(iii) SELECT NAME, SALARY FROM EMPLOYEES E, SALGRAD S WHERE


E.SGRADE=S.SGRADE AND E.ECODE<103;

Page 51 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
(iv) SELECT SGRADE, SALARY+HRA FROM SALGRAD WHERE SGRADE='S02';

Ans. (i) SELECT * FROM EMPLOYEES ORDER BY DOJ DESC;

(ii) SQL> SELECT NAME, DESIGN FROM EMPLOYEES WHERE SGRADE='S02' OR


SGRADE='S03';

(iii) mysql> SELECT * FROM EMPLOYEES WHERE DOJ BETWEEN '2006-02-09' AND
'2009-08-08';

(iv) SQL> INSERT INTO EMPLOYEES VALUES(109, 'Harish Roy', 'HEAD-IT', 'S02', '2007-
09-09','1983-04-21');

5 (c) Give the output of the following SQL queries: 2

(i) SELECT COUNT(SGRADE), SGRADE FROM EMPLOYEES GROUP BY SGRADE;

(ii) SELECT MIN(DOB), MAX(DOJ) FROM EMPLOYEES;

(iii) SELECT NAME, SALARY FROM EMPLOYEES E, SALGRAD S WHERE


E.SGRADE=S.SGRADE AND E.ECODE<103;

(iv) SELECT SGRADE, SALARY+HRA FROM SALGRAD WHERE SGRADE='S02';

Ans. (i) SQL> SELECT COUNT(SGRADE), SGRADE FROM EMPLOYEES GROUP BY SGRADE;
COUNT(SGRADE) SGRADE
------------- ---------
2 S03
2 S02
1 S01
Page 52 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
(ii) SQL> SELECT MIN(DOB), MAX(DOJ) FROM EMPLOYEES;
+-----------------+----------------+
| MIN(DOB) | MAX(DOJ) |
+-----------------+----------------+
| 1980-01-13 | 2010-02-12 |
+-----------------+----------------+
(iii) SQL> SELECT NAME, SALARY FROM EMPLOYEES E, SALGRAD S WHERE
E.SGRADE=S.SGRADE AND E.ECODE<103;
+-------------------+-------------+
| NAME | SALARY |
+-------------------+-------------+
| Ravi Chander | 32000.00 |
| Abdul Ahmed | 24000.00 |
+--------------+----------+
(iv) SQL> SELECT SGRADE, SALARY+HRA FROM SALGRAD WHERE SGRADE='S02';
+------------+-------------------+
| SGRADE | SALARY+HRA |
+------------+-------------------+
| S02 | 44000.00 |
+------------+------------------+

CBSE 2012

5 (a) Give a suitable example of a table with sample data and illustrate Primary and Alternate 2
Keys in it.

Ans. Table/Relation: In database, a relation means a 'table', in which data are organized in the
form of rows and columns. Therefore in database, relations are equivalent to tables.

Consider the following table Student

Page 53 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

Table/Relation: Student

Primary Key: A column or set of columns that uniquely identifies a row within a table is
called primary key. In table Student: SID is the Primary Key.
Candidate Key: Candidate keys are set of fields (columns with unique values) in the
relation that are eligible to act as a primary key.
Alternate Key: Out of the candidate keys, after selecting a key as primary key, the
remaining keys are called alternate key. In above table SName can be Alternate Key.

Page 54 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

Consider the following tables CARDEN and CUSTOMER and answer (b) and (c) parts of
this question:
Tables CARDEN
Ccode CarName Make Color Capacity Charges
501 A-STAR SUZUKI RED 3 14
503 INDIGO TATA SILVER 3 12
502 INNOVA TOYATA WHITE 7 15
509 SX4 SUZUKI SILVER 4 14
510 C CLASS MERCEDES RED 4 35

Tables CUSTOMER
CODE CNAME CCODE
1001 Hemant Sahu 501
1002 Raj Lal 509
1003 Feroza Shah 503
1004 Ketan Dhal 502
5 (b) Write SQL commands for the following statements: 4

(i) To display the names of all the silver colored Cars.

(ii) To display name of car, make and capacity of cars in descending order of their
sitting capacity.

(iii) To display the highest charges at which a vehicle can be hired from CARDEN.

(iv) To display the customer name and the corresponding name of the cars hired by
them.

Page 55 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

Tables CARDEN
Ccode CarName Make Color Capacity Charges
501 A-STAR SUZUKI RED 3 14
503 INDIGO TATA SILVER 3 12
502 INNOVA TOYATA WHITE 7 15
509 SX4 SUZUKI SILVER 4 14
510 C CLASS MERCEDES RED 4 35

Tables CUSTOMER
CODE CNAME CCODE
1001 Hemant Sahu 501
1002 Raj Lal 509
1003 Feroza Shah 503
1004 Ketan Dhal 502
5 (c) Give the output of the following SQL queries:
(i) SELECT COUNT(DISTINCT MAKE) FROM CARDEN;

(ii) SELECT MAX(CHARGES), MIN(CHARGES) FROM CARDEN;

(iii) SELECT COUNT(*), Make FROM CARDEN;

SELECT COUNT(*) , MAKE FROM CARDEN GROUP BY MAKE;

(iv) SELECT CARNAME FROM CARDEN WHERE CAPACITY=4;

Ans. (i) SQL> SELECT CARNAME FROM CARDEN WHERE COLOR='SILVER';

Page 56 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
CARNAME
----------
INDIGO
SX4

(ii) SQL> SELECT CARNAME, MAKE, CAPACITY FROM CARDEN ORDER BY CAPACITY
DESC;

CARNAME MAKE CAPACITY


---------- ---------- - ---------
INNOVA TOYATA 7
C CLASS MERCEDES 4
SX4 SUZUKI 4
INDIGO TATA 3
A-STAR SUZUKI 3

(iii) SQL> SELECT MAX(CHARGES) FROM CARDEN;

MAX(CHARGES)
------------
35

(iv) SQL> SELECT CNAME, CARNAME FROM CUSTOMER R, CARDEN N WHERE


R.CCODE=N.CCODE;

CNAME CARNAME
------------ ----------
Hemant Sahu A-STAR
Feroza Shah INDIGO
Ketan Dhal INNOVA
Raj Lal SX4

5 (c) Give the output of the following SQL queries:


(i) SELECT COUNT(DISTINCT MAKE) FROM CARDEN;

+----------------------------------+
| COUNT(DISTINCT MAKE) |
+-----------------------------------+
| 4 |
+----------------------------------+
(ii) SELECT MAX(CHARGES), MIN(CHARGES) FROM CARDEN;

+-----------------------+---------------------+
| MAX(CHARGES) | MIN(CHARGES) |
+-----------------------+----------------------+
| 35 | 12 |
+-----------------------+-----------------------+
Page 57 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
(iii) SELECT COUNT(*), Make FROM CARDEN;
Will give error as follows:
ERROR at line 1:
ORA-00937: not a single-group group function
Following is the output in MySQL (But it is not meaningful.)
+---------------+----------+
| COUNT(*) | Make |
+---------------+----------+
| 5 | SUZUKI |
+---------------+-----------+
The corrected question can be either :
SELECT COUNT(*) FROM CARDEN;
OR

SELECT COUNT(*) , MAKE FROM CARDEN GROUP BY MAKE;

+---------------+---------------+
| COUNT(*) | MAKE |
+---------------+---------------+
| 2 | SUZUKI |
| 1 | TATA |
| 1 | TOYOTA |
| 1 | MERCEDES |
+---------------+---------------+
(iv) SELECT CARNAME FROM CARDEN WHERE CAPACITY=4;

+---------------+
| CARNAME |
+---------------+
| SX4 |
| C CLASS |
+--------------+

Ans. (i) SQL> SELECT COUNT(DISTINCT MAKE) FROM CARDEN;

COUNT(DISTINCTMAKE)
-------------------
4

(ii) SQL> SELECT MAX(CHARGES), MIN(CHARGES) FROM CARDEN;

MAX(CHARGES) MIN(CHARGES)
------------ ------------
35 12

Page 58 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

(iii) SQL> SELECT COUNT(*) FROM CARDEN;

COUNT(*)
----------
5
Or

SQL> SELECT COUNT(*) , MAKE FROM CARDEN GROUP BY MAKE;

COUNT(*) MAKE
---------- ----------
2 SUZUKI
1 TATA
1 MERCEDES
1 TOYATA

(iv) SQL> SELECT CARNAME FROM CARDEN WHERE CAPACITY=4;

CARNAME
----------
SX4
C CLASS

CBSE 2013

5 (a) Explain the concept of Candidate keys with the help of an appropriate example. 2

Ans. Candidate Key: Candidate keys are set of fields (columns with unique values) in the
relation that are eligible to act as a primary key. There can be multiple Candidate Keys in
one table.
In the following table SID and SName can be termed as Candidate keys.

Page 59 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

Table/Relation: Student

Write SQL queries for (b) to (g) and write the output for the SQL queries mentioned shown in
(h1) to (h4) parts on the basis of tables PRODUCTS and SUPPLIERS.
Tables PRODUCTS
PID PNAME QTY PRICE COMPANY SUOCODE
101 DIGITAL CAMERA 14X 120 12000 RENIX S01
102 DIGITAL PAD 11i 100 22000 DIGI POP S02
104 PEN DRIVE 16 GB 500 1100 STOREKING S01
106 LED SCREEN 70 28000 DISPEXPERTS S02
105 CAR GPS SYSTEM 60 12000 MOVEON S03
Tables SUPPLIERS
SUPCODE SNAME CITY
S01 GEL ALL INC KOLKATA
S03 EASY MARKET CORP DELHI
S02 DIGI BUSY GROUP CHENNAI
Page 60 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

5 (b) To display the details of all the products in ascending order of the product name (i.e. 1
PNAME).

(c) To display product name and price of all those products, whose price is in the range of 1
10000 and 15000 (both values inclusive).

(d) To display the number of products, which are supplied by each supplier i.e. the expected 1
output should be:
S01 2
S02 2
S03 1

(e) To display the price, product name and quantity (i.e. QTY) of those products which have 1
quantity more than 100.

(f) To display the names of those suppliers, who are either from DELHI or from CHENNAI. 1

(g) To display the name of the companies and the name of the products in descending order 1
of company names.

Tables PRODUCTS
PID PNAME QTY PRICE COMPANY SUOCODE
101 DIGITAL CAMERA 14X 120 12000 RENIX S01
102 DIGITAL PAD 11i 100 22000 DIGI POP S02
104 PEN DRIVE 16 GB 500 1100 STOREKING S01
106 LED SCREEN 70 28000 DISPEXPERTS S02
105 CAR GPS SYSTEM 60 12000 MOVEON S03
Tables SUPPLIERS
SUPCODE SNAME CITY
S01 GEL ALL INC KOLKATA
S03 EASY MARKET CORP DELHI
S02 DIGI BUSY GROUP CHENNAI
5 h) Obtain the outputs of the following SQL queries based on the data given in tables
PRODUCTS and SUPPLIERS above.
(h1) SELECT DISTINCT SUPCODE FROM PRODUCTS;

(h2) SELECT MAX(PRICE), MIN(PRICE) FROM PRODUCTS;

(h3) SELECT PRICE*QTY AMOUNT FROM PRODUCTS WHERE PID=104;

(h4) SELECT PNAME, SNAME FROM PRODUCTS P, SUPPLIERS S WHERE


P.SUPCODE=S.SUPCODE AND QTY>100;

Page 61 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

Ans. (b) SQL> SELECT * FROM PRODUCTS ORDER BY PNAME ASC;

PID PNAME QTY PRICE COMPANY SUPCODE


------ ------------------------- ------- ---------- -------------------- -------
105 CAR GPS SYSTEM 60 12000 MOVEON S03
101 DIGITAL CAMERA 14X 120 12000 RENIX S01
102 DIGITAL PAD 11i 100 22000 DIGI POP S02
106 LED SCREEN 70 28000 DISPEXPERTS S02
104 PEN DRIVE 16 GB 500 11000 STOREKING S01

(c) SQL> SELECT PNAME, PRICE FROM PRODUCTS WHERE PRICE BETWEEN 10000 AND
15000;

PNAME PRICE
------------------------- ----------
DIGITAL CAMERA 14X 12000
PEN DRIVE 16 GB 11000
CAR GPS SYSTEM 12000

(d) SQL> SELECT SUPCODE, COUNT(*) FROM PRODUCTS GROUP BY SUPCODE;

SUPCODE COUNT(*)
------- ----------
S02 2
S01 2
S03 1

(e) SQL> SELECT PRICE, PNAME, QTY FROM PRODUCTS WHERE QTY>100;

PRICE PNAME QTY


---------- ------------------------- ----------
12000 DIGITAL CAMERA 14X 120
11000 PEN DRIVE 16 GB 500

(f) SQL> SELECT SNAME FROM SUPPLIERS WHERE CITY IN ('DELHI', 'CHENNAI');

SNAME
-------------------------
EASY MARKET CORP
DIGI BUSY GROUP

Page 62 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
OR

SQL> SELECT SNAME FROM SUPPLIERS WHERE CITY ='DELHI' OR CITY ='CHENNAI';

SNAME
-------------------------
EASY MARKET CORP
DIGI BUSY GROUP

(g) SQL> SELECT COMPANY, PNAME FROM PRODUCTS ORDER BY COMPANY DESC;

COMPANY PNAME
-------------------- -------------------------
STOREKING PEN DRIVE 16 GB
RENIX DIGITAL CAMERA 14X
MOVEON CAR GPS SYSTEM
DISPEXPERTS LED SCREEN
DIGI POP DIGITAL PAD 11i

5 (h) Obtain the outputs of the following SQL queries based on the data given in tables 2
PRODUCTS and SUPPLIERS above.
(h1) SELECT DISTINCT SUPCODE FROM PRODUCTS;

(h2) SELECT MAX(PRICE), MIN(PRICE) FROM PRODUCTS;

(h3) SELECT PRICE*QTY AMOUNT FROM PRODUCTS WHERE PID=104;

(h4) SELECT PNAME, SNAME FROM PRODUCTS P, SUPPLIERS S WHERE


P.SUPCODE=S.SUPCODE AND QTY>100;

Ans. (h1) SQL> SELECT DISTINCT SUPCODE FROM PRODUCTS;

SUPCODE
-------
S02
S01
S03

(h2) SQL> SELECT MAX(PRICE), MIN(PRICE) FROM PRODUCTS;

MAX(PRICE) MIN(PRICE)
---------- ----------
28000 11000

(h3) SQL> SELECT PRICE*QTY AMOUNT FROM PRODUCTS WHERE PID=104;

Page 63 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
AMOUNT
----------
5500000

(h4) SQL> SELECT PNAME, SNAME FROM PRODUCTS P, SUPPLIERS S WHERE


P.SUPCODE=S.SUPCODE AND QTY>100;

PNAME SNAME
------------------------- -------------------------
DIGITAL CAMERA 14X GEL ALL INC
PEN DRIVE 16 GB GEL ALL INC

CBSE 2014

5 (a) Explain the concept of Cartesian Product between two tables, with the help of
appropriate example.

Ans. Cartesian product is a binary operation and is denoted by (x) Cartesian


product returns a number of rows equal to number of rows in the first table multiply by
number of rows in the second table. At the same time, number of columns equal to
number of columns in the first table added by number of columns in the second table.

Example 1:

Example 2:
Say, table A has three numbers

Numbers
1
2
3

And table B has three letters :


Letters

Page 64 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
E
F
G

The Cartesian product of the two table (A X B) will be the following rows :
Numbers Letters
1 E
1 F
1 G
2 E
2 F
2 G
3 E
3 F
3 G

NOTE: Answer the following questions (b) and (c) on the basis of the following tables SHOPPE
and ACCESSORIES.
Table: SHOPPE
ID SNAME AREA
S01 ABC COMPUTRONICS CP
S02 ALL INFOTECH MEDIA GK II
S03 TECH SHOPPE CP
S04 GEEKS TECHNO SOFT NEHRU PLACE
S05 HIGHTECH TECH STORE NEHRU PLACE
Table: ACCESSORIES
NO NAME PRICE ID
A01 MOTHER BOARD 12000 S01
A02 HARD DISK 5000 S01
A03 KEY BOARD 500 S02
A04 MOUSE 300 S01
A05 MOTHER BOARD 13000 S02
A06 KEY BOARD 400 S03
A07 LCD 6000 S04
T08 LCD 5500 S05
T09 MOUSE 350 S05
T10 HARD DISK 4500 S03

To display Name, Price of all Accessories and their respective SNAME where they are
available.

Page 65 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
5 (b) Write the SQL queries: 4
(i) To display Name and Price of all the Accessories in ascending order of their Price.

(ii) To display ID and SNAME of all Shoppe located in Nehru Place.

(iii) To display Minimum and Maximum Price of each Name of Accessories.

(iv) To display Name, Price of all Accessories and their respective SNAME where they
are available.

Ans. (i) SQL> SELECT NAME, PRICE FROM ACCESSORIES ORDER BY PRICE ASC;
NAME PRICE
-------------------- ----------
MOUSE 300
MOUSE 350
KEY BOARD 400
KEY BOARD 500
HARD DISK 4500
HARD DISK 5000
LCD 5500
LCD 6000
MOTHER BOARD 12000
MOTHER BOARD 13000

(ii) SQL> SELECT ID, SNAME FROM SHOPPE WHERE AREA='NEHRU PLACE';

ID SNAME
---- --------------------
S04 GEEKS TECHNO SOFT
S05 HIGHTECH TECH STORE

(iii) SQL> SELECT NAME, MIN(PRICE), MAX(PRICE) FROM ACCESSORIES GROUP BY


NAME;

NAME MIN(PRICE) MAX(PRICE)


-------------------- ---------- ----------
HARD DISK 4500 5000
KEY BOARD 400 500
LCD 5500 6000
MOUSE 300 350
MOTHER BOARD 12000 13000

(iv) SQL> SELECT NAME,PRICE, SNAME FROM ACCESSORIES A, SHOPPE S WHERE


A.ID=S.ID;
Or
SELECT NAME,PRICE, SNAME FROM ACCESSORIES , SHOPPE WHERE
ACCESSORIES.ID= SHOPPE.ID;
Page 66 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

NAME PRICE SNAME


-------------------- ---------- --------------------
MOTHER BOARD 12000 ABC COMPUTRONICS
HARD DISK 5000 ABC COMPUTRONICS
KEY BOARD 500 ALL INFOTECH MEDIA
MOUSE 300 ABC COMPUTRONICS
MOTHER BOARD 13000 ALL INFOTECH MEDIA
KEY BOARD 400 TECH SHOPPE
LCD 6000 GEEKS TECHNO SOFT
LCD 5500 HIGHTECH TECH STORE
MOUSE 350 HIGHTECH TECH STORE
HARD DISK 4500 TECH SHOPPE

5 (c) Write the output of the following SQL commands: 2

(i) SELECT DISTINCT NAME FROM ACCESSORIES WHERE PRICE>=5000;

(ii) SELECT AREA, COUNT(*) FROM SHOPPE GROUP BY AREA;

(iii) SELECT COUNT(DISTINCT AREA) FROM SHOPPE;

(iv) SELECT NAME, PRICE*0.05 DISCOUNT FROM ACCESSORIES WHERE ID IN


('S02','S03');

Ans. (i) SQL> SELECT DISTINCT NAME FROM ACCESSORIES WHERE PRICE>=5000;

NAME
--------------------
HARD DISK
LCD
MOTHER BOARD

(ii) SQL> SELECT AREA, COUNT(*) FROM SHOPPE GROUP BY AREA;

AREA COUNT(*)
------------ ----------
GK II 1
CP 2
NEHRU PLACE 2

(iii) SQL> SELECT COUNT(DISTINCT AREA) FROM SHOPPE;

COUNT(DISTINCTAREA)
-------------------
3

Page 67 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
(iv) SQL> SELECT NAME, PRICE*0.05 DISCOUNT FROM ACCESSORIES WHERE ID IN
('S02','S03');

NAME DISCOUNT
-------------------- ----------
KEY BOARD 25
MOTHER BOARD 650
KEY BOARD 20
HARD DISK 225

CBSE 2015

5 (a) Observe the following table carefully and write the names of the most appropriate 2
columns, which can be considered as (i) Candidate Keys and (ii) Primary key:

Code Item Qty Price Transaction_Date


1001 Plastic Folder 14’’ 100 3400 2014-12-14
1004 Pen Stand Standard 200 4500 2015-01-31
1005 Stapler Mini 250 1200 2015-02-28
1009 Punching Machine Small 200 1400 2015-03-12
1004 Stapler Big 100 1500 2015-02-02
Ans. (i)Candidate Keys: Code, Item, Transaction_Date
(ii) Primary Key: Code
5 (b) Consider the following DEPTS and EMPLOYEES tables. Write SQL queries for (i) to (iv) and 6
find outputs for SQL queries (v) to (viii).

Table: DEPTS
DCODE DEPARTMENT LOCACTION
D01 INFRASTRUCTURE DELHI
D02 MARKETING DELHI
D03 MEDIA MUMBAI
D05 FINANCE KOLKATA
D04 HUMAN RESOURCE MUMBAI
Table: EMPLOYEES
ENO NAME DOJ DOB GENDER DCODE
1001 George K 02-SEP-2013 01-SEP-1991 MALE D01
1002 Ryma Sen 11-DEC-2012 15-DEC-1990 FEMALE D03
1003 Mohitesh 03-FEB-2013 04-SEP-1987 MALE D05
1007 Anil Jha 17-JAN-2014 19-OCT-1984 MALE D04
1004 Manila Sahai 09-DEC-2012 14-NOV-1986 FEMALE D01
1005 R SAHAY 18-NOV-2013 31-MAR-1987 MALE D02
1006 Jaya Priya 09-JUN-2014 23-JUN-1985 FEMALE D05

Note: DOJ refers to date of joining and DOB refers to date of Birth of employees.
SELECT MAX(DOJ), MIN(DOB) FROM EMPLOYEES;

Page 68 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

(i) To display Eno, Name, Gender from the table EMPLOYEES in ascending order of
Eno.
(ii) To display the Name of all the MALE employees from the table EMPLOYEES.
(iii) To display the Eno and Name of those employees from the table EMPLOYEES who
are born between ’01-Jan-1987’ and ’01-Dec-1991’.

(iv) To count and display FEMALE employees who have joined after ’01-Jan-1986’.

(v) SELECT COUNT(*), DCODE FROM EMPLOYEES GROUP BY DCODE HAVING


COUNT(*)>1;

(vi) SELECT DISTINCT DEPARTMENT FROM DEPTS;

(vii) SELECT NAME, DEPARTMENT FROM EMPLOYEES E, DEPTS D WHERE


E.DCODE=D.DCODE AND ENO<1003;

(viii) SELECT MAX(DOJ), MIN(DOB) FROM EMPLOYEES;

Ans. (i) SQL> SELECT ENO, NAME, GENDER FROM EMPLOYEES ORDER BY ENO ASC;

ENO NAME GENDER


---------- --------------- --------
1001 George K MALE
1002 Ryma Sen FEMALE
1003 Mohitesh MALE
1004 Manila Sahai FEMALE
1005 R SAHAY MALE
1006 Jaya Priya FEMALE
1007 Anil Jha MALE

(ii) SQL> SELECT NAME FROM EMPLOYEES WHERE GENDER='MALE';

NAME
---------------
George K
Mohitesh
Anil Jha
R SAHAY

(iii) SQL> SELECT ENO,NAME FROM EMPLOYEES WHERE DOB BETWEEN '01-JAN-1987'
AND '01-DEC-1991';

ENO NAME
---------- ---------------
1001 George K
Page 69 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
1002 Ryma Sen
1003 Mohitesh
1005 R SAHAY

(iv) SQL> SELECT COUNT(*) FROM EMPLOYEES WHERE GENDER='FEMALE' AND


DOJ>'01-JAN-1986';

COUNT(*)
----------
3

(v) SQL> SELECT COUNT(*), DCODE FROM EMPLOYEES GROUP BY DCODE HAVING
COUNT(*)>1;

COUNT(*) DCODE
---------- -----
2 D01
2 D05

(vi) SQL> SELECT DISTINCT DEPARTMENT FROM DEPTS;

DEPARTMENT
----------------
HUMAN RESOURCE
INFRASTRUCTURE
MARKETING
FINANCE
MEDIA

(vii) SQL> SELECT NAME, DEPARTMENT FROM EMPLOYEES E, DEPTS D WHERE


E.DCODE=D.DCODE AND ENO<1003;

NAME DEPARTMENT
--------------- ----------------
George K INFRASTRUCTURE
Ryma Sen MEDIA

(viii) SQL> SELECT MAX(DOJ), MIN(DOB) FROM EMPLOYEES;

MAX(DOJ) MIN(DOB)
--------- ---------
09-JUN-14 19-OCT-84

CBSE 2016

Page 70 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

5 (a) Observe the following PARTICIPANTS and EVENTS tables carefully and write the name of 2
the RDBMS operation which will be used to produce the output as shown in RESULTS?
Also, find the Degree and Cardinality of the result.
Table: PARTICIPANTS Table: EVENTS
PNO NAME EVENTCODE EVENTNAME
1 Aruanabha Tariban 1001 IT Quiz
2 John Fedricks 1002 Group Debate
3 Kanti Desai

Table: RESULT
PNO NAME EVENTCODE EVENTNAME
1 Aruanabha Tariban 1001 IT Quiz
1 Aruanabha Tariban 1002 Group Debate
2 John Fedricks 1001 IT Quiz
2 John Fedricks 1002 Group Debate
3 Kanti Desai 1001 IT Quiz
3 Kanti Desai 1002 Group Debate

Ans. The name of the RDBMS operation is Cartesian Product.

The Degree of the RESULT table is sum of degree of two tables and Cardinality is product
of cardinality of two tables.

Degree=4
Cardinality=6

5 (b) Write SQL queries for (i) to (iv) and find outputs for SQL queries (v) to (viii), which is based 6
on the following two tables VEHICLE and TRAVEL:

Table: VEHICLE
VCODE VEHICLETYPE PERKM
V01 VOLVO BUS 150
V02 AC DELUX BUS 125
V03 ORDINARY BUS 80
V05 SUV 30
V04 CAR 18
Table: TRAVEL
CN0 CNAME TRAVELDATE KM VCODE NOP
101 K Niwal 13-DEC-2015 200 V01 32
103 Fredrick Sym 21-MAR-2016 120 V03 45
105 Hitesh Jain 23-APR-2016 450 V02 42
102 Ravi Anish 13-JAN-2016 80 V02 40
107 John Malina 10-FEB-2015 65 V04 2
104 Sahanubhuti 28-JAN-2016 90 V05 4

Page 71 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
106 Ramesh Jaya 06-APR-2016 100 V01 25

Note: - Km is Kilometers travelled


- NOP is number of passengers travelled in vehicle.

SQL> SELECT CNAME, KM*PERKM FROM TRAVEL A, VEHICLE B


WHERE A.VCODE=B.VCODE AND A.VCODE='V05';

(i) To display CNO, CNAME, TRAVELDAT from the table TRAVEL in ascending order of
CNO.

(ii) To display the CNAMEof all the customers from the table TRAVEL who are traveling
by vehicle with code V01 or V02.

(iii) To display the CNO and CNAME of those customers from the table TRAVEL who
travelled between ’31-Dec-2015’ and ’01-May-2016’.

(iv) To display all the details from table TRAVEL for the customers, who have travel
distance more than 120 KM in ascending order od NOP.

(v) SQL> SELECT COUNT(*), VCODE FROM TRAVEL GROUP BY VCODE HAVING
COUNT(*)>1;

(vi) SQL> SELECT DISTINCT VCODE FROM TRAVEL;

(vii) SQL> SELECT A.VCODE,CNAME,VEHICLETYPE


2 FROM TRAVEL A, VEHICLE B
3 WHERE A.VCODE=B.VCODE AND KM<90;

(viii) SQL> SELECT CNAME, KM*PERKM


2 FROM TRAVEL A, VEHICLE B
3 WHERE A.VCODE=B.VCODE AND A.VCODE='V05';

Ans. (i) SQL> SELECT CNO, CNAME, TRAVELDATE FROM TRAVEL ORDER BY CNO ASC;

CNO CNAME TRAVELDAT


---------- --------------- ---------
101 K Niwal 13-DEC-15
102 Ravi Anish 13-JAN-16
103 Fredrick Sym 21-MAR-16
104 Sahanubhuti 28-JAN-16
105 Hitesh Jain 23-APR-16
106 Ramesh Jaya 06-APR-16
107 John Malina 10-FEB-15

(ii) SQL> SELECT CNAME FROM TRAVEL WHERE VCODE='V01' OR VCODE='V02';

Page 72 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
CNAME
---------------
K Niwal
Hitesh Jain
Ravi Anish
Ramesh Jaya

OR

SQL> SELECT CNAME FROM TRAVEL WHERE VCODE IN ('V01','V02');

CNAME
---------------
K Niwal
Hitesh Jain
Ravi Anish
Ramesh Jaya

(iii) SQL> SELECT CNO, CNAME FROM TRAVEL WHERE TRAVELDATE BETWEEN '31-
DEC-2015' AND '01-MAY-2016';

CNO CNAME
---------- ---------------
103 Fredrick Sym
105 Hitesh Jain
102 Ravi Anish
104 Sahanubhuti
106 Ramesh Jaya

SQL> SELECT CNO, CNAME FROM TRAVEL WHERE TRAVELDATE>='31-DEC-2015'


AND TRAVELDATE<='01-MAY-2016';

CNO CNAME
---------- ---------------
103 Fredrick Sym
105 Hitesh Jain
102 Ravi Anish
104 Sahanubhuti
106 Ramesh Jaya

(iv) SQL> SELECT * FROM TRAVEL WHERE KM>120 ORDER BY NOP ASC;

CNO CNAME TRAVELDAT KM VCODE NOP


---------- --------------- --------- ---------- ------ ----------
101 K Niwal 13-DEC-15 200 V01 32

Page 73 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
105 Hitesh Jain 23-APR-16 450 V02 42

(v) SQL> SELECT COUNT(*), VCODE FROM TRAVEL GROUP BY VCODE HAVING
COUNT(*)>1;

COUNT(*) VCODE
---------- ------
2 V02
2 V01

(vi) SQL> SELECT DISTINCT VCODE FROM TRAVEL;

VCODE
------
V02
V05
V03
V01
V04

(vii) SQL> SELECT A.VCODE,CNAME,VEHICLETYPE


2 FROM TRAVEL A, VEHICLE B
3 WHERE A.VCODE=B.VCODE AND KM<90;

VCODE CNAME VEHICLETYPE


------ --------------- ---------------
V02 Ravi Anish AC DELUX BUS
V04 John Malina CAR

(viii) SQL> SELECT CNAME, KM*PERKM


2 FROM TRAVEL A, VEHICLE B
3 WHERE A.VCODE=B.VCODE AND A.VCODE='V05';

CNAME KM*PERKM
--------------- ----------
Sahanubhuti 2700

CBSE 2017

5 (a) Observe the following table MEMBER carefully and write the name of the RDBMS 2
operation out of (i) SELECTION (ii) PROJECTION (iii) UNION (iv) CARTESIAN PRODUCT,
which has been used to produce the output as follows in RESULT. Also, find the Degree
and Cardinality of the RESULT.

Page 74 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
MEMBER
NO MNAME STREAM
M001 JAYA SCIENCE
M002 ADITYA HUMANITIES
M003 HANSRAJ SCIENCE
M004 SHIVAK COMMERCE

RESULT
NO MNAME STREAM
M002 ADITYA HUMANITIES

Ans. The name of the RDBMS operation is (i) SELECTION operation. 2


Select Operation (σ) in relational algebra returns those tuples (records) in a relation that
fulfil a condition / predicate (Produce table containing subset of rows).

The table RESULT has Degree= 3 and Cardinality = 1

5 (b) Write SQL queries for (i) to (iv) and find output for SQL queries (v) to (viii), which are 8
based on the tables,
Table: DVD
DCODE DTITLE DTYPE
F101 Henry Martin Folk
C102 Dhrupad Classical
C101 The Planets Classical
F102 Universal Soldier Folk
R102 A day in life Rock
Table: MEMBER
MID NAME DCODE ISSUEDATE
101 AGAM SINGH R102 30-NOV-17
103 ARTH JOSEPH F102 13-DEC-16
102 NISHA HANS C101 24-JUL-17

(i) To display all details from the table MEMBER in descending order of ISSUEDAT.
(ii) To display the DCODE and DTITLE of all Folk Type DVDs from the table DVD.
(iii) To display the DTYPE and number of DVDs in each DTYPE from the table DVD.
(iv) To display all NAME and ISSUEDATE of those members from the table MEMBER
who have DVDs issued (i.e. ISSUEDATE) in the year 2017.
(v) SELECT MIN(ISSUEDATE) FROM MEMBER;
(vi) SELECT DISTINCT DTYPE FROM DVD;
(vii) SELECT D.DCODE, NAME, DTITLE
FROM DVD D, MEMBER M WHERE D.DCODE=M.DCODE;
(viii) SELECT DTITLE FROM DVD WHERE DTYPE NOT IN ('Folk','Classical');

Page 75 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

Ans. (i) SQL> SELECT * FROM MEMBER ORDER BY ISSUEDATE desc;

(ii) SQL> SELECT DCODE, DTITLE FROM DVD WHERE DTYPE='Folk';


Soldier

(iii) SQL> SELECT DTYPE, COUNT(*) FROM DVD GROUP BY DTYPE;

DTYPE COUNT(*)
---------- ----------
Rock 1
Folk 2
Classical 2

(iv) SQL> SELECT NAME, ISSUEDATE FROM MEMBER M, DVD D WHERE


M.DCODE=D.DCODE AND ISSUEDATE LIKE '%2017';

no rows selected

SQL> SELECT NAME, ISSUEDATE FROM MEMBER M, DVD D WHERE


M.DCODE=D.DCODE AND ISSUEDATE LIKE '%17';

NAME ISSUEDATE
------------ ---------
AGAM SINGH 30-NOV-17
NISHA HANS 24-JUL-17

(v) SQL> SELECT MIN(ISSUEDATE) FROM MEMBER;

MIN(ISSUE
---------
13-DEC-16

(vi) SQL> SELECT DISTINCT DTYPE FROM DVD;

DTYPE
----------
Rock
Folk
Classical

(vii) SQL> SELECT D.DCODE, NAME, DTITLE


Page 76 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
2 FROM DVD D, MEMBER M WHERE D.DCODE=M.DCODE;

DCODE NAME DTITLE


------ ------------ --------------------
R102 AGAM SINGH A day in life
F102 ARTH JOSEPH Universal Soldier
C101 NISHA HANS The Planets

(viii) SQL> SELECT DTITLE FROM DVD


2 WHERE DTYPE NOT IN ('Folk','Classical');

DTITLE
--------------------
A day in life

CBSE Sample QP 2017-2018

5 (a) Differentiate between DDL & DML commands. Identify DDL & DML commands 2
from the following:-
(UPDATE, SELECT, ALTER, DROP)
(b) Consider the following relations MobileMaster & MobileStock:- 6

Table: MobileMaster

Table: MobileStock

Page 77 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

SELECT AVG(M_Price) FROM MobileMaster;

Write the SQL query for questions from (i) to (iv) & write the output of SQL command for
questions from (v) to (viii) given below: -

(i) Display the Mobile company, Mobile name & price in descending order of their
manufacturing date.

(ii) List the details of mobile whose name starts with ‘S’.

(iii) Display the Mobile supplier & quantity of all mobiles except ‘MB003’.

(iv) To display the name of mobile company having price between 3000 & 5000.

(v) SELECT M_Id, SUM(M_Qty) FROM MobileStock GROUP BY M_Id;

(vi) SELECT MAX(M_Mf_Date), MIN(M_Mf_Date) FROM MobileMaster;

(vii) SELECT M1.M_Id, M1.M_Name, M2.M_Qty, M2.M_Supplier FROM MobileMaster


M1, MobileStock M2 WHERE M1.M_Id=M2.M_Id AND M2.M_Qty>=300;

(viii) SELECT AVG(M_Price) FROM MobileMaster;

5 (a) DDL stands for Data Definition language and comprises of commands which will 2
change the structure of database object.

DML stands for Data Manipulation Language and comprises of commands which
are used to insert, edit, view & delete the data stored in a database object.

DDL Commands: ALTER, DROP


DML Commands: UPDATE, SELECT

5 (b) (i) SELECT M_Compnay, M_Name, M_Price FROM MobileMaster ORDER BY


M_Mf_Date DESC;

(½ mark for correct SELECT)


(½ mark for correct ORDER BY)
Page 78 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

(ii) SELECT * FROM MobileMaster WHERE M_Name LIKE “S%‟;

(½ mark for correct SELECT)


(½ mark for correct WHERE clause)

(iii) SELECT M_Supplier, M_Qty FROM MobileStock WHERE M_Id <> “MB003‟;
OR
SELECT M_Supplier, M_Qty FROM MobileStock WHERE M_Id not like “MB003‟;

(½ mark for correct SELECT)


(½ mark for correct WHERE clause)

(iv) SELECT M_Company FROM MobileMaster WHERE M_Price BETWEEN 3000 AND
5000;

(½ mark for correct SELECT)


(½ mark for correct BETWEEN clause)

(v)

(vi)

(vii)

(viii) AVG(M_Price)

5450

(½ mark for correct output)

Page 79 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

CBSE 2018
5 (a) Observe the following tables VIDEO and MEMBER carefully and write the name of 2
the RDBMS operation out of (i) SELECTION (ii) PROJECTION (iii) UNION (iv)
CARTESIAN PRODUCT, which has been used to produce the output as shown below
,Also, find the Degree and Cardinality of the final result.

Ans Option (iv) CARTESIAN PRODUCT

DEGREE = 5
CARDINALITY = 9

(b) Write SQL queries for (i) to (iv) and find outputs for SQL queries (v) to (viii), which
are based on the tables.

Page 80 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

SELECT COUNT(*), SUM(AMOUNT) FROM TRANSACT WHERE DOT <= '2017-06-01';

(i) To display details of all transactions of TYPE Deposit from Table TRANSACT.
Ans SELECT * FROM TRANSACT WHERE TYPE = ' Deposit ' ;
(ii) To display the ANO and AMOUNT of all Deposits and Withdrawals done in the
month of October 2017 from table TRANSACT.
Ans. SELECT ANO,AMOUNT FROM TRANSACT WHERE DOT >= ' 2017-10-01 ' AND DOT
<= ' 2017-10-31 ' ;
OR
SELECT ANO,AMOUNT FROM TRANSACT WHERE DOT BETWEEN ' 2017-10-01 ' AND
' 2017-10-31 ' ;
(iii) To display the last date of transaction (DOT) from the table TRANSACT for the
Accounts having ANO as 103.
Ans. SELECT MAX(DOT) FROM TRANSACT WHERE ANO = 103;
(iv) To display all ANO, ANAME and DOT of those persons from tables ACCOUNT and
TRANSACT who have done transactions less than or equal to 3000.
Ans. SELECT ACCOUNT.ANO,ANAME,DOT FROM ACCOUNT,TRANSACT WHERE
ACCOUNT.ANO=TRANSACT.ANO AND AMOUNT <=3000;
OR
SELECT A.ANO,ANAME,DOT FROM ACCOUNT A,TRANSACT T WHERE A.ANO=T.ANO
AND AMOUNT <=3000;
(v) SELECT ANO, ANAME FROM ACCOUNT WHERE ADDRESS NOT IN ('CHENNAI',
'BANGALORE');

Ans. ANO ANAME


--------------------------
103 Ali Reza
105 Simran Kaur
(vi) SELECT DISTINCT ANO FROM TRANSACT;
Page 81 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
Ans DISTINCT ANO
---------------------
101
102
103

(vii) SELECT ANO, COUNT(*), MIN(AMOUNT) FROM TRANSACT GROUP BY ANO HAVING
COUNT(*)> 1;
Ans. ANO COUNT(*) MIN(AMOUNT)
-------------------------------------------------------
101 2 2500
103 2 1000
(viii) SELECT COUNT(*), SUM(AMOUNT) FROM TRANSACT WHERE DOT <= '2017-06-01';
Ans COUNT(*) SUM(AMOUNT)
-------------------------------------
2 5000

CBSE Sample Papers 2018-2019

5 (a) Observe the following table and answer the parts(i) and(ii) accordingly
Table: Product

(i) Write the names of most appropriate columns, which can be considered as candidate
keys.
(ii) What is the degree and cardinality of the above table?
Ans (i) Candidate Key: Pno, Name

(ii) Degree:4
Cardinality:5

(b) Write SQL queries for (i) to (iv) and find outputs for SQL queries (v) to (viii), which are 6
based on the tables.

Page 82 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

TRAINER

COURSE

SELECT COUNT(*), SUM(FEES) FROM COURSE WHERE STARTDATE< ‘2018-09-15’;

(i) Display the Trainer Name, City & Salary in descending order of their Hiredate.
Ans. SELECT TNAME, CITY, SALARY FROM TRAINER ORDER BY HIREDATE DESC;
(ii) To display the TNAME and CITY of Trainer who joined the Institute in the month of
December 2001.
Ans. SELECT TNAME, CITY FROM TRAINER WHERE HIREDATE BETWEEN ‘2001-12-01’ AND
‘2001-12-31’;
OR
SELECT TNAME, CITY FROM TRAINER WHERE HIREDATE >= ‘2001-12-01’ AND
HIREDATE<=‘2001-12-31’;
OR
SELECT TNAME, CITY FROM TRAINER WHERE HIREDATE LIKE ‘2001-12%’;
(iii) To display TNAME, HIREDATE, CNAME, STARTDATE from tables TRAINER and COURSE
of all those courses whose FEES is less than or equal to 10000.
Ans. SELECT TNAME,HIREDATE,CNAME,STARTDATE FROM TRAINER, COURSE WHERE
TRAINER.TID=COURSE.TID AND FEES<=10000;
(iv) To display number of Trainers from each city.
Ans. SELECT CITY, COUNT(*) FROM TRAINER GROUP BY CITY;
(v) SELECT TID, TNAME, FROM TRAINER WHERE CITY NOT IN(‘DELHI’, ‘MUMBAI’);
Ans. TID TNAME
---------------------------------
103 DEEPTI
106 MANIPRABHA
(vi) SELECT DISTINCT TID FROM COURSE;
Page 83 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

Ans. DISTINCT TID


---------------------
101
103
102
104
105

(vii) SELECT TID, COUNT(*), MIN(FEES) FROM COURSE GROUP BY TID HAVING COUNT(*)>1;
Ans. TID COUNT(*) MIN(FEES)
-----------------------------------------------
101 2 12000

(viii) SELECT COUNT(*), SUM(FEES) FROM COURSE WHERE STARTDATE< ‘2018-09-15’;


Ans. COUNT(*) SUM(FEES)
--------------- -------------------
4 65000

*************

CBSE 2019 BMV


5 Write SQL queries for (i) to (iv) and write outputs for SQL queries (v) to (viii), which 8
are based on the table given below:
Table: Trains

Table: PASSENGERS

Page 84 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
SELECT TNAME, PNAME FROM TRAINS T,PASSENGERS P WHERE T.TNO = P.TNO AND
AGE
BETWEEN 50 AND 60;

(i) To display details of all Trains which Start from New Delhi
Ans SELECT * FROM TRAINS WHERE START=’New Delhi’;
(½ Mark for correct SELECT statement)
(½ Mark for correct WHERE clause)
(ii) To display the PNR, PNAME, GENDER and AGE of all Passengers whose AGE is below 50.
Ans. SELECT PNR, PNAME, GENDER, AGE FROM PASSENGERS WHERE AGE < 50;
(½ Mark for correct SELECT statement)
(½ Mark for correct WHERE clause)
(iii) To display total number of MALE and FEMALE Passengers .

Ans. SELECT GENDER, COUNT(*) FROM PASSENGERS GROUP BY GENDER;


OR
SELECT GENDER, COUNT(GENDER) FROM PASSENGERS GROUP BY GENDER;

(½ Mark for correct SELECT statement)


(½ Mark for correct GROUP BY/additional COUNT clause)

(iv) To display details of all Passengers travelling in Trains whose TNO is 12015

Ans. SELECT * FROM PASSENGERS WHERE TNO=12015;

(½ Mark for correct SELECT statement)


(½ Mark for correct WHERE clause)

(v) SELECT MAX(TRAVELDATE), MIN(TRAVELDATE) FROM PASSENGERS WHERE GENDER =


'FEMALE';

Ans. MAX(TRAVELDATE) MIN(TRAVELDATE)


---------------------------------------------------------------
2018-11-10 2018-05-09

(½ Mark for correct MAX(TRAVELDATE))


(½ Mark for correct MIN(TRAVELDATE))

(vi) SELECT END, COUNT(*) FROM TRAINS GROUP BY END HAVING COUNT(*)>1;

Ans END COUNT(*)


-----------------------------------
Habibganj 2
Amritsar Junction 2
New Delhi 4

(1 Mark for correct output)


Page 85 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
NOTE: Values may be written in any order

(vii) SELECT DISTINCT TRAVELDATE FROM PASSENGERS;

Ans. DISTINCT TRAVELDATE


--------------------------------------
2018-12-25
2018-11-10
2018-10-12
2018-05-09

(1 Mark for correct output)


NOTE: Values may be written in any order

(viii) SELECT TNAME, PNAME FROM TRAINS T,PASSENGERS P WHERE T.TNO = P.TNO AND
AGE BETWEEN 50 AND 60;

Ans TNAME PNAME


---------------------------------------------
Amritsar Mail N S SINGH
Swarna Shatabdi R SHARMA

(1 Mark for correct output)

CBSE 2019 BMV-1


5 (a) Observe the following tables carefully and answer the questions that follow: 2

Which attribute out of the two attributes FNO and NAME in table FARMER should
be considered as Primary Key? Give reason for your selection of the same.

Ans. Primary Key: FNO


Reason: Unique values for identification of each tuple/record
Page 86 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
(1 Mark for writing correct attribute)
(1 Mark for writing correct reason)

(b) Write SQL queries for (i) to (iv) and write outputs for SQL queries (v) to (viii),
which are based on the following tables:
TABLE: FACULTY
+----------+--------------------------+------------+---------+----------------------+-------------------+
| FCODE | FNAME | GENDER | Room| Subject | JoinDate |
+----------+---------------------------+------------+--------+----------------------+-------------------+
| F1001 | HARI CHARAN JHA | MALE | 101 | ENGLISH | 2000-10-11 |
| F1004 | MERRY JOSE | FEMALE| 202 | PROGRAMMING| 2003-11-30 |
| F1002 | FARDEEN KHAN | MALE | 301 | WEB DESIGN | 2001-09-06 |
| F1005 | PRIYA MAHESHWARI| FEMALE| 302 | WEB DESIGN | 2004-12-15 |
| F1009 | TANYA SANTAN | FEMALE | 203 | PROGRAMMING | 2006-12-31 |
| F1006 | AMAR ANSHUL | MALE | 208 | DATA STRUCTURE| 2005-05-02 |
+---------+-----------------------------+------------+-------+------------------------+-----------------+
TABLE: SCHEDULE;
+-----------+------------+------------+------------+------------+
| CCODE | FCODE1 | FCODE2 | FCODE3 | FCODE4 |
+----------+-------------+------------+------------+------------+
| C1 | F1009 | F1002 | F1001 | F1005 |
| C2 | F1001 | F1009 | F1005 | F1006 |
| C3 | F1006 | F1001 | F1004 | F1002 |
| C4 | F1002 | F1004 | F1009 | F1001 |
+---------+--------------+------------+-----------+------------+
(i) To display details of all Male faculties from the FACULTY table
Ans SELECT * FROM FACULTY WHERE Gender='Male';
(½ Mark for correct SELECT statement) & (½ Mark for correct WHERE clause)
(ii) To display the FCODE, FNAME and GENDER of all faculties, who are either
teaching subject "Programming" or teaching subject "Web Design".
Ans. SELECT FCODE, FNAME, Gender FROM FACULTY WHERE SUBJECT='Programming'
OR SUBJECT='Web Design';
OR
SELECT FCODE, FNAME, Gender FROM FACULTY WHERE Subject
IN('Programming', 'Web Design');

(½ Mark for correct SELECT statement)


(½ Mark for correct WHERE clause)

(iii) To display FCODE, FNAME, JOINDATE, ROOM of all faculty members in


descending order of the ROOM numbers.

Ans. SELECT FCODE, FNAME, JOINDATE, ROOM FROM FACULTY ORDER BY ROOM
DESC;

(½ Mark for correct SELECT statement) & (½ Mark for correct ORDER BY clause)
(iv) To add a new faculty member with the following details:
Page 87 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

Ans. INSERT INTO FACULTY VALUES ('F1010', 'Anish Mohanty', 'M', 209, 'Data
Structure', '2019-03-22');
● (½ Mark for correct INSERT statement)
● (½ Mark for correct inserting correct values)
Note: While writing the answers of the following output questions, consider the
original data as given in the above tables (i.e., without considering the changes
done by part iv of this question)
(v) SELECT COUNT(FCODE1),COUNT(FCODE2) FROM SCHEDULE WHERE
FCODE1='F1009' OR FCODE2='F1009';
Ans. +-----------------------+------------------------+
| COUNT(FCODE1) | COUNT(FCODE2) |
+------------------------+------------------------+
| 2 | 2 |
+------------------------+------------------------+
(½ Mark for writing correct output)
(vi) SELECT CCODE,FCODE3,FNAME FROM FACULTY,SCHEDULE WHERE
FACULTY.FCODE=SCHEDULE.FCODE3 AND FACULTY.FCODE='F1001';
Ans +-----------+------------+--------------------------+
| CCODE | FCODE3 | FNAME |
+----------+-------------+--------------------------+
| C1 | F1001 | HARI CHARAN JHA |
+----------+------------+--------------------------+
(½ Mark for correct output)
NOTE: Values may be written in any order
(vii) SELECT GENDER,COUNT(*) FROM FACULTY GROUP BY GENDER;
Ans. +------------+---------------+
| GENDER | COUNT(*) |
+-------------+--------------+
| FEMALE | 3 |
| MALE | 3 |
+-------------+--------------+
(½ Mark for correct output)
NOTE: Values may be written in any order
(viii) SELECT MIN(ROOM) FROM FACULTY WHERE JOINDATE>'2004-12-15';
Ans +------------------+
| MIN(ROOM) |
+------------------+
| 203 |
+-----------------+

CBSE 2019 Compartment


5 (a) Observe the following table STOCK carefully and answer the questions that follow : 2
Table : STOCK
Page 88 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

Which attribute out of SNO, NAME and PRICE is the ideal one for being considered
as the Primary Key and why ?
Ans

(b) Write SQL queries for (i) to (iv) and write outputs for SQL queries (v) to (viii), which 6
are based on the tables given below :

Table : TRAINS

Table : PASSENGERS

NOTE : All Dates are given in ‘YYYY-MM-DD’ format.


(i) To display total number of MALE and FEMALE Passengers
(ii) To display details of all Trains which Start from Pune Junction
(iii) To display details of all Passengers travelling in Trains whose TNO is 12030
(iv) To display the PNR, PNAME, GENDER and AGE of all Passengers whose AGE is
above 50.
(v) SELECT DISTINCT TRAVELDATE FROM PASSENGERS;
(vi) SELECT MIN (TRAVELDATE), MAX (TRAVELDATE) FROM PASSENGERS WHERE
GENDER = 'MALE';
(vii) SELECT START, COUNT(*) FROM TRAINS GROUP BY START HAVING COUNT (*)>1;

Page 89 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
(viii) SELECT TNAME, PNAME FROM TRAINS T, PASSENGERS P WHERE T.TNO = P.TNO
AND AGE BETWEEN 40 AND 50;

CBSE Sample paper 2019-20

5 (a) Observe the following table and answer the parts(i) and(ii) accordingly 2
Table: Product

(i) Write the names of most appropriate columns, which can be considered as
candidate keys.
Ans Candidate Key: Pno, Name
(1 Mark for writing correct Candidate Keys)
(ii) What is the degree and cardinality of the above table?
Ans. Degree:4
Cardinality:5
(½ Mark for writing correct value of degree)
(½ Mark for writing correct value of cardinality)
(b) Write SQL queries for (i) to (iv) and find outputs for SQL queries (v) to (viii), which
are based on the tables.
TRAINER

COURSE

Page 90 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

(i) Display the Trainer Name, City & Salary in descending order of their Hiredate.
Ans SELECT TNAME, CITY, SALARY FROM TRAINER ORDER BY HIREDATE DESC;
(½ Mark for SELECT TNAME, CITY, SALARY FROM TRAINER)
(½ Mark for ORDER BY HIREDATE DESC)
(ii) To display the TNAME and CITY of Trainer who joined the Institute in the month of
December 2001.
Ans. SELECT TNAME, CITY FROM TRAINER WHERE HIREDATE BETWEEN ‘2001-12-01’
AND ‘2001-12-31’;
OR
SELECT TNAME, CITY FROM TRAINER WHERE HIREDATE >= ‘2001-12-01’ AND
HIREDATE<=‘2001-12-31’;
OR
SELECT TNAME, CITY FROM TRAINER WHERE HIREDATE LIKE ‘2001-12%’;
(iii) To display TNAME, HIREDATE, CNAME, STARTDATE from tables TRAINER and
COURSE of all those courses whose FEES is less than or equal to 10000.
Ans. SELECT TNAME,HIREDATE,CNAME,STARTDATE FROM TRAINER, COURSE WHERE
TRAINER.TID=COURSE.TID AND FEES<=10000;

(1 Mark for correct query)


OR
(½ Mark for correct SELECT )
(½ Mark for correct WHERE Clause)

(iv) To display number of Trainers from each city.

Ans. SELECT CITY, COUNT(*) FROM TRAINER GROUP BY CITY;


(1 Mark for correct query)
OR
(½ Mark for correct SELECT )
(½ Mark for GROUP BY CITY)

(v) SELECT TID, TNAME FROM TRAINER WHERE CITY NOT IN(‘DELHI’, ‘MUMBAI’);

Ans. TID TNAME


---------------
103 DEEPTI
106 MANIPRABHA

(½ Mark for correct output)


(vi) SELECT DISTINCT TID FROM COURSE;

Page 91 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
Ans DISTINCT TID
101
103
102
104
105

(½ Mark for correct output)

(vii) SELECT TID, COUNT(*), MIN(FEES) FROM COURSE GROUP BY TID HAVING
COUNT(*)>1;
Ans. TID COUNT(*) MIN(FEES)
-----------------------------------
101 2 12000
(½ Mark for correct output)
(viii) SELECT COUNT(*), SUM(FEES) FROM COURSE WHERE STARTDATE< ‘2018-09-15’;

Ans COUNT(*) SUM(FEES)


--------------------------
4 65000
(½ Mark for correct output)

CBSE Board Exam. 13th March 2022


Page 92 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

Section-A

4 Consider the following SQL table MEMBER in a SQL Database CLUB. 2


Steps for Creating Database CLUB and Table: MEMBER
mysql> CREATE DATABASE CLUB;

mysql> USE CLUB;

mysql> CREATE TABLE MEMBER (M_ID CHAR(5) PRIMARY KEY, NAME VARCHAR(10),
ACTIVITY VARCHAR(10));

mysql> INSERT INTO MEMBER VALUES ('M1001','Amina','GYM');

mysql> INSERT INTO MEMBER VALUES ('M1002','Pratik','GYM');

mysql> INSERT INTO MEMBER VALUES ('M1003','Simon','SWIMMING');

mysql> INSERT INTO MEMBER VALUES ('M1004','Rakesh','GYM');

mysql> INSERT INTO MEMBER VALUES ('M1005','Avneet','SWIMMING');

mysql> select * from Member;


+------------+-----------+-----------------+
| M_ID | NAME | ACTIVITY |
+-----------+------------+-----------------+
| M1001 | Amina | GYM |
| M1002 | Pratik | GYM |
| M1003 | Simon | SWIMMING |
| M1004 | Rakesh | GYM |
| M1005 | Avneet | SWIMMING |
+--------- -+------------+-----------------+

Consider the following SQL table MEMBER in a SQL Database CLUB:

Table: Member
+------------+-----------+-----------------+
| M_ID | NAME | ACTIVITY |
+-----------+------------+-----------------+
| M1001 | Amina | GYM |
| M1002 | Pratik | GYM |
| M1003 | Simon | SWIMMING |
| M1004 | Rakesh | GYM |
| M1005 | Avneet | SWIMMING |
+--------- -+------------+-----------------+
Assume that the required library for establishing the connection between Python and
MYSQL

Page 93 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
is already imported in the given Python code. Also assume that DB is the name of the
database
connection for table MEMBER stored in the database CLUB.

Predict the output of the following code:

import mysql.connector as sqltor


DB=sqltor.connect(host="localhost",user="root",passwd="yadav", database="CLUB")
if DB.is_connected():
print("Successful")
else:
print("Not connected")
MYCUR=DB.cursor()
MYCUR.execute("USE CLUB")
MYCUR.execute("select * from member where activity='GYM' ")
R=MYCUR.fetchone()
for i in range(2):
R=MYCUR.fetchone()
print(R[0], R[1],R[2],sep="#")

Output:
Successful
M1002#Pratik#GYM
M1004#Rakesh#GYM

5 Steps for creating Table VACCINATION_DATA in Database CLUB.


(Database CLUB is created in previous question no. 4. To use or open the database CLUB
write:
mysql> CREATE DATABASE CLUB; # Database to be created only once
mysql> USE CLUB;
mysql> create table VACCINATION_DATA (VID INT(3), Name Varchar(10), Age int(3), Dose1
date, Dose2 Date, City Varchar(10));
mysql> INSERT INTO VACCINATION_DATA VALUES (101, 'Jenny', 27, '2021-12-25','2022-01-
31','Delhi');
mysql> INSERT INTO VACCINATION_DATA VALUES (102, 'Harjot', 55, '2021-07-14','2021-10-
14','Mumbai');
mysql> INSERT INTO VACCINATION_DATA VALUES (103, 'Srikanth', 43, '2021-04-18','2021-
07-20','Delhi');
mysql> INSERT INTO VACCINATION_DATA VALUES (104, 'Gazala', 75, '2021-07-
31',NULL,'Kolkata');
mysql> INSERT INTO VACCINATION_DATA VALUES (105, 'Shiksha', 32, '2022-01-
01',NULL,'Mumbai');
mysql> SELECT * FROM VACCINATION_DATA;
Write the output of SQL queries (a) to (d) based on the table VACCINATION_DATA given 2
below:
TABLE: VACCINATION_DATA;
+------+--------------+------+----------------+-----------------+-------------+

Page 94 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
| VID | Name | Age | Dose1 | Dose2 | City |
+------+--------------+------+----------------+-----------------+-------------+
| 101 | Jenny | 27 | 2021-12-25 | 2022-01-31 | Delhi |
| 102 | Harjot | 55 | 2021-07-14 | 2021-10-14 | Mumbai |
| 103 | Srikanth | 43 | 2021-04-18 | 2021-07-20 | Delhi |
| 104 | Gazala | 75 | 2021-07-31 | NULL | Kolkata |
| 105 | Shiksha | 32 | 2022-01-01 | NULL | Mumbai |
+------+-------------+------+-----------------+-----------------+-------------+

(a) mysql> SELECT Name, Age FROM VACCINATION_DATA WHERE Dose2 IS NOT NULL AND
Age>40;

(b) mysql> SELECT City, COUNT(*) FROM VACCINATION_DATA GROUP BY City;

(c) mysql> SELECT DISTINCT City FROM VACCINATION_DATA;

(d) mysql> SELECT MAX(Dose1), MIN(Dose2) FROM VACCINATION_DATA;

Ans: (a) mysql> SELECT Name, Age FROM VACCINATION_DATA WHERE Dose2 IS NOT NULL AND
Age>40;
Output:
+-------------+-------+
| Name | Age |
+-------------+-------+
| Harjot | 55 |
| Srikanth | 43 |
+-------------+------+
(b) mysql> SELECT City, COUNT(*) FROM VACCINATION_DATA GROUP BY City;
Output:
+-------------+---------------+
| City | COUNT(*) |
+-------------+---------------+
| Delhi | 2 |
| Mumbai | 2 |
| Kolkata | 1 |
+--------------+---------------+
(c) mysql> SELECT DISTINCT City FROM VACCINATION_DATA;
Output:
+--------------+
| City |
+--------------+
| Delhi |
| Mumbai |
| Kolkata |
+--------------+

Page 95 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
(d) mysql> SELECT MAX(Dose1), MIN(Dose2) FROM VACCINATION_DATA;
Output:
+-------------------+-----------------+
| MAX(Dose1) | MIN(Dose2) |
+-------------------+-----------------+
| 2022-01-01 | 2021-07-20 |
+-------------------+-----------------+

6 Steps for creating Table DOCTOR and PATIENT in Database CLUB. 2


(Database CLUB is created in previous question no. 4. To use or open the database CLUB
write:

mysql> CREATE DATABASE CLUB; # Database to be created only once

mysql> USE CLUB;

mysql> CREATE TABLE DOCTOR (DNO CHAR(2), DNAME VARCHAR(10), FEES DECIMAL(8,2));

mysql> INSERT INTO DOCTOR VALUES ('D1','AMITABH',1500);

mysql> INSERT INTO DOCTOR VALUES ('D2','ANIKET',1000);

mysql> INSERT INTO DOCTOR VALUES ('D3','NIKHIL',1500);

mysql> INSERT INTO DOCTOR VALUES ('D4','ANJANA',1500);

mysql> SELECT * FROM DOCTOR;


+--------+--------------+------------+
| DNO | DNAME | FEES |
+--------+--------------+-------------+
| D1 | AMITABH | 1500.00 |
| D2 | ANIKET | 1000.00 |
| D3 | NIKHIL | 1500.00 |
| D4 | ANJANA | 1500.00 |
+--------+--------------+------------+

mysql> CREATE TABLE PATIENT (PNO CHAR(2), PNAME VARCHAR(10), ADMDATE DATE, DNO
CHAR(2));

mysql> INSERT INTO PATIENT VALUES ('P1','NOOR', '2021-12-25','D1');

mysql> INSERT INTO PATIENT VALUES ('P2','ANNIE', '2021-11-20','D2');

mysql> INSERT INTO PATIENT VALUES ('P3','PRAKASH', '2020-12-10',NULL);

mysql> INSERT INTO PATIENT VALUES ('P4','HARMEET', '2019-12-20','D1');

Page 96 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

mysql> SELECT * FROM PATIENT;


+--------+--------------+------------------+--------+
| PNO | PNAME | ADMDATE | DNO |
+--------+--------------+-----------------+---------+
| P1 | NOOR | 2021-12-25 | D1 |
| P2 | ANNIE | 2021-11-20 | D2 |
| P3 | PRAKASH | 2020-12-10 | NULL |
| P4 | HARMEET | 2019-12-20 | D1 |
+------ +---------------+----------------+---------+

6 Write the output of SQL queries (a) and (b) based on the following two tables DOCTOR and 2
PATIENT belonging to the same database CLUB.

Table: DOCTOR
+--------+--------------+-------------+
| DNO | DNAME | FEES |
+--------+--------------+-------------+
| D1 | AMITABH | 1500.00 |
| D2 | ANIKET | 1000.00 |
| D3 | NIKHIL | 1500.00 |
| D4 | ANJANA | 1500.00 |
+--------+--------------+-------------+

Table: PATIENT
+--------+--------------+------------------+--------+
| PNO | PNAME | ADMDATE | DNO |
+--------+--------------+-----------------+---------+
| P1 | NOOR | 2021-12-25 | D1 |
| P2 | ANNIE | 2021-11-20 | D2 |
| P3 | PRAKASH | 2020-12-10 | NULL |
| P4 | HARMEET | 2019-12-20 | D1 |
+------ +---------------+----------------+---------+

(a) mysql> SELECT DNAME, PNAME FROM DOCTOR NATURAL JOIN PATIENT;

(b) mysql> SELECT PNAME, ADMDATE, FEES FROM PATIENT P, DOCTOR D WHERE
D.DNO=P.DNO AND FEES>1000;
OR
mysql> SELECT PNAME, ADMDATE, FEES FROM PATIENT , DOCTOR WHERE DOCTOR.DNO=
PATIENT.DNO AND FEES>1000;

Ans (a) mysql> SELECT DNAME, PNAME FROM DOCTOR NATURAL JOIN PATIENT;
Output:
+--------------+----------------+
Page 97 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
| DNAME | PNAME |
+--------------+----------------+
| AMITABH | NOOR |
| ANIKET | ANNIE |
| AMITABH | HARMEET |
+--------------+----------------+

(b) mysql> SELECT PNAME, ADMDATE, FEES FROM PATIENT P, DOCTOR D WHERE
D.DNO=P.DNO AND FEES>1000;
Output:
+---------------+------------------+------------+
| PNAME | ADMDATE | FEES |
+---------------+------------------+------------+
| NOOR | 2021-12-25 | 1500.00 |
| HARMEET | 2019-12-20 | 1500.00 |
+---------------+------------------+-------------+

7 Differentiate between Candidate Key and Primary Key in the context of Relational Database 2
Model.
OR

Consider the following table PLAYER:

Table: PLAYER

PNO NAME SCORE


P1 RISHABH 52
P2 HUSSAIN 45
P3 ARNOLD 23
P4 ARVAV 18
P5 GURUSHARAN 42

(a) Identify and write the name of the most appropriate column from the given table PLAYER
that can be used as a Primary Key.

(b) Define the term Degree in the relational data model. What is the Degree of the given table
PLAYER?

9 (i) A SQL table ITEMS contains the following columns: 1

INO, INAME, QUANTITY, PRICE, DISCOUNT

Write the SQL command to remove the column DISCOUNT from the table.

Page 98 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

(ii) Categorize the following SQL commands into DDL and DML: 2
CREATE, UPDATE, INSERT, DROP

Ans. (i) alter table ITEMS drop column DISCOUNT;


or
alter table ITEMS drop DISCOUNT;

(ii) DDL: CREATE, DROP

DML: UPDATE, INSERT

10 Rohan is learning to work upon Relational Database Management System (RDBMS) 3


application. Help him to perform following tasks:

(a) To open the database named “LIBRARY”.


(b) To display the names of all the tables stored in the opened database.
(c) To display the structure of the table “BOOKS” existing in the already opened database
“LIBRARY”.

Ans.: (a) USE LIBRARY;


(b) SHOW TABLES;
(c) DESC BOOKS;
OR
DESCRIBE BOOKS;

11 Write SQL queries for (a) to (d) based on the tables PASSENGER and FLIGHT given below: 4

Creating Tables PASSENGER and FLIGHT in Database CLUB:

mysql> CREATE TABLE PASSENGER (PNO INT(4) PRIMARY KEY, NAME VARCHAR(10), GENDER
VARCHAR(8), FNO CHAR(4));

mysql> INSERT INTO PASSENGER VALUES (1001, 'Suresh','MALE','F101');

mysql> INSERT INTO PASSENGER VALUES (1002, 'Anita','FEMALE','F104');

mysql> INSERT INTO PASSENGER VALUES (1003, 'Harjas','MALE','F102');

mysql> INSERT INTO PASSENGER VALUES (1004, 'Nita','FEMALE','F103');

mysql> SELECT * FROM PASSENGER;


+--------+-----------+-------------+--------+
| PNO | NAME | GENDER | FNO |
+--------+-----------+-------------+--------+
Page 99 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
| 1001 | Suresh | MALE | F101 |
| 1002 | Anita | FEMALE | F104 |
| 1003 | Harjas | MALE | F102 |
| 1004 | Nita | FEMALE | F103 |
+--------+------------+-------------+--------+

mysql> CREATE TABLE FLIGHT (FNO CHAR(4), START VARCHAR(10), END VARCHAR(10),
F_DATE DATE, FARE DECIMAL(7,2));

mysql> INSERT INTO FLIGHT VALUES ('F101', 'MUMBAI', 'CHENNAI', '2021-12-25', 4500.00);

mysql> INSERT INTO FLIGHT VALUES ('F102', 'MUMBAI', 'BENGALURU', '2021-11-20',


4000.00);

mysql> INSERT INTO FLIGHT VALUES ('F103', 'DELHI', 'CHENNAI', '2021-12-10', 5500);

mysql> INSERT INTO FLIGHT VALUES ('F104', 'KOLKATA', 'MUMBAI', '2021-12-20', 4500);

mysql> INSERT INTO FLIGHT VALUES ('F105', 'DELHI', 'BENGALURU', '2021-01-15', 5000);

mysql> SELECT * FROM FLIGHT;


+--------+--------------+------------------+----------------+-------------+
| FNO | START | END | F_DATE | FARE |
+--------+--------------+------------------+----------------+-------------+
| F101 | MUMBAI | CHENNAI | 2021-12-25 | 4500.00 |
| F102 | MUMBAI | BENGALURU | 2021-11-20 | 4000.00 |
| F103 | DELHI | CHENNAI | 2021-12-10 | 5500.00 |
| F104 | KOLKATA | MUMBAI | 2021-12-20 | 4500.00 |
| F105 | DELHI | BENGALURU | 2021-01-15 | 5000.00 |
+--------+--------------+------------------+-----------------+------------+

Write SQL queries for (a) to (d) based on the tables PASSENGER and FLIGHT given below: 4

Table: PASSENGER
+--------+-----------+-------------+--------+
| PNO | NAME | GENDER | FNO |
+--------+-----------+-------------+--------+
| 1001 | Suresh | MALE | F101 |
| 1002 | Anita | FEMALE | F104 |
| 1003 | Harjas | MALE | F102 |
| 1004 | Nita | FEMALE | F103 |
+--------+------------+-------------+--------+
Table: FLIGHT
+--------+--------------+------------------+----------------+-------------+
| FNO | START | END | F_DATE | FARE |
+--------+--------------+------------------+----------------+-------------+
| F101 | MUMBAI | CHENNAI | 2021-12-25 | 4500.00 |

Page 100 of 154


OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
| F102 | MUMBAI | BENGALURU | 2021-11-20 | 4000.00 |
| F103 | DELHI | CHENNAI | 2021-12-10 | 5500.00 |
| F104 | KOLKATA | MUMBAI | 2021-12-20 | 4500.00 |
| F105 | DELHI | BENGALURU | 2021-01-15 | 5000.00 |
+--------+--------------+------------------+-----------------+------------+

(a) Write a query to change the fare to 6000 of the flight whose FNO is F104.

(b) Write a query to display the total number of MALE and FEMALE PASSENGERS.

(c) Write a query to display the NAME, corresponding FARE and F_DATE of all PASSENGERS who
have a flight to START from DELHI.

(d) Write a query to delete the records of the flights which end at MUMBAI.

Ans. (a) UPDATE FLIGHT SET FARE=6000 WHERE FNO='F104';

(b) SELECT GENDER, COUNT(*) "TOTAL NUMBER" FROM PASSENGER GROUP BY GENDER;
Output: Don’t write the output
+------------+-----------------------+
| GENDER | TOTAL NUMBER |
+------------+-----------------------+
| MALE | 2 |
| FEMALE | 2 |
+------------+-----------------------+

(c) SELECT NAME, FARE , F_DATE FROM FLIGHT F, PASSENGER P WHERE F.FNO=P.FNO AND
START='DELHI';
Output: Don’t write the output
+----------+------------+-----------------+
| NAME | FARE | F_DATE |
+----------+------------+-----------------+
| Nita | 5500.00 | 2021-12-10 |
+----------+------------+----------------+

(d) DELETE FROM FLIGHT WHERE END='MUMBAI';

CBSE 2021 COMPT


Q. In SQL, write the name of the aggregate function which will display the cardinality of a table. 1
11
Ans. Count()
Q 12 Which of the following clauses in SQL is most appropriate to use to select matching tuples in 1
a specific range of values?
(A) IN
(B) LIKE
(C) BETWEEN
(D) IS
Page 101 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

Ans. (C) BETWEEN

Q 13 Which of the following is not a valid datatype in SQL? 1


(A) DATE
(B) STRING
(C) DECIMAL
(D) CHAR
Ans. (B) STRING

14 Which of the following is not a valid DML command in SQL? 1


(A) INSERT
(B) UPDATE
(C) ALTER
(D) DELETE

Ans. (C) ALTER

18 Which of the following commands in SQL is used to add a new record into a table? 1
(A) ADD
(B) INSERT
(C) UPDATE
(D) NEW

Ans. (B) INSERT

19. Which of the following is the correct expansion of DML in context of SQL? 1
(A) Direct Machine Language
(B) Data Mixing Language
(C) Distributed Machine Language
(D) Data Manipulation Language
Ans. (D) Data Manipulation Language

22. Anmol maintains that database of Medicines for his pharmacy using SQL to store the data.
The structure of the table PHARMA for the purpose is as follows:

• Name of the table - PHARMA


• The attributes of PHARMA are as follows:
MID - character of size 2
MNAME - character of size 20
PRICE - numeric
UNITS - numeric
EXPIRY – date

Table: PHARMA
Page 102 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
+--------+---------------------+---------+---------+-----------------+
| MID | MNAME | PRICE | UNITS | EXPIRY |
+--------+---------------------+---------+---------+-----------------+
| M1 | PARACETAMOL | 12.00 | 120 | 2022-12-25 |
| M2 | CETRIZINE | 6.00 | 125 | 2022-10-12 |
| M3 | METFORMIN | 14.00 | 150 | 2022-05-23 |
| M4 | VITAMIN B-6 | 12.00 | 120 | 2022-07-01 |
| M5 | VITAMIN D3 | 25.00 | 150 | 2022-06-30 |
| M6 | TELMISARTAN | 22.00 | 115 | 2022-02-25 |
+-------+----------------------+---------+---------+-----------------+

(a) Write the degree and cardinality of the table PHARMA. 1

(b) Identify the attribute best suitable to be declared as a primary key. 1

(c) Anmol has received a new medicine to be added into his stock, but for which he does not 1
know the number of UNITS. So he decides to add the medicine without its value for UNITS.
The rest of the values are as follows:

MID MNAME PRICE EXPIRY


M7 SUCRALFATE 17 2022-03-20

Write the SQL command which Anmol should execute to perform the required task.

(d) Anmol wants to change the name of the attribute UNITS to QUANTITY in the table PHARMA. 1
Which of the following commands will he use for the purpose?
(i) UPDATE
(ii) DROP TABLE
(iii) CREATE TABLE
(iv) ALTER TABLE

(e) Now Anmol wants to increase the PRICE of all medicines by 5. Which of the following 1
commands will he use for the purpose?
(i) UPDATE SET
(ii) INCREASE BY
(iii) ALTER TABLE
(iv) INSERT INTO

(f) Write a statement to change the name of the attribute UNITS to QUANTITY in the table 1
PHARMA.

(g) Write a statement to change value of QUANTITY to 120 where QUANTITY is null. 1

Creating Database CBSE2021 and Using database CBSE2021, Creating Table PHARMA and
inserting records in table PHARMS.
Execute the following MySQL on your Computer: Type the statement after MySQL>
prompt and each statement should end with semicolon (;)
Page 103 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

mysql> CREATE DATABASE CBSE2021;

mysql> USE CBSE2021;

mysql> CREATE TABLE PHARMA (MID CHAR(2), MNAME VARCHAR(20), PRICE DECIMAL(7,2),
UNITS INT(3), EXPIRY DATE);

mysql> INSERT INTO PHARMA VALUES ('M1','PARACETAMOL', 12, 120, '2022-12-25');

mysql> INSERT INTO PHARMA VALUES ( 'M2', 'CETRIZINE', 6, 125, '2022-10-12' );

mysql> INSERT INTO PHARMA VALUES ( 'M3', 'METFORMIN', 14, 150, '2022-05-23' );

mysql> INSERT INTO PHARMA VALUES ( 'M4', 'VITAMIN B-6', 12, 120, '2022-07-01');

mysql> INSERT INTO PHARMA VALUES ( 'M5', 'VITAMIN D3', 25, 150, '2022-06-30');

mysql> INSERT INTO PHARMA VALUES ( 'M6', 'TELMISARTAN', 22, 115, '2022-02-25');

mysql> SELECT * FROM PHARMA;

+--------+---------------------+---------+---------+-----------------+
| MID | MNAME | PRICE | UNITS | EXPIRY |
+--------+---------------------+---------+---------+-----------------+
| M1 | PARACETAMOL | 12.00 | 120 | 2022-12-25 |
| M2 | CETRIZINE | 6.00 | 125 | 2022-10-12 |
| M3 | METFORMIN | 14.00 | 150 | 2022-05-23 |
| M4 | VITAMIN B-6 | 12.00 | 120 | 2022-07-01 |
| M5 | VITAMIN D3 | 25.00 | 150 | 2022-06-30 |
| M6 | TELMISARTAN | 22.00 | 115 | 2022-02-25 |
+-------+----------------------+---------+---------+-----------------+

(a) Write the degree and cardinality of the table PHARMA.


(b) Identify the attribute best suitable to be declared as a primary key.
(c) Anmol has received a new medicine to be added into his stock, but for which he does not
know the number of UNITS. So he decides to add the medicine without its value for UNITS.
The rest of the values are as follows :
MID MNAME PRICE EXPIRY
M7 SUCRALFATE 17 2022-03-20
Write the SQL command which Anmol should execute to perform the required task.
(d) Anmol wants to change the name of the attribute UNITS to QUANTITY in the table PHARMA.
Which of the following commands will he use for the purpose?
(i) UPDATE
(ii) DROP TABLE
(iii) CREATE TABLE
(iv) ALTER TABLE

Page 104 of 154


OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

(e) Now Anmol wants to increase the PRICE of all medicines by 5. Which of the following
commands will he use for the purpose?
(i) UPDATE SET
(ii) INCREASE BY
(iii) ALTER TABLE
(iv) INSERT INTO
(f) Write a statement to change the name of the attribute UNITS to QUANTITY in the table
PHARMA.
(g) Write a statement to change value of QUANTITY to 120 where QUANTITY is null.
Ans. (a) Degree = 5 (Number of columns/ attributes)
cardinality= 6 (Number of rows -excluding the header row)

(b) MID is the best suitable to be declared as a primary key. (Primary key uniquely identifies
each row or each tuple in a table or a relation.)

(c) INSERT INTO PHARMA (MID, MNAME, PRICE, EXPIRY) VALUES


('M7','SUCRALFATE',17,'2022-03-20');
OR
INSERT INTO PHARMA VALUES ( 'M7','SUCRALFATE',17, NULL, '2022-03-20');

(d) (iv) ALTER TABLE

(e) (i) UPDATE SET

(f) ALTER TABLE PHARMA RENAME COLUMN UNITS TO QUANTITY;

(g) UPDATE PHARMA SET QUANTITY=120 WHERE QUANTITY IS NULL;

30 What are Tuples in a SQL Table? Write a suitable example with a SQL Table to illustrate your 2
answer.

Ans. A SQL tuple is a row value.

Example: The following table/relation PHARMA there are 6 tuples / or 6 rows.

Table: PHARMA
+--------+---------------------+---------+---------+-----------------+
| MID | MNAME | PRICE | UNITS | EXPIRY |
+--------+---------------------+---------+---------+-----------------+
| M1 | PARACETAMOL | 12.00 | 120 | 2022-12-25 | Tuple-1 / Row-1
| M2 | CETRIZINE | 6.00 | 125 | 2022-10-12 | Tuple-2 / Row-2
| M3 | METFORMIN | 14.00 | 150 | 2022-05-23 |
| M4 | VITAMIN B-6 | 12.00 | 120 | 2022-07-01 |
| M5 | VITAMIN D3 | 25.00 | 150 | 2022-06-30 |
| M6 | TELMISARTAN | 22.00 | 115 | 2022-02-25 | Tuple-6/ Row-6
Page 105 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
+-------+----------------------+---------+---------+-----------------+

31 For the following SQL Table named PASSENGERS in a database TRAVEL: 2

TNO NAME START END


T1 RAVI KUMAR DELHI MUMBAI
T2 NISHANT JAIN DELHI KOLKATA
T3 DEEPAK PRAKASH MUMBAI PUNE

A cursor named Cur is created in Python for a connection of a host which contains the
database TRAVEL. Write the output for the execution of the following Python statements for
the above SQL Table PASSENGERS:

Cur.execute("USE TRAVEL")
Cur.execute("SELECT * FROM PASSENGERS")
Recs=Cur.fetchall()
for R in Recs:
print(R[1])

Creating Database TRAVEL, Using Database TRAVEL, Creating Table PASSENGERS and
inserting records into Table PASSENGERS.

mysql> CREATE database TRAVEL;


mysql> USE TRAVEL;
mysql> CREATE TABLE PASSENGERS (TNO CHAR(3), NAME VARCHAR(20), START
VARCHAR(10), END VARCHAR(10));
mysql> INSERT INTO PASSENGERS VALUES ('T1', 'RAVI KUMAR', 'DELHI', 'MUMBAI' );
mysql> INSERT INTO PASSENGERS VALUES ('T2', 'NISHANT JAIN', 'DELHI', 'KOLKATA' );
mysql> INSERT INTO PASSENGERS VALUES ('T3', 'DEEPAK PRAKASH', 'MUMBAI', 'PUNE' );
mysql> SELECT * FROM PASSENGERS;
+--------+-------------------------+--------------+--------------+
| TNO | NAME | START | END |
+--------+-------------------------+--------------+--------------+
| T1 | RAVI KUMAR | DELHI | MUMBAI |
| T2 | NISHANT JAIN | DELHI | KOLKATA |
| T3 | DEEPAK PRAKASH | MUMBAI | PUNE |
+--------+-------------------------+--------------+--------------+

Ans. Output:
RAVI KUMAR
NISHANT JAIN
DEEPAK PRAKASH

32 Write the names of any two constraints and their respective uses in SQL. 2

Ans. Two constraints in SQL are:


(i) PRIMARY KEY
Page 106 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
(ii) NOT NULL

Uses:
(i) PRIMARY KEY: It is used to make an attribute/ column a Primary Key so that all the
values will be Unique. (No duplicate values are allowed in the column/attribute declared
as PRIMARY KEY.)

(ii) NOT NULL: It is used so that none of the values can be left or no column will be NULL

36 Write the outputs of the SQL queries (i) to (iii) based on the relations CUSTOMER and 3
TRANSACTION given below:

Table: CUSTOMER
ACNO NAME GENDER BALANCE
C1 RISHABH M 15000
C2 AAKASH M 12500
C3 INDIRA F 9750
C4 TUSHAR M 14600
C5 ANKITA F 22000

Table: TRANSACTION
ACNO TDATE AMOUNT TYPE
C1 2020-07- 1000 DEBIT
21
C5 2019-12- 1500 CREDIT
31
C3 2020-01- 2000 CREDIT
01

(i) SELECT MAX(BALANCE), MIN(BALANCE)FROM CUSTOMER WHERE GENDER = 'M';

(ii) SELECT SUM(AMOUNT), TYPE FROM TRANSACTION GROUP BY TYPE;

(iii) SELECT NAME, TDATE, AMOUNT FROM CUSTOMER C, TRANSACTION T WHERE C.ACNO =
T.ACNO AND TYPE = 'CREDIT';

Creating Tables CUSTOMER and TRANSACTION in Database CBSE2021, inserting records in


these two tables:

mysql> use cbse2021;

mysql> CREATE TABLE CUSTOMER (ACNO CHAR(2), NAME VARCHAR(10), GENDER CHAR(1),
BALANCE DECIMAL(8,2));

mysql> INSERT INTO CUSTOMER VALUES ('C1', 'RISHABH', 'M', 15000);

Page 107 of 154


OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
mysql> INSERT INTO CUSTOMER VALUES ('C2', 'AAKASH', 'M', 12500);

mysql> INSERT INTO CUSTOMER VALUES ('C3', 'INDIRA', 'F', 9750);

mysql> INSERT INTO CUSTOMER VALUES ('C4', 'TUSHAR', 'M', 14600);

mysql> INSERT INTO CUSTOMER VALUES ('C5', 'ANKITA', 'F', 22000);

mysql> SELECT * FROM CUSTOMER;


+---------+-------------+------------+---------------+
| ACNO | NAME | GENDER | BALANCE |
+---------+-------------+------------+---------------+
| C1 | RISHABH | M | 15000.00 |
| C2 | AAKASH | M | 12500.00 |
| C3 | INDIRA | F | 9750.00 |
| C4 | TUSHAR | M | 14600.00 |
| C5 | ANKITA | F | 22000.00 |
+---------+-------------+------------+---------------+

mysql> CREATE TABLE TRANSACTION (ACNO CHAR(2), TDATE DATE, AMOUNT


DECIMAL(8,2), TYPE VARCHAR(7));

mysql> INSERT INTO TRANSACTION VALUES ('C1', '2020-07-21', 1000, 'DEBIT' );

mysql> INSERT INTO TRANSACTION VALUES ('C5', '2019-12-31', 1500, 'CREDIT' );

mysql> INSERT INTO TRANSACTION VALUES ('C3', '2020-01-01', 2000, 'CREDIT' );

mysql> SELECT * FROM TRANSACTION;


+---------+-----------------+---------------+-----------+
| ACNO | TDATE | AMOUNT | TYPE |
+---------+-----------------+---------------+-----------+
| C1 | 2020-07-21 | 1000.00 | DEBIT |
| C5 | 2019-12-31 | 1500.00 | CREDIT |
| C3 | 2020-01-01 | 2000.00 | CREDIT |
+---------+-----------------+---------------+-----------+
Ans. (i) SELECT MAX(BALANCE), MIN(BALANCE)FROM CUSTOMER WHERE GENDER = 'M';
Output:
+----------------------+-----------------------+
| MAX(BALANCE) | MIN(BALANCE) |
+-----------------------+---------------------+
| 15000.00 | 12500.00 |
+----------------------+----------------------+
(ii) SELECT SUM(AMOUNT), TYPE FROM TRANSACTION GROUP BY TYPE;

Output:
+-----------------------+----------+

Page 108 of 154


OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
| SUM(AMOUNT) | TYPE |
+-----------------------+----------+
| 1000.00 | DEBIT |
| 3500.00 | CREDIT |
+----------------------+------------+
(iii) SELECT NAME, TDATE, AMOUNT FROM CUSTOMER C, TRANSACTION T WHERE C.ACNO =
T.ACNO AND TYPE = 'CREDIT';

Output:
+------------+----------------+---------------+
| NAME | TDATE | AMOUNT |
+------------+----------------+---------------+
| INDIRA | 2020-01-01 | 2000.00 |
| ANKITA | 2019-12-31 | 1500.00 |
+------------+----------------+---------------+

39 Write SQL statements for the following queries (i) to (v) based on the relations CUSTOMER 5
and TRANSACTION given below:

Table: CUSTOMER
ACNO NAME GENDER BALANCE
C1 RISHABH M 15000
C2 AAKASH M 12500
C3 INDIRA F 9750
C4 TUSHAR M 14600
C5 ANKITA F 22000

Table: TRANSACTION
ACNO TDATE AMOUNT TYPE
C1 2020-07- 1000 DEBIT
21
C5 2019-12- 1500 CREDIT
31
C3 2020-01- 2000 CREDIT
01

(i) To display all information about the CUSTOMERs whose NAME starts with 'A'.

(ii) To display the NAME and BALANCE of Female CUSTOMERs (with GENDER as 'F') whose
TRANSACTION Date (TDATE) is in the year 2019.

(iii) To display the total number of CUSTOMERs for each GENDER.

(iv) To display the CUSTOMER NAME and BALANCE in ascending order of GENDER.

(v) To display CUSTOMER NAME and their respective INTEREST for all CUSTOMERs where
Page 109 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
INTEREST is calculated as 8% of BALANCE.

Ans. (i) SELECT * FROM CUSTOMER WHERE NAME LIKE 'A%';


Output: (Don’t write the output)
+---------+------------+-------------+--------------+
| ACNO | NAME | GENDER | BALANCE |
+---------+------------+-------------+--------------+
| C2 | AAKASH | M | 12500.00 |
| C5 | ANKITA | F | 22000.00 |
+---------+------------+-------------+-------------+
(ii) SELECT NAME, BALANCE FROM CUSTOMER C, TRANSACTION T WHERE C.ACNO = T.ACNO
AND GENDER='F' AND TDATE LIKE '2019%';
Output: (Don’t write the output)
+-----------+---------------+
| NAME | BALANCE |
+-----------+--------------+
| ANKITA | 22000.00 |
+-----------+--------------+
(iii) SELECT GENDER, COUNT(*) FROM CUSTOMER GROUP BY GENDER;
Output: (Don’t write the output)
+------------+---------------+
| GENDER | COUNT(*) |
+------------+---------------+
|M | 3 |
|F | 2 |
+------------+---------------+
(iv) SELECT NAME, BALANCE FROM CUSTOMER ORDER BY GENDER ASC;
Output: (Don’t write the output)
+--------------+--------------+
| NAME | BALANCE |
+--------------+--------------+
| INDIRA | 9750.00 |
| ANKITA | 22000.00 |
| RISHABH | 15000.00 |
| AAKASH | 12500.00 |
| TUSHAR | 14600.00 |
+-------------+--------------+
(v) SELECT NAME, 0.08*BALANCE 'INTEREST ' FROM CUSTOMER;
Output: (Don’t write the output)
+-------------+---------------+
| NAME | INTEREST |
+-------------+---------------+
| RISHABH | 1200.0000 |
| AAKASH | 1000.0000 |
| INDIRA | 780.0000 |
| TUSHAR | 1168.0000 |
| ANKITA | 1760.0000 |
Page 110 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
+-------------+---------------+

CBSE 2020 COMPT


4 (a) Which SQL command is used to add a new attribute in a table? 1

Ans. (a) The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.
Syntax:
ALTER TABLE table_name ADD column_name datatype;
Example:
ALTER TABLE Customers ADD Email varchar(25);

(b) Which SQL aggregate function is used to count all records of a table? 1
Ans. Count(*) function is used to count all records of a table.
Example:
select count(*) AS studentCount from student;

(c) Which clause is used with a SELECT command in SQL to display the records in ascending 1
order of an attribute?

Ans. The SQL ORDER BY clause is used to sort the data in ascending or descending order, based
on one or more columns. Some databases sort the query results in an ascending order by
default.

Example:
SELECT * FROM CUSTOMERS ORDER BY NAME, SALARY;

(d) Write the full form of the following abbreviations: 1


(i) DDL
(ii) DML

Ans> (i) DDL: Data Definition Language


(ii) DML: Data Manipulation Language

(e) Observe the following tables, EMPLOYEES and DEPARTMENT carefully and answer the 2
questions that follow:

TABLE: EMPLOYEES
ENO ENAME DOJ DNO
E1 NUSRAT 2001-11-21 D3
E2 KABIR 2005-10-25 D1

TABLE: DEPARTMENT
DNO DNAME
D1 ACCOUNTS
D2 HR
D3 ADMIN

Page 111 of 154


OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
(i) What is the Degree of the table EMPLOYEES? What is the cardinality of the table
DEPARTMENT?

(ii) What is a Primary Key? Explain.

Ans. (i) Degree of the table EMPLOYEES=4 (Degree in number of columns/attributes in a table or
relation.)

Cardinality of the table DEPARTMENT=3 (Cardinality in number of rows/tuples in a table


excluding the header row)

(ii) The primary key of a relational table uniquely identifies each record in the table.

(e) OR 2
Differentiate between Selection and Projection operations in context of a Relational
Database. Also, illustrate the difference with one supporting example of each.
Ans. The difference between selection and projection is Selection means which rows are to be
returned and Projection means choosing which columns the query shall return.
Give suitable Example:

(g) Creating Database CBSE2020, using database CBSE2020, Creating Two Tables: CUSTOMERS
and PURCHASES and Inserting records in these two tables:

mysql> CREATE DATABASE CBSE2020;


mysql> USE CBSE2020;
mysql> CREATE TABLE CUSTOMERS(CNO CHAR(2),CNAME VARCHAR(10), CITIES
VARCHAR(10));
mysql> INSERT INTO CUSTOMERS VALUES ('C1', 'SANYAM', 'DELHI');
mysql> INSERT INTO CUSTOMERS VALUES ('C2', 'SHRUTI', 'DELHI');
mysql> INSERT INTO CUSTOMERS VALUES ('C3', 'MEHER', 'MUMBAI');
mysql> INSERT INTO CUSTOMERS VALUES ('C4', 'SAKSHI', 'CHENNAI');
mysql> INSERT INTO CUSTOMERS VALUES ('C5', 'RITESH', 'INDORE');
mysql> INSERT INTO CUSTOMERS VALUES ('C6', 'RAHUL', 'DELHI');
mysql> INSERT INTO CUSTOMERS VALUES ('C7', 'AMEER', 'CHENNAI');
mysql> INSERT INTO CUSTOMERS VALUES ('C8', 'MINAKSHI', 'BANGALORE');
mysql> INSERT INTO CUSTOMERS VALUES ('C9', 'ANSHUL', 'MUMBAI');
mysql> SELECT * FROM CUSTOMERS;
+--------+--------------+------------------+
| CNO | CNAME | CITIES |
+--------+---------------+-----------------+
| C1 | SANYAM | DELHI |
| C2 | SHRUTI | DELHI |
| C3 | MEHER | MUMBAI |
| C4 | SAKSHI | CHENNAI |
| C5 | RITESH | INDORE |
| C6 | RAHUL | DELHI |
Page 112 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
| C7 | AMEER | CHENNAI |
| C8 | MINAKSHI | BANGALORE |
| C9 | ANSHUL | MUMBAI |
+--------+---------------+------------------+

mysql> CREATE TABLE PURCHASES (SNO CHAR(3), QTY INT(3) , PUR_DATE DATE, CNO
CHAR(3));
mysql> INSERT INTO PURCHASES VALUES ('S1', 15, '2018-12-25', 'C2O');
mysql> INSERT INTO PURCHASES VALUES ('S2', 10, '2018-11-10', 'C1');
mysql> INSERT INTO PURCHASES VALUES ('S3', 12, '2018-11-10', 'C4');
mysql> INSERT INTO PURCHASES VALUES ('S4', 7, '2019-01-12', 'C7');
mysql> INSERT INTO PURCHASES VALUES ('S5', 11, '2019-02-12', 'C2');
mysql> INSERT INTO PURCHASES VALUES ('S6', 10, '2018-10-12', 'C6');
mysql> INSERT INTO PURCHASES VALUES ('S7', 5 ,'2019-05-09', 'C8');
mysql> INSERT INTO PURCHASES VALUES ('S8', 20, '2019-05-09', 'C3');
mysql> INSERT INTO PURCHASES VALUES ('S9', 8, '2018-05-09', 'C9');
mysql> INSERT INTO PURCHASES VALUES ('S10', 15, '2018-11-12', 'C5');
mysql> INSERT INTO PURCHASES VALUES ('S11', 6, '2018-08-04', 'C7');
mysql> SELECT * FROM PURCHASES;
+--------+-------+-----------------+--------+
| SNO | QTY | PUR_DATE | CNO |
+--------+-------+-----------------+--------+
| S1 | 15 | 2018-12-25 | C2O |
| S2 | 10 | 2018-11-10 | C1 |
| S3 | 12 | 2018-11-10 | C4 |
| S4 | 7 | 2019-01-12 | C7 |
| S5 | 11 | 2019-02-12 | C2 |
| S6 | 10 | 2018-10-12 | C6 |
| S7 | 5 | 2019-05-09 | C8 |
| S8 | 20 | 2019-05-09 | C3 |
| S9 | 8 | 2018-05-09 | C9 |
| S10 | 15 | 2018-11-12 | C5 |
| S11 | 6 | 2018-08-04 | C7 |
+--------+-------+-----------------+-------+

4 (g) Write outputs for SQL queries (i) to (iii), which are based on the following tables, 3
CUSTOMERS and PURCHASES:

Table: CUSTOMERS Table: PURCHASES


+--------+--------------+------------------+ +--------+-------+-----------------+--------+

Page 113 of 154


OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
| CNO | CNAME | CITIES | | SNO | QTY | PUR_DATE | CNO |
+--------+---------------+-----------------+ +--------+-------+-----------------+--------+
| C1 | SANYAM | DELHI | | S1 | 15 | 2018-12-25 | C2O |
| C2 | SHRUTI | DELHI | | S2 | 10 | 2018-11-10 | C1 |
| C3 | MEHER | MUMBAI | | S3 | 12 | 2018-11-10 | C4 |
| C4 | SAKSHI | CHENNAI | | S4 | 7 | 2019-01-12 | C7 |
| C5 | RITESH | INDORE | | S5 | 11 | 2019-02-12 | C2 |
| C6 | RAHUL | DELHI | | S6 | 10 | 2018-10-12 | C6 |
| C7 | AMEER | CHENNAI | | S7 | 5 | 2019-05-09 | C8 |
| C8 | MINAKSHI | BANGALORE | | S8 | 20 | 2019-05-09 | C3 |
| C9 | ANSHUL | MUMBAI | | S9 | 8 | 2018-05-09 | C9 |
+--------+---------------+------------------+ | S10 | 15 | 2018-11-12 | C5 |
| S11 | 6 | 2018-08-04 | C7 |
+--------+-------+-----------------+-------+

(i) SELECT COUNT(DISTINCT CITIES) FROM CUSTOMERS;

(ii) SELECT MAX(PUR_DATE) FROM PURCHASES;

(iii) SELECT CNAME, QTY, PUR_DATE FROM CUSTOMERS, PURCHASES WHERE CUSTOMERS.CNO
= PURCHASES.CNO AND QTY IN (10,20);

Ans. (i) SELECT COUNT(DISTINCT CITIES) FROM CUSTOMERS;


Output:
+-----------------------------------+
| COUNT(DISTINCT CITIES) |
+-----------------------------------+
| 5 |
+-----------------------------------+
(ii) SELECT MAX(PUR_DATE) FROM PURCHASES;
Output:
+-------------------------+
| MAX(PUR_DATE) |
+-------------------------+
| 2019-05-09 |
+-------------------------+
(iii) SELECT CNAME, QTY, PUR_DATE FROM CUSTOMERS, PURCHASES WHERE CUSTOMERS.CNO
= PURCHASES.CNO AND QTY IN (10,20);
Output:
+-------------+--------+-----------------+
| CNAME | QTY | PUR_DATE |
+-------------+--------+-----------------+
| SANYAM | 10 | 2018-11-10 |
| MEHER | 20 | 2019-05-09 |
| RAHUL | 10 | 2018-10-12 |
+--------------+-------+-----------------+
4 (h) Write SQL queries for (i) to (iv), which are based on the tables: CUSTOMERS and PURCHASES 4
Page 114 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
given in the question 4(g) :

(i) To display details of all CUSTOMERS whose CITIES are neither Delhi nor Mumbai.

(ii) To display the CNAME and CITIES of all CUSTOMERS in ascending order of their CNAME.

(iii) To display the number of CUSTOMERS along with their respective CITIES in each of the
CITIES.

(iv) To display details of all PURCHASES whose Quantity is more than 15.

Ans. (i) SELECT * FROM CUSTOMERS WHERE CITIES NOT IN ('DELHI','MUMBAI');


Output: Don’t write the output.
+-------+---------------+-------------------+
| CNO | CNAME | CITIES |
+-------+---------------+--------------------+
| C4 | SAKSHI | CHENNAI |
| C5 | RITESH | INDORE |
| C7 | AMEER | CHENNAI |
| C8 | MINAKSHI | BANGALORE |
+-------+----------------+-------------------+

(ii) SELECT CNAME, CITIES FROM CUSTOMERS ORDER BY CNAME ASC;


OR
SELECT CNAME, CITIES FROM CUSTOMERS ORDER BY CNAME;
Output: Don’t write the output.
+---------------+------------------+
| CNAME | CITIES |
+---------------+-------------------+
| AMEER | CHENNAI |
| ANSHUL | MUMBAI |
| MEHER | MUMBAI |
| MINAKSHI | BANGALORE |
| RAHUL | DELHI |
| RITESH | INDORE |
| SAKSHI | CHENNAI |
| SANYAM | DELHI |
| SHRUTI | DELHI |
+---------------+-------------------+

(iii) SELECT CITIES, COUNT(*) FROM CUSTOMERS GROUP BY CITIES;


Output: Don’t write the output.
+-------------------+--------------+
| CITIES | COUNT(*) |
+-------------------+--------------+
| DELHI | 3 |
| MUMBAI | 2 |
Page 115 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
| CHENNAI | 2 |
| INDORE | 1 |
| BANGALORE | 1 |
+------------------+--------------+

(iv) SELECT * FROM PURCHASES WHERE QTY>15;


Output: Don’t write the output.
+-------+-------+-----------------+--------+
| SNO | QTY | PUR_DATE | CNO |
+-------+-------+-----------------+--------+
| S8 | 20 | 2019-05-09 | C3 |
+------+--------+-----------------+--------+

CBSE SQP 2020


4 (a) Which key word is used to sort the records of a table in descending order? 1

Ans. DESC

(b) Which clause is used to sort the records of a table? 1

Ans. ORDER BY

(c) Which command is used to modify the records of the table? 1

Ans. UPDATE

(d) Which clause is used to remove the duplicating rows of the table? 1

Ans. DISTINCT

(e) Differentiate between Primary key and Candidate key. 2


OR
Differentiate between Degree and Cardinality.
Ans. A Candidate Key can be any column or a combination of columns that can qualify as
unique key in database. There can be multiple Candidate Keys in one table where as A
Primary Key is a column or a combination of columns that uniquely identify a record. Only
one Candidate Key can be Primary Key.

OR

Degree: It is the total number of attributes in the table.

Cardinality: It is the total number of tuples in the table

Creating Table: Student in database CBSE2020


Page 116 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

mysql> USE CBSE2020;

mysql> CREATE TABLE STUDENT (RollNo INT(2), Name VARCHAR(10), Class VARCHAR(3),
DOB DATE, Gender CHAR(1), City VARCHAR(10), Marks INT(3));

mysql> INSERT INTO STUDENT VALUES (1, 'Nanda','X','1995-06-06','M','Agra',551);

mysql> INSERT INTO STUDENT VALUES (2, 'Saurabh','XII','1993-05-07','M','Mumbai',462);

mysql> INSERT INTO STUDENT VALUES (3, 'Sanal','XI','1994-05-06','F','Delhi',400);

mysql> INSERT INTO STUDENT VALUES (4, 'Trisla','XII','1995-08-08','F','Mumbai',450);

mysql> INSERT INTO STUDENT VALUES (5, 'Store','XII','1995-10-08','M','Delhi',369);

mysql> INSERT INTO STUDENT VALUES (6, 'Marisla','XI','1994-12-12','F','Dubai',250);

mysql> INSERT INTO STUDENT VALUES (7, 'Neha','X','1995-12-08','F','Moscow',377);

mysql> INSERT INTO STUDENT VALUES (8, 'Nishant','X','1995-06-12','M','Moscow',489);

mysql> SELECT * FROM STUDENT;


+---------+----------+-------+--------------+----------+----------+--------+
| RollNo | Name | Class | DOB | Gender | City | Marks |
+---------+----------+-------+--------------+----------+----------+--------+
| 1 | Nanda | X | 1995-06-06 | M | Agra | 551 |
| 2 | Saurabh | XII | 1993-05-07 | M | Mumbai | 462 |
| 3 | Sanal | XI | 1994-05-06 | F | Delhi | 400 |
| 4 | Trisla | XII | 1995-08-08 | F | Mumbai | 450 |
| 5 | Store | XII | 1995-10-08 | M | Delhi | 369 |
| 6 | Marisla | XI | 1994-12-12 | F | Dubai | 250 |
| 7 | Neha | X | 1995-12-08 | F | Moscow | 377 |
| 8 | Nishant | X | 1995-06-12 | M | Moscow | 489 |
+---------+-----------+------+--------------+---------+-----------+-------+

4 (g) Write a output for SQL queries (i) to (iii), which are based on the table: STUDENT? 3
Table: STUDENT
+---------+----------+-------+--------------+----------+----------+--------+
| RollNo | Name | Class | DOB | Gender | City | Marks |
+---------+----------+-------+--------------+----------+----------+--------+
| 1 | Nanda | X | 1995-06-06 | M | Agra | 551 |
| 2 | Saurabh | XII | 1993-05-07 | M | Mumbai | 462 |
| 3 | Sanal | XI | 1994-05-06 | F | Delhi | 400 |
| 4 | Trisla | XII | 1995-08-08 | F | Mumbai | 450 |
| 5 | Store | XII | 1995-10-08 | M | Delhi | 369 |
| 6 | Marisla | XI | 1994-12-12 | F | Dubai | 250 |

Page 117 of 154


OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
| 7 | Neha | X | 1995-12-08 | F | Moscow | 377 |
| 8 | Nishant | X | 1995-06-12 | M | Moscow | 489 |
+---------+-----------+------+--------------+---------+-----------+-------+

(i) SELECT COUNT(*), City FROM STUDENT GROUP BY CITY HAVING COUNT(*)>1;

(ii) SELECT MAX(DOB),MIN(DOB) FROM STUDENT;

(iii) SELECT NAME,GENDER FROM STUDENT WHERE CITY=”Delhi”;

Ans. (i) SELECT COUNT(*), City FROM STUDENT GROUP BY CITY HAVING COUNT(*)>1;
Output:
+--------------+-----------+
| COUNT(*) | City |
+--------------+-----------+
| 2 | Mumbai |
| 2 | Delhi |
| 2 | Moscow |
+--------------+----------+

(ii) SELECT MAX(DOB),MIN(DOB) FROM STUDENT;


Output:
+-----------------+-----------------+
| MAX(DOB) | MIN(DOB) |
+-----------------+-----------------+
| 1995-12-08 | 1993-05-07 |
+-----------------+-----------------+

(iii) SELECT NAME,GENDER FROM STUDENT WHERE CITY="Delhi";


Output:
+----------+-------------+
| NAME | GENDER |
+----------+-------------+
| Sanal | F |
| Store | M |
+----------+------------+

4 (h) Write SQL queries for (i) to (iv), which are based on the table: STUDENT:
Table: STUDENT
+---------+----------+-------+--------------+----------+----------+--------+
| RollNo | Name | Class | DOB | Gender | City | Marks |
+---------+----------+-------+--------------+----------+----------+--------+
| 1 | Nanda | X | 1995-06-06 | M | Agra | 551 |
| 2 | Saurabh | XII | 1993-05-07 | M | Mumbai | 462 |
| 3 | Sanal | XI | 1994-05-06 | F | Delhi | 400 |
| 4 | Trisla | XII | 1995-08-08 | F | Mumbai | 450 |
| 5 | Store | XII | 1995-10-08 | M | Delhi | 369 |
Page 118 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
| 6 | Marisla | XI | 1994-12-12 | F | Dubai | 250 |
| 7 | Neha | X | 1995-12-08 | F | Moscow | 377 |
| 8 | Nishant | X | 1995-06-12 | M | Moscow | 489 |
+---------+-----------+------+--------------+---------+-----------+-------+

(i) To display the records from table student in alphabetical order as per the name of the
student.

(ii) To display Class, Dob and City whose marks is between 450 and 551.

(iii) To display Name, Class and total number of students who have secured more than 450
marks, class wise

(iv) To increase marks of all students by 20 whose class is “XII”

Ans. (i) SELECT * FROM STUDENT ORDER BY NAME ASC;


Output: Don’t write the output
+---------+----------+-------+--------------+---------+----------+--------+
| RollNo | Name | Class | DOB | Gender | City | Marks |
+---------+----------+-------+--------------+---------+----------+--------+
| 6 | Marisla | XI | 1994-12-12 | F | Dubai | 250 |
| 1 | Nanda | X | 1995-06-06 | M | Agra | 551 |
| 7 | Neha | X | 1995-12-08 | F | Moscow | 377 |
| 8 | Nishant | X | 1995-06-12 | M | Moscow | 489 |
| 3 | Sanal | XI | 1994-05-06 | F | Delhi | 400 |
| 2 | Saurabh | XII | 1993-05-07 | M | Mumbai | 462 |
| 5 | Store | XII | 1995-10-08 | M | Delhi | 369 |
| 4 | Trisla | XII | 1995-08-08 | F | Mumbai | 450 |
+---------+---------+--------+---------------+--------+----------+--------+

(ii) SELECT CLASS, DOB, CITY FROM STUDENT WHERE MARKS BETWEEN 450 AND 551;
Output: Don’t write the output
+---------+--------------+-----------+
| CLASS | DOB | CITY |
+---------+--------------+-----------+
|X | 1995-06-06 | Agra |
| XII | 1993-05-07 | Mumbai |
| XII | 1995-08-08 | Mumbai |
|X | 1995-06-12 | Moscow |
+---------+--------------+-----------+

(iii) SELECT NAME, CLASS , COUNT(*) FROM STUDENT WHERE MARKS>450 GROUP BY CLASS;
Output: Don’t write the output
+-----------+----------+--------------+
| NAME | CLASS | COUNT(*) |
+-----------+----------+--------------+
| Nanda | X | 2 |
Page 119 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
| Saurabh | XII | 1 |
+-----------+----------+-------------+

(iv) UPDATE STUDENT SET MARKS=MARKS+20 WHERE CLASS='XII';


Output: Don’t write the output

mysql> SELECT * FROM STUDENT;


+---------+-----------+-------+--------------+---------+-----------+--------+
| RollNo | Name | Class | DOB | Gender | City | Marks |
+---------+----------+--------+--------------+---------+-----------+--------+
| 1 | Nanda | X | 1995-06-06 | M | Agra | 551 |
| 2 | Saurabh | XII | 1993-05-07 | M | Mumbai | 482 |
| 3 | Sanal | XI | 1994-05-06 | F | Delhi | 400 |
| 4 | Trisla | XII | 1995-08-08 | F | Mumbai | 470 |
| 5 | Store | XII | 1995-10-08 | M | Delhi | 389 |
| 6 | Marisla | XI | 1994-12-12 | F | Dubai | 250 |
| 7 | Neha | X | 1995-12-08 | F | Moscow | 377 |
| 8 | Nishant | X | 1995-06-12 | M | Moscow | 489 |
+--------+-----------+-------+---------------+--------+-----------+--------+

Compartment Examination, 2022

3 Differentiate between Degree and Cardinality in the context of Relational Data Model. 2

Ans. Degree: Number of columns in a table is called its Degree.

Cardinality: Number of rows in a table is called its Cardinality.

OR

Any valid example to differentiate between Degree & cardinality.

(2 marks for writing any valid difference)

OR

(1 mark each for writing correct definition/example/explanation of Degree and


Cardinality)

4 Consider the following table EMPLOYEE in a Database COMPANY: 2

Page 120 of 154


OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

Assume that the required library for establishing the connection between
Python and MySQL is already imported in the given Python code.

Also assume that DB is the name of the database connection for the given
table EMPLOYEE stored in the database COMPANY.

Predict the output of the following Python code:

import mysql.connector
DB=mysql.connector.connect(host="localhost",user="root",passwd='yadav')
CUR = DB.cursor()
CUR.execute("USE COMPANY")
CUR.execute("SELECT * FROM EMPLOYEE WHERE DEPT = 'AC' ")
for i in range(2) :
R=CUR.fetchone()
print(R[0], R[1], sep ="#")

Ans. H1001#Avneet
A1003#Amina

(1 mark for writing each correct line of output)


(Note: Deduct ½ mark, if line break or/and # sign is not correctly written)

Steps for creating above database and Table:

mysql> create Database COMPANY;


Query OK, 1 row affected (0.04 sec)

mysql> use COMPANY;


Database changed

mysql> Create Table EMPLOYEE (E_ID char(6), NAME varchar(10), DEPT varchar(4));
Query OK, 0 rows affected (0.15 sec)

Page 121 of 154


OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

mysql> insert into EMPLOYEE values ('H1001', 'Avneet', 'AC');


Query OK, 1 row affected (0.03 sec)

mysql> insert into EMPLOYEE values ('A1002', 'Rakesh', 'HR');


Query OK, 1 row affected (0.01 sec)

mysql> insert into EMPLOYEE values ('A1003', 'Amina', 'AC');


Query OK, 1 row affected (0.01 sec)

mysql> insert into EMPLOYEE values ('H1002', 'Simon', 'HR');


Query OK, 1 row affected (0.01 sec)

mysql> insert into EMPLOYEE values ('A1004', 'Pratik', 'AC');


Query OK, 1 row affected (0.01 sec)

mysql> select * from EMPLOYEE;


+-------+--------+------+
| E_ID | NAME | DEPT |
+-------+--------+------+
| H1001 | Avneet | AC |
| A1002 | Rakesh | HR |
| A1003 | Amina | AC |
| H1002 | Simon | HR |
| A1004 | Pratik | AC |
+-------+--------+------+

5 Write the output of the SQL queries (a) to (d) based on the table TRAVEL given below: 2

Table: TRAVEL

Page 122 of 154


OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

(a) SELECT START, END FROM TRAVEL WHERE FARE <=4000;

(b) SELECT T_ID, FARE FROM TRAVEL WHERE T_DATE LIKE '2021-12-%' ;

(c) SELECT T_ID, T_DATE FROM TRAVEL WHERE END = 'CHENNAI' ORDER BY FARE ;

(d) SELECT START, MIN(FARE)FROM TRAVEL GROUP BY START ;

Ans. (a)

(b)

(c)

(d)

6 Write the output of the SQL queries (a) and (b) based on the following two tables FLIGHT
and PASSENGER belonging to the same database:
Page 123 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

Table: FLIGHT

Table: PASSENGER

(a) SELECT NAME, DEPART FROM FLIGHT NATURAL JOIN PASSENGER ;

(b) SELECT NAME, FARE FROM PASSENGER P, FLIGHT F WHERE F.FNO = P.FNO AND F.DEPART
= 'MUMBAI' ;

Ans. (a)

Page 124 of 154


OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
(b)

7 Explain Primary Key in the context of Relational Database Model. Support your answer 2
with suitable example.

Ans. An attribute or a group of attributes that identifies a tuple uniquely is known as a Primary
Key of the table.

(1 mark for writing only the correct explanation/example of primary key) (1 mark for
writing appropriate example)

OR

Consider the following table BATSMEN:

(a) Identify and write the name of the Candidate Keys in the given table BATSMEN.

(b) How many tuples are there in the given table BATSMEN ?

Page 125 of 154


OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
Ans. (a) PNO, NAME

(b) 5

9 (a) A SQL table BOOKS contains the following column names:


BOOKNO,BOOKNAME, QUANTITY, PRICE, AUTHOR
Write the SQL statement to add a new column REVIEW to store the reviews of the book.

(b) Write the names of any two commands of DDL and any two commands of DML in SQL.

Ans. (a) ALTER TABLE BOOKS ADD REVIEW VARCHAR(20) ;

(½ mark for writing ALTER TABLE BOOKS)


(½ mark for writing ADD REVIEW VARCHAR(any size);
OR ADD REVIEW CHAR(any size);)

(b) DDL Commands : CREATE, DROP, ALTER (any two)


DML Commands : INSERT, DELETE, UPDATE etc. (any two)

(½ mark each for correctly writing any two DDL and any two DML commands)

10 Rashmi has forgotten the names of the databases,tables and the structure of the tables 3
that she had created in Relational Database Management System (RDBMS) on her
computer.

(a) Write the SQL statement to display the names of all the databases present in RDBMS
application on her computer.

(b) Write the statement which she should execute to open the database named "STOCK".

(c) Write the statement which she should execute to display the structure of the table
"ITEMS" existing in the above opened database "STOCK".

Ans. (a) SHOW DATABASES ;

(1 Mark for writing correct SQL command)

(b) USE STOCK;

(1 Mark for writing correct SQL command)

(c) DESCRIBE ITEMS;

OR

DESC ITEMS;
Page 126 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

(1 Mark for writing correct SQL command)

11 Write SQL queries for (a) to (d) based on the tables CUSTOMER and TRANSACT given 4
below:

(a) Write the SQL statements to delete the records from table TRANSACT whose amount is
less than 1000.

(b) Write a query to display the total AMOUNT of all DEBITs and all CREDITs.

(c) Write a query to display the NAME and corresponding AMOUNT of all CUSTOMERs who
made a transaction type (TTYPE) of CREDIT.

(d) Write the SQL statement to change the Phone number of customer whose CNO is 1002 to
9988117700 in the table CUSTOMER.

Ans. (a) DELETE FROM TRANSACT WHERE AMOUNT <1000;

(½ Mark for writing DELETE FROM TRANSACT)


(½ Mark for writing WHERE AMOUNT

(b) SELECT TTYPE, SUM(AMOUNT)FROM TRANSACT GROUP BY TTYPE;

(½ Mark for writing SELECT TTYPE, SUM(AMOUNT) FROM TRANSACT)


(½ Mark for writing GROUP BY TTYPE)
(Note: No marks should be deducted, if TTYPE is not mentioned with SELECT)

(c) SELECT NAME, AMOUNT FROM CUSTOMER NATURAL JOIN TRANSACT WHERE
TTYPE='CREDIT';

OR

Page 127 of 154


OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

SELECT NAME, AMOUNT FROM CUSTOMER C, TRANSACT T WHERE C.CNO=T.CNO AND


TTYPE='CREDIT';

OR

SELECT NAME, AMOUNT FROM CUSTOMER, TRANSACT WHERE


CUSTOMER.CNO=TRANSACT.CNO AND TTYPE='CREDIT';

(½ mark for writing SELECT - FROM part correctly) (½ mark for writing WHERE part
correctly)

(d) UPDATE CUSTOMER SET PHONE=9988117700 WHERE CNO=1002;

(½ mark for writing UPDATE - SET part correctly)


(½ mark for writing WHERE part correctly)

CBSE-2022

3 Differentiate between the terms Attribute and Domain in the context of Relational Data 2
Model.

Ans.

(1 mark each for writing any correct explanation of Attribute and Domain)

4 Consider the following SQL table MEMBER in a SQL Database CLUB: 2

Page 128 of 154


OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

Assume that the required library for establishing the connection between Python and
MYSQL is already imported in the given Python code. Also assume that DB is the name of
the database connection for table MEMBER stored in the database CLUB. Predict the
output of the following code:

import mysql.connector
DB=mysql.connector.connect(host="localhost",user="root",passwd='yadav')

MYCUR = DB.cursor()
MYCUR.execute("USE CLUB")
MYCUR.execute("SELECT * FROM MEMBER WHERE ACTIVITY='GYM'")
R=MYCUR.fetchone()
for i in range(2):
R=MYCUR.fetchone()
print(R[0], R[1], sep ="#")

Ans. M1002#Pratik
M1004#Rakesh

(1 mark for writing each correct line of output)

(Note: Deduct ½ mark for missing # or writing the output in a single line OR writing any
additional line along with the correct output)

Steps to create above database and table:

mysql> use club;


Database changed
mysql> create table MEMBER (M_ID char(6), NAME varchar(10), ACTIVITY varchar(10));
Query OK, 0 rows affected (0.08 sec)

mysql> insert into MEMBER values ('M1001', 'Amina', 'GYM');


Query OK, 1 row affected (0.01 sec)

mysql> insert into MEMBER values ('M1002', 'Pratik', 'GYM');


Query OK, 1 row affected (0.01 sec)
Page 129 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

mysql> insert into MEMBER values ('M1003', 'Simon', 'SWIMMING');


Query OK, 1 row affected (0.01 sec)

mysql> insert into MEMBER values ('M1004', 'Rakesh', 'GYM');


Query OK, 1 row affected (0.01 sec)

mysql> insert into MEMBER values ('M1005' ,'Avneet', 'SWIMMING');


Query OK, 1 row affected (0.01 sec)

mysql> select * from member;


+-------+--------+----------+
| M_ID | NAME | ACTIVITY |
+-------+--------+----------+
| M1001 | Amina | GYM |
| M1002 | Pratik | GYM |
| M1003 | Simon | SWIMMING |
| M1004 | Rakesh | GYM |
| M1005 | Avneet | SWIMMING |
+-------+--------+----------+
5 rows in set (0.00 sec)

5 Write the output of the SQL queries (a) to (d) based on the table VACCINATION_DATA 2
given below:

(a) SELECT Name, Age FROM VACCINATION_DATA WHERE Dose2 IS NOT NULL AND Age > 40;

(b) SELECT City, COUNT(*) FROM VACCINATION_DATA GROUP BY City;

(c) SELECT DISTINCT City FROM VACCINATION_DATA;

(d) SELECT MAX(Dose1),MIN(Dose2)FROM VACCINATION_DATA;

Page 130 of 154


OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
Ans. (a)

(b)

(c)

(d)

6 Write the output of SQL queries (a) and (b) based on the following two tables DOCTOR 2
and PATIENT belonging to the same database:

(a) SELECT DNAME, PNAME FROM DOCTOR NATURAL JOIN PATIENT ;

(b) SELECT PNAME, ADMDATE, FEES FROM PATIENT P, DOCTOR D WHERE D.DNO = P.DNO
AND FEES > 1000;

Page 131 of 154


OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
Ans. (a)

(b)

7 Differentiate between Candidate Key and Primary Key in the context of Relational 2
Database Model.

Ans. A table may have more than one or a combination of attribute(s)that identifies a tuple
uniquely. All such attribute(s) are known as Candidate Keys.

Out of all the Candidate keys, the most appropriate one, which is used for unique
identification of the Tuples, is called the Primary Key.

OR
2

(a) Identify and write the name of the most appropriate column from the given table PLAYER
that can be used as a Primary key.

(b) Define the term Degree in relational data model. What is the Degree of the given table
PLAYER ?

Ans. (a) PNO

Page 132 of 154


OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

(b) Total number of columns/attributes in a table/relation is known as its Degree.

The Degree of the given table is 3.

9 (i) A SQL table ITEMS contains the following columns: INO, INAME, QUANTITY, PRICE, 1
DISCOUNT Write the SQL command to remove the column DISCOUNT from the table.

(ii) Categorize the following SQL commands into DDL and DML: 2
CREATE, UPDATE, INSERT, DROP

Ans. (i) ALTER TABLE ITEMS DROP COLUMN DISCOUNT; OR ALTER TABLE ITEMS DROP
DISCOUNT;

(ii) DDL Commands: CREATE, DROP


DML Commands: INSERT, UPDATE

10 Rohan is learning to work upon Relational Database Management System (RDBMS)


application. Help him to perform following tasks:

(a) To open the database named "LIBRARY".

(b) To display the names of all the tables stored in the opened database.

(c) To display the structure of the table "BOOKS" existing in the already opened database
"LIBRARY".

Ans. (a) USE LIBRARY ;

(b) SHOW TABLES; 4


OR
SHOW TABLES FROM LIBRARY;

(c) DESCRIBE BOOKS ;


OR
DESC BOOKS ;

11 Write SQL queries for (a) to (d) based on the tables PASSENGER and FLIGHT given below: 4

Page 133 of 154


OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

(a) Write a query to change the fare to 6000 of the flight whose FNO is F104.

(b) Write a query to display the total number of MALE and FEMALE PASSENGERS.

(c) Write a query to display the NAME, corresponding FARE and F_DATE of all PASSENGERS
who have a flight to START from DELHI.

(d) Write a query to delete the records of flights which end at Mumbai.

Ans. (a) UPDATE FLIGHT SET FARE=6000 WHERE FNO="F104";

(b) SELECT GENDER, COUNT(*) FROM PASSENGER GROUP BY GENDER;

(c) SELECT NAME, FARE, F_DATE FROM PASSENGER P, FLIGHT F


WHERE F.FNO= P.FNO AND START = 'DELHI';
OR
SELECT NAME, FARE, F_DATE FROM PASSENGER, FLIGHT
WHERE PASSENGER.FNO= FLIGHT.FNO AND START = 'DELHI';
OR
SELECT NAME, FARE, F_DATE FROM PASSENGER, FLIGHT
WHERE PASSENGER.FNO= FLIGHT.FNO AND START LIKE 'DELHI';
OR
SELECT NAME,FARE,F_DATE FROM PASSENGER NATURAL JOIN FLIGHT
WHERE START = 'DELHI';

(d) DELETE FROM FLIGHT WHERE END = "MUMBAI"; OR DELETE FROM FLIGHT WHERE END
LIKE "MUMBAI";

CBSE-2023

10 Fill in the blank. 1


_____ is a number of tuples in a relation.
(a) Attribute (b) Degree
(c) Domain (d) Cardinality
Page 134 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

Ans (d) Cardinality

12 Fill in the blank : 1


_______ clause is used with SELECT statement to display data in a sorted form with
respect to a specified column.
(a) WHERE (b) ORDER BY
(c) HAVING (d) DISTINCT

Ans. b) ORDER BY 1
16 fetchall() method fetches all rows in a result set and returns a :
(a) Tuple of lists (b) List of tuples
(c) List of strings (d) Tuple of strings

Ans. (b) List of tuples


22 Explain the concept of ‘‘Alternate Key’’ in a Relational Database Management System 2
with an appropriate example.

Ans. Alternate Keys are all the Candidate Keys of a RDBMS table, which have not been used
as a Primary Key.

Example:

In this example, any one of the RegNo and AadhaarNo can be used as a Primary Key. If
RegNo is used as the Primary Key then AadhaarNo is the Alternate Key.

25 (a) Differentiate between CHAR and VARCHAR data types in SQL with appropriate example. 2

(b) OR
Name any two DDL and any two DML commands.

Ans. (a) CHAR is of fixed length character(string) data type, which means, declaring CHAR (10)
implies to reserve spaces for 10 characters. If data does not have 10 characters (e.g.,
‘CITY’ has four characters), MySQL fills the remaining 6 characters with spaces padded on
the right.

VARCHAR is a variable-length character(string) data type. Declaring


VARCHAR (30) means a maximum of 30 characters can be stored but the actual allocated
bytes will depend on the length of the entered string. So ‘CITY’ in VARCHAR (30) will
occupy space needed to store 4 characters only and the remaining 26 will be released.

OR

Page 135 of 154


OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

CHAR data type is used to store strings of fixed length, while the VARCHAR data type is
used to store strings of variable-length.
Eg, to store ‘India’, VARCHAR(20) occupies only 5 bytes whereas CHAR(20) occupies 20
bytes.

(b) DDL – CREATE, ALTER, DROP (OR any two valid DDL command)
DML – INSERT, UPDATE, DELETE, SELECT

(OR any two valid DML command)

26 (a) Consider the following tables – LOAN and BORROWER: 1

Table: LOAN

Table: BORROWER

How many rows and columns will be there in the natural join of these two tables?

Ans. Rows: 2 (rows which have common key)


Columns: 4 ( sum of column from both tables, common column appears only ol

Page 136 of 154


OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

(b) Write the output of the queries (i) to (iv) based on the table, WORKER given below:
TABLE: WORKER

(i) SELECT F_NAME, CITY FROM WORKER ORDER BY STATE DESC;

(ii) SELECT DISTINCT (CITY) FROM WORKER;

Page 137 of 154


OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
(iii) SELECT F_NAME, STATE FROM WORKER WHERE L_NAME LIKE '_HA%';

(iv) SELECT CITY, COUNT (*) FROM WORKER GROUP BY CITY;

Ans. (i)

(ii)

(iii)

(iv)

28 (a) Write the outputs of the SQL queries (i) to (iv) based on the relations COMPUTER and 2
SALES given below:

Page 138 of 154


OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
Table: COMPUTER

Table: SALES

(i) SELECT MIN(PRICE), MAX(PRICE) FROM COMPUTER;

(ii) SELECT COMPANY, COUNT(*) FROM COMPUTER GROUP BY COMPANY HAVING


COUNT(COMPANY) > 1;

(iii) SELECT PROD_NAME, QTY_SOLD FROM COMPUTER C, SALES S WHERE


C.PROD_ID=S.PROD_ID AND TYPE = 'INPUT';

(iv) SELECT PROD_NAME, COMPANY, QUARTER FROM COMPUTER C, SALES S WHERE


C.PROD_ID=S. PROD_ID;

Ans. (i)

Page 139 of 154


OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
(ii)

(iii)

(iv)

(b) Write the command to view all databases. 1

Ans. SHOW DATABASES;

34 The school has asked their estate manager Mr. Rahul to maintain the data of all the labs
in a table LAB. Rahul has created a table and entered data of 5 labs.

(i) Identify the columns which can be considered as Candidate keys. 1

(ii) Write the degree and cardinality of the table. 1

Page 140 of 154


OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
(iii) Write the statements to: 2
(a) Insert a new row with appropriate data.
(b) Increase the capacity of all the labs by 10 students which are on ‘I’ Floor.

OR
(Option for part (iii) only)
(iii) Write the statements to:
(a) Add the constraint PRIMARY KEY to a column LABNO in the table.
(b) Delete the table LAB

Ans. (i) Candidate keys: LABNO and LAB_NAME

(ii) Degree = 5
Cardinality = 5

(iii) (a) INSERT INTO LAB VALUES('L006','PHYSICS','RAVI',25,'II');

(b) UPDATE LAB SET CAPACITY=CAPACITY+10 WHERE FLOOR='I';

OR
(Option for part (iii) only)
(iii) (a) ALTER TABLE LAB ADD PRIMARY KEY (LABNO);
(b) DROP TABLE LAB;

Compartment Examination, 2023

10 Fill in the blank : 1


In a relational model, tables are called _________, that store data for different columns.
(a) Attributes (b) Degrees
(c) Relations (d) Tuples

Ans, (c) Relations


12 Fill in the blank : 1
_________ statement of SQL is used to insert new records in a table.
(a) ALTER
(b) UPDATE
(c) INSERT
(d) CREATE

Ans. (c) INSERT

16 fetchone() method fetches only one row in a ResultSet and returns a __________. 1
(a) Tuple
(b) List
(c) Dictionary
Page 141 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
(d) String

Ans, (a) Tuple

22 Explain the usage of HAVING clause in GROUP BY command in RDBMS with the help of an 2
example.

Ans. HAVING is used for including conditions based on aggregate functions on groups.

SELECT DEPT, COUNT(*) FROM EMPLOYEE GROUP BY DEPT HAVING COUNT(*)>1;

Above command will return the number of employees in each department for the
departments having more than 1 employee.

25 (a) Differentiate between IN and BETWEEN operators in SQL with appropriate examples. 2

Ans. IN() function in MySQL finds a match in the given arguments.

Exprs IN (value1, value2, ...) The function returns 1 if Exprs is equal to any of the values in
the IN list, otherwise, returns 0.

Example:
SELECT NAME, SECTION FROM STUDENTS WHERE SECTION IN (‘C’ , ‘D’);

Whereas BETWEEN operator in MySQL selects values within a given range. The values can be
numbers, text, or dates. Range values are inclusive, both begin and end values are included.

Example: SELECT ROLL, MARKS FROM RESULT WHERE MARKS BETWEEN 75 AND 100;

OR
(b) Which of the following is NOT a DML command. DELETE, DROP, INSERT, UPDATE 2

Ans. DROP

26 (a) Consider the following tables - Student and Sport: 1

Table: Student

Table: Sport

Page 142 of 154


OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

What will be the output of the following statement ?


SELECT * FROM Student, Sport;

Ans.

(b) Write the output of the queries (i) to (iv) based on the table, GARMENT given below: 2

TABLE: GARMENT

(i) SELECT DISTINCT(COUNT(FCODE)) FROM GARMENT;

(ii) SELECT FCODE, COUNT(*), MIN(PRICE) FROM GARMENT GROUP BY FCODE HAVING
COUNT(*)>1;

(iii) SELECT TYPE FROM GARMENT WHERE ODR_DATE >'2021-02-01' AND PRICE <1500;

(iv) SELECT * FROM GARMENT WHERE TYPE LIKE 'F%';

Ans. (i)

(ii)

(iii)

Page 143 of 154


OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

(iv)

28 (a) Write the output of any three SQL queries (i) to (iv) based on the tables COMPANY and 3
CUSTOMER given below:

(i) SELECT PRODUCTNAME, COUNT(*)FROM COMPANY GROUP BY PRODUCTNAME HAVING


COUNT(*)> 2;

(ii) SELECT NAME, PRICE, PRODUCTNAME FROM COMPANY C, CUSTOMER CT WHERE C.CID =
CU.CID AND C_NAME = 'SONY';

Corrected query:

SELECT NAME, PRICE, PRODUCTNAME FROM COMPANY C, CUSTOMER CU WHERE C.CID =


CU.CID AND C_NAME = 'SONY';

(iii) SELECT DISTINCT CITY FROM COMPANY;

(iv) SELECT * FROM COMPANY WHERE C_NAME LIKE '%ON%';

Page 144 of 154


OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
Ans.: (i)

(ii)

(iii)

(iv)

32 (b) CBSE 2023 COMPT Connectivity 3


The table Bookshop in MySQL contains the following attributes :
B_code - Integer
B_name - String
Qty - Integer
Price - Integer
Note the following to establish connectivity between Python and MySQL on a ‘localhost’ :
● Username is ‘shop’
● Password is ‘Book’
● The table exists in a MySQL database named Bstore.
The code given below updates the records from the table Bookshop in MySQL.
Statement 1 - to form the cursor object.
Statement 2 - to execute the query that updates the Qty to 20 of the records whose
B_code is 105 in the table.
Statement 3 - to make the changes permanent in the database.
import mysql.connector as mysql
def update_book():
mydb=mysql.connect(host="localhost",user="shop",
passwd="Book", database = "Bstore")
mycursor=__________ # Statement 1
qry= "update Bookshop set Qty=20 where B_code=105"
___________________ # Statement 2
___________________ # Statement 3

mycursor.execute("Select * from Bookshop")


B_Details=mycursor.fetchall()
for i in B_Details:

Page 145 of 154


OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
print(i)

update_book()

Ans import mysql.connector as mysql


def update_book():
mydb=mysql.connect(host="localhost",user="shop",passwd="Book",database="Bstore")
mycursor=mydb.cursor()#__________ # Statement 1
qry= "update Bookshop set Qty=20 where B_code=105"
mycursor.execute(qry)#___________________ # Statement 2
#mydb.commit() #___________________ # Statement 3

mycursor.execute("COMMIT") #___________________ # Statement 3


mycursor.execute("Select * from Bookshop")
B_Details=mycursor.fetchall()
for i in B_Details:
print(i)

update_book()

OR

(ii) The table Bookshop in MySQL contains the following


attributes :
B_code - Integer
B_name - String
Qty - Integer
Price - Integer
Note the following to establish connectivity between Python and MySQL on a ‘localhost’ :
● Username is ‘shop’
● Password is ‘Book’
● The table exists in a MySQL database named Bstore.
The code given below reads the records from the table
Bookshop and displays all the records :
Statement 1 - to form the cursor object.
Statement 2 - to write the query to display all the records from the table.
Statement 3 - to read the complete result of the query into the object named B_Details,
from the table Bookshop in the database.
import mysql.connector as mysql
def Display_book():
mydb=mysql.connect(host="localhost",
user="shop",passwd="Book",database="Bstore")
mycursor=____________________ # Statement 1
mycursor.execute("_________") # Statement 2
B_Details=___________________ # Statement 3
for i in B_Details:

Page 146 of 154


OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
print(i)
Display_book()
Ans. import mysql.connector as mysql
def Display_book():
mydb=mysql.connect(host="localhost",user="root",passwd="yadav",database="Bstore")
mycursor=mydb.cursor()# Statement 1
mycursor.execute("SELECT * FROM BOOKSHOP") # Statement 2
B_Details=mycursor.fetchall() # Statement 3
for i in B_Details:
print(i)

Display_book()

34 The ABC Company is considering to maintain their salespersons records using SQL to store
data. As a database administrator, Alia created the table Salesperson and also entered the
data of 5 Salespersons.

Based on the data given above, answer the following questions:

(i) Identify the attribute that is best suited to be the Primary Key and why? 1

(ii) The Company has asked Alia to add another attribute in the table. What will be the new 1
degree and cardinality of the above table?

(iii) Write the statements to: 2

(a) Insert details of one salesman with appropriate data.

(b) Change the Region of salesman ‘SHYAM’ to ‘SOUTH’ in the table Salesperson.

OR (Option for part iii only)


(iii) Write the statement to:

(a) Delete the record of salesman RISHABH, as he has left the company.

(b) Remove an attribute REGION from the table

Page 147 of 154


OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
Ans. (i) Primary key: S_ID As it is non-repeating value and not expected to repeat for new rows too.
Note:
S_NAME should also be considered as a Primary Key

(ii) Degree = 6 and Cardinality = 5

(iii) (a) INSERT INTO SALESPERSON VALUES ("S006","JAYA",23,34000,'SOUTH');


(b) UPDATE SALESPERSON SET REGION='SOUTH' WHERE S_NAME="SHYAM";

OR (Option for part (iii) only)


(a) DELETE FROM SALESPERSON WHERE S_NAME="RISHABH";
(b) ALTER TABLE SALESPERSON DROP COLUMN REGION;

CBSE-SQP-2023-2024
2 In a table in MYSQL database, an attribute A of datatype varchar(20) has the value 1
“Keshav”. The attribute B of datatype char(20) has value “Meenakshi”. How many
characters are occupied by attribute A and attribute B?
a. 20,6 b. 6,20
c. 9,6 d. 6,9

Ans. d. 6,9

5 In MYSQL database, if a table, Alpha has degree 5 and cardinality 3, and another table, 1
Beta has degree 3 and cardinality 5, what will be the degree and cardinality of the
Cartesian product of Alpha and Beta?
a. 5,3 b. 8,15
c. 3,5 d. 15,8

Ans. b. 8,15

11 Fill in the blank: 1


The modem at the sender’s computer end acts as a ____________.
a. Model
b. Modulator
c. Demodulator
d. Convertor

Ans. b. Modulator

14 Which of the following statements is FALSE about keys in a relational database? 1


a. Any candidate key is eligible to become a primary key.
b. A primary key uniquely identifies the tuples in a relation.
c. A candidate key that is not a primary key is a foreign key.
d. A foreign key is an attribute whose value is derived from the primary key of another
Page 148 of 154
OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
relation.

Ans. Option c A candidate key that is not a primary key is a foreign key.

24 Ms. Shalini has just created a table named “Employee” containing columns Ename, 2
Department and Salary.
After creating the table, she realized that she has forgotten to add a primary key column
in the table. Help her in writing an SQL command to add a primary key column EmpId of
integer type to the table Employee.
Thereafter, write the command to insert the following record in the table:
EmpId- 999
Ename- Shweta
Department: Production
Salary: 26900

OR

Zack is working in a database named SPORT, in which he has created a table named
“Sports” containing columns SportId, SportName, no_of_players, and category.
After creating the table, he realized that the attribute, category has to be deleted from
the table and a new attribute TypeSport of data type string has to be added. This
attribute TypeSport cannot be left blank. Help Zack write the commands to complete
both the tasks.

Ans. SQL Command to add primary key:


ALTER TABLE Employee ADD EmpId INTEGER PRIMARY KEY;

As the primary key is added as the last field, the command for inserting data will be:
INSERT INTO Employee VALUES("Shweta","Production",26900,999);

Alternative answer:
INSERT INTO Employee(EmpId,Ename,Department,Salary) VALUES (999, "Shweta",
"Production", 26900);

OR

To delete the attribute, category:


ALTER TABLE Sports DROP category;

To add the attribute, TypeSport


ALTER TABLE Sports ADD TypeSport char(10) NOT NULL;

Page 149 of 154


OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

27 Consider the table CLUB given below and write the output of the SQL queries that follow. 3

(i) SELECT COUNT(DISTINCT SPORTS) FROM CLUB;

(ii) SELECT CNAME, SPORTS FROM CLUB WHERE DOAPP<"2006-04-30" AND CNAME LIKE
"%NA";

(iii) SELECT CNAME, AGE, PAY FROM CLUB WHERE GENDER = "MALE" AND PAY BETWEEN
1000 AND 1200;

Ans. (i)

Page 150 of 154


OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
(ii)

(iii)

29 Consider the table Personal given below: 3

Table: Personal

Based on the given table, write SQL queries for the following:

(i) Increase the salary by 5% of personals whose allowance is known.

(ii) Display Name and Total Salary (sum of Salary and Allowance) of all personals. The
column heading ‘Total Salary’ should also be displayed.

(iii) Delete the record of personals who have salary greater than 25000.

Ans. (i) UPDATE Personal SET Salary=Salary + Salary*0.5 WHERE Allowance IS NOT NULL;

(ii) SELECT Name, Salary + Allowance AS "Total Salary" FROM Personal;

(iii) DELETE FROM Personal WHERE Salary>25000;

Page 151 of 154


OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12

31 Consider the tables PRODUCT and BRAND given below: 4

Table: PRODUCT

Table: BRAND

Write SQL queries for the following: (i) ii) (iii) (iv)
(i) Display product name and brand name from the tables PRODUCT and BRAND.

(ii) Display the structure of the table PRODUCT.

(iii) Display the average rating of Medimix and Dove brands

(iv) Display the name, price, and rating of products in descending order of rating.

Ans. (i) SELECT PName, BName FROM PRODUCT P, BRAND B WHERE P.BID=B.BID;

(ii) DESC PRODUCT;

Page 152 of 154


OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
(iii) SELECT BName, AVG(Rating) FROM PRODUCT P, BRAND B WHERE P.BID=B.BID GROUP
BY BName HAVING BName='Medimix' OR BName='Dove';

(iv) SELECT PName, UPrice, Rating FROM PRODUCT ORDER BY Rating DESC;

35 (i) Define the term Domain with respect to RDBMS. Give one example to support your 1
answer.

Ans. Domain is a set of values from which an attribute can take value in each row.

For example, roll no field can have only integer values and so its domain is a set of
integer values.

(ii) Kabir wants to write a program in Python to insert the following record in the table 4
named Student in MYSQL database, SCHOOL:
• rno(Roll number )- integer
• name(Name) - string
• DOB (Date of birth) – Date
• Fee – float

Note the following to establish connectivity between Python and MySQL:


• Username - root
• Password - tiger
• Host - localhost

The values of fields rno, name, DOB and fee has to be accepted from the user. Help Kabir
to write the program in Python.

Ans.

OR
(i) Give one difference between alternate key and candidate key. 1

Page 153 of 154


OUR OWN HIGH SCHOOL, AL WARQA’A
Computer Science SQL and Databases
Grade 12
(ii) Sartaj has created a table named Student in MYSQL database, SCHOOL:
• rno(Roll number )- integer
• name(Name) - string
• DOB (Date of birth) – Date
• Fee – float

Note the following to establish connectivity between Python and MySQL:


• Username - root
• Password - tiger
• Host – localhost

Sartaj, now wants to display the records of students whose fee is more than 5000. Help
Sartaj to write the program in Python.

Ans. (i) All keys that have the properties to become a primary key are candidate keys. The
candidate keys that do not become primary keys are alternate keys.

(ii)

*****

Page 154 of 154

You might also like