0% found this document useful (0 votes)
37 views13 pages

DBMS - Worksheet 1 - 3 Marks

This document contains a worksheet for Grade 12 Computer Science focusing on Database Management Systems (DBMS). It includes SQL query exercises, Python code snippets for database connectivity, and tasks related to various database operations such as selecting, updating, and joining tables. The worksheet is structured in sections with specific questions and coding tasks aimed at assessing students' understanding of DBMS concepts.

Uploaded by

bessiecarlm
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)
37 views13 pages

DBMS - Worksheet 1 - 3 Marks

This document contains a worksheet for Grade 12 Computer Science focusing on Database Management Systems (DBMS). It includes SQL query exercises, Python code snippets for database connectivity, and tasks related to various database operations such as selecting, updating, and joining tables. The worksheet is structured in sections with specific questions and coding tasks aimed at assessing students' understanding of DBMS concepts.

Uploaded by

bessiecarlm
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/ 13

Grade: 12 Computer Science

DBMS
Worksheet - 1
3 Marks
1. Write the output of the queries (i) to (vi) based on the table given below:

(i) Select BRAND_NAME, FLAVOUR from CHIPS where PRICE <> 10;
(ii) Select * from CHIPS where FLAVOUR=”TOMATO” and PRICE > 20;
(iii) Select BRAND_NAME from CHIPS where price > 15 and QUANTITY < 15;
(iv) Select count( distinct (BRAND_NAME)) from CHIPS;
(v) Select price , price *1.5 from CHIPS where FLAVOUR = “PUDINA”;
(vi) Select distinct (BRAND_NAME) from CHIPS order by BRAND_NAME desc;
2.(A) Consider the following tables BOOKS and ISSUED in a database named “LIBRARY”.
WriteSQL commands for the statements (i) to (iv).

(i) Display book name and author name and price of computer type books.
(ii) To increase the price of all history books by Rs 50.
(iii) Show the details of all books in ascending order of their prices.
(iv) To display book id, book name and quantity issued for all books which have been
issued.
(B)Write the command to view all tables in a database.

DBMS Worksheet - 1 - 3 Marks Page 1


3. The code given below inserts the following record in the table Student:

Note the following to establish connectivity between Python and MySQL:


* Username is root
* Password is toor@123
* The table exists in a “stud” database.
* The details (RollNo, Name, Clas and Marks) are to be accepted from the user.
Write the following missing statements to complete the code:
Statement 1 – to form the cursor object

4. Write the outputs of the SQL queries (i) to (iii) based on the relations Teacher and
Posting given below:

1. SELECT count(DISTINCT Address) FROM Consumer;

DBMS Worksheet - 1 - 3 Marks Page 2


2.SELECT Company, MAX(Price), MIN(Price), COUNT(*) from Stationary GROUP BY
Company;
SELECT Consumer.ConsumerName, Stationary.StationaryName, Stationary.Price FROM
Stationary, Consumer WHERE Consumer.S_ID = Stationary.S_ID;
5. Write a output for SQL queries (i) to (iii), which are based on the table: SCHOOL and
ADMIN given below:

1. SELECT SUM (PERIODS), SUBJECT FROM SCHOOL GROUP BY SUBJECT;


2. SELECT TEACHERNAME, GENDER FROM SCHOOL, ADMIN WHERE DESIGNATION
= ‘COORDINATOR’ AND SCHOOL.CODE=ADMIN.CODE;
3. SELECT COUNT (DISTINCT SUBJECT) FROM SCHOOL;
6. The code given below reads the following record from the table named studentand
displays only those records who have marks greater than 75:
RollNo – integer
Name – string
Class – integer
Marks – integer
Note the following to establish connectivity between Python and MYSQL:
• Username is root
• Password is tiger
• The table exists in a MYSQL database named school.
Write the following missing statements to complete the code:
Statement 1 – to form the cursor object
Statement 2 – to execute the query that extracts records of those students whose marks
are greater than 75.
Statement 3- to read the complete result of the query (records whose 2+3 marks are
greater than 75) into the object named data, from the table student in the database.
import mysql.connector as mysql
def sql_data():

DBMS Worksheet - 1 - 3 Marks Page 3


con1=mysql.connect(host="localhost",user="root", password="tiger",
database="school")
mycursor=_______________ #Statement 1
print("Students with marks greater than 75 are : ")
_________________________ #Statement 2
data=__________________ #Statement 3
for i in data:
print(i)
print()
7. (a) Consider the following tables – Applicants and Centre

What will be the output of the following statement?


SELECT * FROM Applicants NATURAL JOIN Centre;
(b) Write the output of the queries (i) to (iv) based on the table, Car given below:

(i) SELECT DISTINCT MAKE FROM CAR;


(ii) SELECT MAKE, COUNT(*) FROM CAR GROUP BY MAKE;
(iii) SELECT CNAME FROM CAR WHERE CAPACITY>5 ORDER BY CNAME;
(iv) SELECT CNAME, MAKE FROM CAR WHERE CHARGES>2500
8. (a) Consider the following tables EMPLOYEE and SALARY

DBMS Worksheet - 1 - 3 Marks Page 4


Give the output of the following SQL queries:
(i) SELECT COUNT(SGRADE), SGRADE FROM EMPLOYEE GROUP BY SGRADE;
(ii) SELECT MIN(DOB), MAX(DOJ) FROM EMPLOYEE;
(iii) SELECT NAME, SALARY FROM EMPLOYEE E, SAL S WHERE
E.SGRADE=S.SGRADE AND E.ECODE<103;
(iv) SELECT SGRADE, SALARY+HRA FROM SAL WHERE SGRADE= ‘S02’;
(b) Write the command to view structure of table FOOD in a database.
9. The code given below inserts the following record in the table Student:
Rollno – integer
Name – string
Age – integer
Note the following to establish connectivity between Python and MYSQL:
• Username is root
• Password is sys
• The table exists in a MYSQL database named school.
• The details (Rollno, Name and Age) are to be accepted from the user.
Write the following missing statements to complete the code:
Statement 1 – to form the cursor object
Statement 2 – to execute the command that inserts the record in the table Student.
Statement 3- to add the record permanently in the database
import mysql.connector
mydb= mysql.connector.connect(host="localhost",user="root",passwd="sys", database =
“myschool”)
mycursor = ______________ #statement 1
while True:
ch=int(input("enter -1 to exit , any other number to insert record"))
if ch==-1:
break
rollno = int(input("enter Roll no:"))
name = input("enter Name:")
age = int(input("enter Age:"))
qry = "insert into student values ({},'{}',{})".format(rollno,name,age)
_________________________ #statement 2
_________________________ #statement 3

DBMS Worksheet - 1 - 3 Marks Page 5


print(“Data added successfully”)
10. The code given below adds a new column in the table Student, updates the data into
it and displays the content of the table. Student table details are as follows:
Rollno – integer
Name – string
Age – integer
Note the following to establish connectivity between Python and MYSQL:
• Username is root
• Password is sys
• The table exists in a MYSQL database named school.
Write the following missing statements to complete the code:
Statement 1 – to form the cursor object
Statement 2 – to execute a query that adds a column named “MARKS” of type integer in
the table Student.
Statement 3- to update the record permanently in the database
import mysql.connector
mydb= mysql.connector.connect(host="localhost",user="root",passwd="sys",
database="myschool")
mycursor = _____________________ #statement1
________________________________ #statement2
mycursor.execute("update student set MARKS=9 where Rollno = 1")
______________________________ #statement3
mycursor.execute("select * from student")
for x in mycursor:
print(x)
11. a. Consider the following tables – Sales and Item:

What will be the output of the following statement?


SELECT SNAME,SCITY,IPRICE FROM sales, Item where SCITY=”Delhi” and Sales.SCode
=Item.SCode;

DBMS Worksheet - 1 - 3 Marks Page 6


b. Write the output of the queries (i) to (iv) based on the table, TABLE: EMPLOYEE

12. a. 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)
Table: Activity

(i) To display the name of all activities with their Acodes in descending order.
(ii) To display sum of PrizeMoney for each of the number of participants groupings (as
shown in column ParticipantsNum 10,12,16)
(iii) To display the coach’s name and ACodes in ascending order of ACode from the table
COACH.
(iv) To display the content of the Activity table whose ScheduleDate is earlier than
01/01/2004 in ascending order of ParticipantsNum.

DBMS Worksheet - 1 - 3 Marks Page 7


(b) Write the command to view all tables in a database.
13. (a)Consider the following tables – Bank_Account and Branch:
Bank_Account: Branch:

What will be the output of the following statement?


SELECT * FROM Bank_Account NATURAL JOIN Branch;
(b) Give the output of the following sql statements as per table given above.
Table : SPORTS

i. SELECT COUNT(*) FROM SPORTS.


ii. SELECT DISTINCT Class FROM SPORTS.
iii. SELECT MAX(Class) FROM SPORTS;
iv. SELECT COUNT(*) FROM SPORTS GROUP BY Game1;
14. (a) consider the following tables School and Admin and answer the following
questions:

TABLE: SCHOOL TABLE : ADMIN

Give the output of the following SQL queries:


i. Select Designation, count(*) from Admin Group by Designation having
count(*)<2;
ii. Select max(experience) from school;
iii. Select teacher from school where experience >12 order by teacher;

DBMS Worksheet - 1 - 3 Marks Page 8


iv. Select count(*), gender from admin group by gender;
(b) Write SQL command to delete a table from database.
15. (a) Consider the following table:
Table: Employee

What is the degree and cardinality of table Employee, if it contains only the given data?
Which field fields is/are the most suitable to be the Primary key if the data shown above
is only the partial data?
(b) Write the output of the queries (i) to (iv) based on the table Employee:
(i) select name, project from employee order by project;
(ii) select name, salary from employee where doj like'2015%';
(iii)select name, salary from employee where salary between 100000 and 200000;
(iv) select min(doj), max(dob) from employee;
16. (a) Write the outputs of the SQL queries (i) to (iv) based on the relations Projects and
Employee given below:

(i) select project, count(*) from employee group by project;


(ii) selectpid, pname, eid, name from projects p,employee e where p.pid=e.project;
(iii) select min(startdate), max(startdate) from projects;
(iv) selectavg(salary) from employee where doj between '2018-08-19' and '2018-08-31';
(b) Write the command to make Projects column of employee table a foreign key which
refers to PID column of Projects table.
17. (a)Write SQL query to add a column total price with datatype numeric and size 10, 2
in a table product.

DBMS Worksheet - 1 - 3 Marks Page 9


(b) Consider the following tables SCHOOL and ADMIN. Give the output the following SQL
queries:
Table: School Table: Admin

i.SELECT Designation COUNT (*) FROM Admin GROUP BY Designation HAVING COUNT
(*) <2:
ii.SELECT max (EXPERIENCE) FROM SCHOOL;
iii. .SELECT TEACHER FROM SCHOOL WHERE EXPERIENCE >12 ORDER BY
TEACHER;
iv.SELECT COUNT (*), GENDER FROM ADMIN GROUP BY GENDER;
18. (a)Sonal needs to display name of teachers, who have “0” as the third character in
their name. She wrote the following query.
SELECT NAME FROM TEACHER WHERE NAME = “$$0?”;
But the query is’nt producing the result. Identify the problem.
(b)Write output for (i) & (iv) based on table COMPANY and CUSTOMER

i. SELECT COUNT(*) , CITY FROM COMPANY GROUP BY CITY;


ii. SELECT MIN(PRICE), MAX(PRICE) FROM CUSTOMER WHERE QTY>10;
iii. SELECT AVG(QTY) FROM CUSTOMER WHERE NAME LIKE “%r%;
iv. SELECT PRODUCTNAME, CITY, PRICE FROM COMPANY, CUSTOMER WHERE
COMPANY.CID=CUSTOMER.CID AND PRODUCTNAME=”MOBILE”;
19. (a) Differentiate between Natural join and Equi join.
Table: Employee Table: Job

DBMS Worksheet - 1 - 3 Marks Page 10


Give the output of following SQL statement:
(i) Select Name, JobTitle, Sales from Employee, Job where
Employee.JobId=Job.JobId and JobId in (101,102);
(ii) (ii) Select JobId, count(*) from Employee group by JobId;
20. (a) Consider the following table GAMES. Give outputs for SQL queries (i) to (iv).
Table: GAMES

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


(ii) SELECT MAX(ScheduleDate),MIN(ScheduleDate) FROM GAMES;
(iii) SELECT SUM(PrizeMoney) FROM GAMES;
(iv) SELECT * FROM GAMES WHERE PrizeMoney> 12000;
21. (a) Consider the following tables – EMPLOYEES AND DEPARTMENT

What will be the output of the following statement?


SELECT ENAME, DNAME FROM EMPLOYEES, DEPARTMENT WHERE
EMPLOYEE.DNO=DEPARTMENT.DNO;
(b) Write the output of the queries (i) to (iv) based on the tables given below:

i) SELECT ITEM_NAME, MAX(PRICE), COUNT(*) FROM ITEM GROUP BY ITEM_NAME;

DBMS Worksheet - 1 - 3 Marks Page 11


ii) SELECT CNAME, MANUFACTURER FROM ITEM, CUSTOMER WHERE
ITEM.ID=CUSTOMER.ID;
iii) SELECT ITEM_NAME, PRICE*100 FROM ITEM WHERE MANUFACTURER="ABC";
(iv) SELECT DISTINCT CITY FROM CUSTOMER;

22. (a) Write the outputs of the SQL queries (i) to (iv) based on the relations SCHOOL
and ADMIN given below:
Table: School Table: Admin

(i) SELECT SUM (PERIODS), SUBJECT FROM SCHOOL GROUP BY SUBJECT;


(ii) SELECT TEACHERNAME, GENDER FROM SCHOOL, ADMIN WHERE DESIGNATION
= ‘COORDINATOR’ AND SCHOOL.CODE=ADMIN.CODE;
(iii) SELECT COUNT (DISTINCT SUBJECT) FROM SCHOOL;
(iv) SELECT MAX(PERIODS) FROM SCHOOL;
(b) Write the command to view the structure of a table SCHOOL
23. Write the outputs of the SQL queries (a) to (c) based on the relation Furniture

(a) SELECT Itemname FROM Furniture WHERE Type="Double Bed";


(b) SELECT MONTHNAME(Dateofstock) FROM Furniture WHERE Type="Sofa";
(c) SELECT Price*Discount FROM Furniture WHERE Dateofstock>31/12/02;

DBMS Worksheet - 1 - 3 Marks Page 12


24. Consider the following table GAMES

Write the output for the following queries :


(i) SELECT COUNT(DISTINCT Number) FROM GAMES;
(ii) SELECT MAX(ScheduleDate),MIN(ScheduleDate) FROM GAMES;
(iii) SELECT SUM(PrizeMoney) FROM GAMES;
25. Consider the table

The Following program code is used to increase the salary of Trainer SUNAINA by 2000.
Note the following to establish connectivity between Python and MYSQL:
 Username is root
 Password is system
 The table exists in a MYSQL database named Admin.
Write the following missing statements to complete the code:
Statement 1 – to form the cursor object
Statement 2 – to execute the command that inserts the record in the table Student.
Statement 3- to add the record permanently in the database
import mysql.connector as mydb
mycon = mydb.connect (host = “localhost”, user = “root”, passwd = “system”, database =
“Admin”)
cursor = _______________ #Statement 1
sql = “UPDATE TRAINER SET SALARY = SALARY + 2000 WHERE TNAME = ‘SUNAINA’”
cursor. _______________ #Statement 2
_______________ #Statement 3
mycon.close( )

DBMS Worksheet - 1 - 3 Marks Page 13

You might also like