UNIT TEST-7
Chapter: Simple Queries, Joins and Interface Python with MySql
Q.No Question Marks
1. Which of the following command is used to connect Python with MySQL? 1
a) mysql.connect( ) b) MySQL.connector.connect( )
c) mysql.connector.connect( ) d) connect.mysql( )
2. Which module needs to be imported to use MySQL in Python? 1
a) mysql b) sqlite3 c) MySQL d) mysql.connector
3. What does the cursor( ) method do in MySQL Python connector? 1
a) Executes the SQL commands directly
b) Establishes a connection to the database
c) Creates a cursor object to interact with the database
d) Creates a database
4. Which method is used to execute SQL queries in Python? 1
a) executeQuery( ) b) run( ) c) execute( ) d) query( )
5. Which method is used to retrieve all rows from the executed query? 1
a) fetchone( ) b) fetchall( ) c) getrows( ) d) read( )
6. After performing an INSERT operation, which method is used to save the changes in 1
the database?
a) commit( ) b) save( ) c) flush( ) d) update( )
7. Which of the following is not a valid parameter in mysql.connector.connect( )? 1
a) host b) username c) user d) password
8. What is the purpose of the close ( ) method? 1
a) To close the database file b) To shut down the MySQL server
c) To close the cursor or connection d) To delete the database
9. Which of the following is correct for selecting a database after connection? 1
a) cursor.select("mydb") b) connection.database("mydb")
c) cursor.execute("USE mydb") d) connect.use("mydb")
10. Name the function is used to check the successful connection? 1
11. ___________ method is used to retrieve the original data that was changed through the 1
commit () method.
a) execute() b)rollback() c) update() d) close()
12. Exception raised when the relational integrity of the database is affected in Python. 1
a) IntegrityFailError b) IntgrityFailure c) IntegrityViolationerror d)IntigrityError
13. Relation R1 has 10 tuples and 5 attributes. Relation R2 has 0 tuples and 7 attributes. 1
When a CROSS JOIN is achieved between R1 and R2, how many tuples would the
resultant set have?
a) 28 b) 10 c) 0 d) 35
14. In which type of join, no duplicate columns are there? 1
a) Equi-join b) Natural c) Left Join d) Right Join
15. State True or False: A join condition can only use equality operator for comparison. 1
16. Which keyword retains duplicate output rows in a query result? 1
a) all b) distinct c) asc d) desc
17. Which of the following queries contains an error? 1
a) Select * from emp where empid = 10003;
b) Select empid from emp where empid = 10006;
c) Select empid from emp;
d) Select empid where empid = 1009 and lastname = 'GUPTA';
18. The pattern '_ _ _' matches any string of ............... three characters. '_ _ _%' matches 1
any string of ............... three characters.
a) Atleast, Exactly b) Exactly, Atleast c) Atleast, All d) All, Exactly
19. In SQL, which type of Join(s) may contain duplicate column(s)? 1
Q20 and Q21 are Assertion (A) and Reason(R) based questions. Mark the correct choice as:
a) Both A and R are True and R is the correct explanation for A.
b) Both A and R are True and R is not the correct explanation for A.
c) A is True but R is False.
d) A is False but R is True
20. Assertion (A): cursor.fetchall( ) is used to delete all records from a table. 1
Reason (R): fetchall( ) retrieves all records returned by a SELECT query.
21. Assertion (A): The cursor( ) method is used to execute SQL queries in Python. 1
Reason (R): The cursor object allows interaction with the database.
Section-B (2 Marks)
22. What is a resultset? 2
23. What is meant by cursor? 2
24. What is the difference between equi join and natural join? 2
25. What is meant by Cartesian product? Give example 2
26. i na 2
d me
1 abc
2 pqr
3 xyz
Predict the output of the following code : Consider the Table -category
import mysql.connector
db = mysql.connector.connect(....)
cursor = db.cursor()
sql1 = "update category set name = '%s' WHERE ID = %s" % ('CSS',2)
cursor.execute(sql1)
db.commit()
print("Rows affected:", cursor.rowcount)
db.close()
27. Consider the table SHOPEE given below. Write the SQL Queries: 2
TABLE SHOPEE
(i) To display names of the product, whose name starts with ‘C’ in ascending order of
Price.
(ii) To display code, product name and City of the products whose quantity is less than
100.
Section-C (3 Marks)
28. 3
Write SQL queries for the following :
(i) To count how many addresses are not having NULL values in the address column of
STUDENTS table.
(ii) To display Name, Class from STUDENTS table and the corresponding Grade from
SPORTS table.
(iii) To display Name of the student and their corresponding Coachnames from
STUDENTS and SPORTS tables.
29. 3
Write the SQL Queries
(i) To display the name of the Teacher and their Designation.
(ii) To display the name and subject of all female teachers.
(iii) To display the details of HOD.
30. 3
Mr.Harsh want to interface python with mysql and write some code help him to write
the code
import_____________.connector as m
mydb=m.________(host=”localhost”,user=”root”, passwd=”tiger”,database=” choo ”)
cursor=mydb.___________( )
cursor._______________(“select * from student”)
data=cursor.__________( )
count=cursor.__________
print(data)
print(“No of records retrived”,count)
mybd.close()
Section-D (4 Marks),
31. Sartaj has created a table named CLASS in MYSQL database, 4
CLASS:
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, DB – SCHOOL
Sartaj, now wants to display the records of students whose fee is more than
5000. Help Sartaj to write the program in Python.
32. Rehaan wants to write a program in Python to insert the following record in the table 4
named EMP in MYSQL database COMPANY:
a. eno(Empno)- integer
b. ename(Name) - string
c. DOB (Date of birth) – Date
d. Salary – float
Note the following to establish connectivity between Python and MySQL:
Username – root, Password – password, Host - localhost
The values of fields eno, name, DOB and salary has to be accepted from the user. Help
Rehaan to write the program in Python.