Database Questions (MySQL+Python)
Database Questions (MySQL+Python)
208 | P a g e
8. What is key in SQL?
a. Which is used to uniquely identify record
b. To maintain data integrity
c. To enable efficient data retrieval
d. All of the above
9. Which key is used to uniquely identify record in table and doesn’t allow NULL values?
a. Alternate key
b. Primary key
c. Super key
d. Foreign key
10. A column that could potentially be used as the primary key and it should also contain
unique values?
a. Alternate key
b. Primary key
c. Candidate key
d. Surrogate key
11. A candidate key that is not selected as primary key which can serve as a unique identifier.
a. Alternate key
b. Primary key
c. Candidate key
d. Surrogate key
13. A column or set of column in table that refers to the primary key of another table.
a. Alternate key
b. Primary key
c. Candidate key
d. Foreign key
14. Which of the following SQL commands is used to view list of all database?
a. select databases
b. show databases
c. view databases
d. project databases
15. Which of the following SQL commands is used to use/select a particular database?
209 | P a g e
a. use
b. select
c. view
d. project
16. Which SQL command is used to define and maintain physical structure or schema of table
in database like creating, altering and deleting database object such as table and
constraints?
a. DDL
b. DML
c. DCL
d. TCL
17. Which SQL command used to change or modify any attribute/column like addition and
deletion of column in database?
a. create
b. alter
c. delete
d. update
21. Which commands is used to show all table in current using database?
a. display tables;
b. show tables;
c. view tables;
d. select all tables;
210 | P a g e
22. Which command is used in where clause to search NULL values in a particular column?
a. IS NULL
b. IN NULL
c. NOT NULL
d. IS NOT NULL
24. Which SQL function is used to determine the no. of row or non-null values?
a. min
b. max
c. count
d. sum
25. Which SQL function is used to count the entire number of row in database table?
a. count
b. count(*)
c. max
d. min
26. In relational database, a key that established a link between two tables?
a. Primary key
b. Alternate key
c. Foreign key
d. Candidate key
27. A table containing data organized in the form of row and column in relational data
model?
a. Relation
b. View
c. Tuple
d. Database
28. Which SQL clause is used to filter the result of SELECT statement in Relational Database?
a. from
b. where
c. join
d. having
211 | P a g e
a. To retrieve a set of statement
b. To add new row of data to a table
c. To change already existing data in table
d. To delete any existing row in table
30. How many candidate key can a relation/table have in relational database
a. One
b. Two
c. Multiple
d. None
31. What is the main difference between candidate key and primary key?
a. A candidate key can contain NULL value but primary key can’t contain NULL value
b. A primary key can contain unique value but it is not necessary for the candidate key to
have unique value
c. Candidate key is chosen by database system while primary key is chosen by the
designer
d. No difference
32. Which DDL command is used to modify the structure of an existing table?
a. create table
b. modify table
c. alter table
d. update table
33. Which DDL command is used to remove a table along with its all content from a database?
a. DELETE TABLE
b. DROP TABLE
c. REMOVE TABLE
d. ERASE TABLE
34. Which constraint enforce data integrity by ensuring that a column always contain a
values?
a. NULL
b. NOT NULL
c. CHECK
d. DEFAULT
35. What is primary difference between candidate key and primary key in database?
a. Primary key is chosen by the end user/designer while candidate key is generated by
database system
b. A candidate key uniquely identifies each row in a table, while primary key is not unique
c. A primary key can have NULL values while a candidate key can’t
212 | P a g e
d. A candidate key can become a primary key, but a primary key can’t become a candidate
key
36. Which key is used to enforce referential integrity between tables in database system
a. Primary key
b. Candidate key
c. Foreign key
d. Alternate key
37. What is the main difference between CHAR and VARCHAR datatype in SQL?
a. CHAR is case-sensitive while VARCHAR is non case sensitive
b. CHAR store variable length strings while VARCHAR stores fixed length string
c. CHAR store fixed length strings while VARCHAR stores variable length string
d. CHAR is used storing numeric data while VARCHAR is used for text data
38. What is the primary purpose of unique key constraints in a relational databases?
a. To ensure that values in a column are NULL
b. To ensure that values in a column are unique
c. To define relationship between tables
d. To specify a condition that must be met for data to be valid in a column
39. Which constraint key is used when you want to enforce uniqueness but allows NULL
values in database
a. PRIMARY KEY
b. UNIQUE KEY
c. NOT NULL
d. CHECK
41. Which keyword is used for table aliasing that involves giving a table short and alternative
name to simplify query syntax?
a. from
b. as
c. where
d. on
42. Which SQL clause is used in database table to eliminate duplicate rows from the query
result?
a. group by
213 | P a g e
b. distinct
c. describe
d. duplicate
43. Which of the following clauses in SQL is most appropriate to use to select matching tuples
in a specific range of values?
a. IN
b. LIKE
c. BETWEEN
d. IS
44. Which of the following SQL datatype allows NULL values by default?
a. INT
b. CHAR
c. VARCHAR
d. FLOAT
45. Which of the following is NOT a required argument to the connect() function?
a. Hostname
b. Username
c. Password
d. Database name
47. Which method is used to fetch n number of results from a SQL query?
a. fetchall()
b. fetchone()
c. fetchmany()
d. All of the above
48. _______ it is a pointer or iterator which points towards the resultset of the SQL query.
a. cursor
b. rset
c. temp
d. None of these
49. Which of the following is not a valid method to fetch records from database in python.
a. fetchmany()
b. fetchone()
214 | P a g e
c. fetchmulti()
d. fetchall()
50. To get all the records from result set, you may use ___________.
a. cursor.fetchmany()
b. cursor.fetchall()
c. cursor.fetchone()
d. cursor.execute()
215 | P a g e
Assertion-and-Reason Type
In the following questions, Assertion (A) and Reason (R).
Choose the correct choice as:
(a) Both Assertion (A) and Reason (R) are the true and Reason (R) is a correct explanation of
Assertion (A).
(b) Both Assertion (A) and Reason (R) are the true but Reason (R) is not a correct
explanation of Assertion (A).
(c) Assertion (A) is true and Reason (R) is false.
(d) Assertion (A) is false and Reason (R) is true.
2. Assertion (A): HAVING clause can only be used with GROUP BY statement.
Reason (R): WHERE clause can be used in place of HAVING clause in GROUP BY
statement.
3. Assertion (A): LIKE operator is used for pattern matching in WHERE clause.
Reason (R): % and _ wildcard is used in LIKE operator for making a pattern.
8. Assertion (A): The HAVING clause is used to filter aggregated data in SQL queries.
Reason (R): The HAVING clause is used to group rows with similar values in one or
more column into result sets.
9. Assertion (A): Inner Join retrieves rows that have matching values in both tables being
joined.
Reason (R): Inner join excludes row with no matching values
10. Assertion (A): Between operator is used to filter data within a specified range.
Reason (R): Where clause works exactly same as between operator
216 | P a g e
Long Answer Type Questions:
1. Write MySQL command to create the table ‘Employee’ with the following structure and
constraint. Table: Employee
2. Write MySQL command to create the table ‘Student’ and ‘Activities’ with the following
structure and constraint.
Table: Student
Column_Name DataType(size) Constraint
Student_ID varchar(20) Primary key
Student_Name char(80) Not Null
Gender char(20) Not Null
Class varchar(30)
Age int(20) Not Null
Address Varchar(150) Unique
Phone Int(15) Not Null, unique
Table: Activities
Column_Name DataType(size) Constraint
Student_ID varchar(20) Foreign key references to
Student_ID of Employee
table
Activity_Name char(80) Not Null
Position char(30) Not Null
3. Anmol maintain 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:
217 | P a g e
Table: PHARMA
(e) Now Anmol wants to increase the PRICE of all medicines following commands will he use
for the purpose?
I. UPDATE SET
II. INCREASE BY
III. ALTER TABLE
IV. INSERT INTO
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
218 | P a g e
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])
5. Write SQL statements for the following queries (i) to (v) based on the relations
CUSTOMER and TRANSACTION given below:
Table: CUSTOMER
Table: TRANSACTION
1. To display all information about the CUSTOMERS whose NAME starts with 'A'.
2. To display the NAME and BALANCE of Female CUSTOMERS (with GENDER as 'F') whose
TRANSACTION Date (TDATE) is in the year 2019.
3. To display the total number of CUSTOMERS for each GENDER.
4. To display the CUSTOMER NAME and BALANCE in ascending order of GENDER.
5. To display CUSTOMER NAME and their respective INTEREST for all CUSTOMERS where
INTEREST is calculated as 8% of BALANCE.
6. The IT Company XYZ has asked their IT manager Ms. Preeti to maintain the data of all the
employees in two tables EMPLOYEE and DEPT. Ms. Preeti has created two tables
EMPLOYEE and DEPT. She entered 6 rows in EMPLOYEE table and 5 rows in DEPT table.
Table: DEPT
D_CODE D_NAME CITY
D001 INFRASTRUCTURE DELHI
D002 MARKETING DELHI
D003 MEDIA MUMBAI
D005 FINANCE KOLKATA
D004 HUMAN RESOURCE MUMBAI
219 | P a g e
Table: EMPLOYEE
E_NO NAME DOJ DOB GENDER D_CODE Salary
1001 Vinay 2013-09-02 1991-09-01 MALE D001 250000
1002 Ruby 2012-12-11 1990-12-15 FEMALE D003 270000
1003 Anuj 2013-02-03 1987-09-04 MALE D005 240000
1007 Sunny 2014-01-17 1988-10-19 MALE D004 250000
1004 Rohit 2012-12-09 1986-11-14 MALE D001 270000
1005 Preeti 2013-11-18 1989-03-31 FEMALE D002 NULL
Note: DOJ refers to date of joining and DOB refers to date of Birth of employees.
Based on the above data, answer the following questions:
1. Identify the column which can be consider as primary key in EMPLOYEE table.
2. Identify the column which can be consider as primary key in DEPT table
3. What is the degree and cardinality of EMPLOYEE table?
4. What is the degree and cardinality of DEPT table?
5. Write SQL queries for the following:
1) Insert two new row in Employee table with following data:
1006 Rahul 2019-11-06 1992-01-04 MALE D003 156000
1008 Sonam 2022-01-06 1991-04-06 FEMALE D005 167000
2) To display E_NO, NAME, GENDER from the table EMPLOYEE in descending order of
E_NO.
3) To display the NAME of all the ‘FEMALE’ employees from the table EMPLOYEE.
4) To display the E_NO and NAME of those employees from the table EMPLOYEE who
are born between ‘1987-01-01’ and ‘1991-12-01’.
5) To display NAME and CITY of those employees whose DEPARTMENT is either ‘MEDIA’
or ‘FINANCE’.
6) To display the NAME of those employees whose name starts with letter ‘R’.
7) To display NAME of those employees whose name contains letter ‘n’.
8) To display NAME of those employees whose name has exact 5 letters.
9) To display D_NAME and CITY from table DEPT where D_NAME ends with letter ‘G’
and CITY is ‘DELHI’.
10) To display the maximum SALARY of EMPLOYEE table.
11) To delete data of all those employees whose age is less than 25.
12) To update SALARY to 230000 of those employee whose E_NO is 1004.
13) To change the sequence of DOB column in employee table and move it before DOJ
column.
14) To add a new column MOBILE int(20) before column SALARY in employee table.
15) To set SALARY to 300000 of all those employees whose age is NULL.
16) To Increase the salary of all employees by 30000 in EMPLOYEE table.
17) To display the average SALARY of EMPLOYEE table.
18) To display name of employees who have SALARY more than 200000 in ascending
order of NAME.
19) To display department wise average salary of employees.
20) To display total number of departments in XYZ company.
21) To delete data of all the employees whose D_CODE is not ‘D001’.
22) To display E_NO, NAME and SALARY of all those employees who don’t live in ‘DELHI’.
220 | P a g e
23) To change column name CITY to D_CITY in DEPT table.
24) To delete EMPLOYEE table.
25) To delete D_NAME column from DEPT table.
7. A garment store is considering to maintain their inventory using SQL to store the data. as
a database administrator, Mr.Rohit has decided that:
Name of the database – STORE
Name of the table – GARMENT
The attributes of GARMENT table are as follows:
GCODE – numeric
DESCRIPTION – character of size 50
PRICE – numeric
FCODE – varchar of size 10
Table: GARMENT
GCODE DESCRIPTION PRICE FCODE
10023 JEANS 1150 F01
10001 SHIRT 750 F02
10044 SHORTS 600 F05
10005 TIE 400 F04
10002 JACKET 5000 F01
10022 SOCKS 150 NULL
221 | P a g e
(viii) SELECT SUM(PRICE) FROM GARMENT;
(ix) SELECT * FROM GARMENT WHERE DESCRIPTION LIKE ‘%T%’ AND
FCODE!=’F02’;
(x) SELECT * FROM GARMENT ORDER BY PRICE DESC;
(xi) SELECT PRICE*10 FROM GARMENT;
(xii) SELECT COUNT(DISTINCT FCODE) FROM GARMENT;
(xiii) SELECT * FROM GARMENT WHERE FCODE NOT IN (‘F01’,’F02’) AND
PRICE<500;
(xiv) SELECT GCODE, PRICE FROM GARMENT WHERE FCODE IS NULL;
(xv) SELECT * FROM GARMENT WHERE PRICE >500 AND PRICE <1000;
8. Write the output of the following SQL queries based on table TRANSACTION given below:
Table: TRANSACTION
T_NO M_NO AMOUNT CARD_TYPE DATE STATUS
1 11 5000 CREDIT 2019-10-11 SUCCESS
2 11 170 CREDIT 2019-10-14 FAILURE
3 13 800 DEBIT 201-10-24 FAILURE
4 12 90 CREDIT 2019-11-10 SUCCESS
5 13 1400 DEBIT 2019-11-11 SUCCESS
6 11 500 DEBIT 2019-11-18 SUCCESS
7 13 1600 DEBIT 2019-11-27 FAILURE
Table: COMPANY
T_NO QTY_ISSUED COMPANY
1 15 SBI
3 50 ICICI
4 34 HDFC
222 | P a g e
9. The code given below reads the records from the table employee and displays only those
records of employee table who don’t lives in city 'Delhi':
E_ID – varchar(50)
E_Name – char(50)
Salary – int(15)
City – char(20)
Note the following to establish connectivity between Python and MySQL:
Username is root
Password is 12345
The table exists in a MySQL database named company.
The table has four attributes (E_ID, E_Name, Salary, City).
10. The code given below deletes the records from the table employee which contains
following record structure:
E_ID – varchar(50)
E_Name – char(50)
Salary – int(15)
City – char(20)
Note the following to establish connectivity between Python and MySQL:
Username is root
Password is 12345
The table exists in a MySQL database named company.
The table has four attributes (E_ID, E_Name, Salary, City).
11. Write a python program to delete all the tuples from Student table whose age>14.
Note the following to establish connectivity between Python and MySQL:
Username is root
Password is 12345
The table exists in a MySQL database named school.
The table has five attributes (Stu_ID, Stu_Name, age, Address)
12. Write a python program to insert 5 records in Employee table. Take these 5 records as an
input from the user (One record at a time).
Note the following to establish connectivity between Python and MySQL:
Username is root
Password is 12345
The table exists in a MySQL database named company.
The table has five attributes (Emp_ID, Emp_Name, DOJ, Gender, Salary)
13. Write a python program to update name of employee in Employee table whose employee
id is ‘E1001’ (Take updates name as an input from the user).
Note the following to establish connectivity between Python and MySQL:
Username is root
Password is 12345
The table exists in a MySQL database named company.
The table has five attributes (Emp_ID, Emp_Name, DOJ, Gender, Salary)
224 | P a g e