NEW
Font size
WorksheetsPython SQL Interview Questions Quiz
Total questions: 70
Worksheet time: 56mins
What is SQL and how is it used with Python?
A tool for machine learning
A library for data visualization
A standard language for managing relational databases
A programming language for web development
How can you connect to a SQL database in Python?
Using the database.connect() method
Using the connect() method from the requests library
Using the connect() method from the pandas library
Using the sqlite3 module
What are the different types of SQL joins?
INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN
SELECT, INSERT, UPDATE, DELETE
UNION, INTERSECT, EXCEPT
GROUP BY, ORDER BY, HAVING
How can you execute an SQL query using Python’s sqlite3 module?
Using the execute_query() method
Using the execute() method of a cursor object
Using the run_query() method
Using the query() method of the connection object
What is SQLAlchemy?
An SQL toolkit and ORM library for Python
A web framework for Python
A database management system
A data visualization library
How do you handle SQL injection in Python?
By using the exec() function
By sanitizing user input
By using parameterized queries
By using raw SQL queries
What is a database cursor?
A database management tool
A method for fetching data
A type of SQL command
An object used to interact with the database
How can you perform bulk inserts in SQL using Python?
Using the executemany() method
Using the bulk_insert() method
Using the insert_all() method
Using the insert_many() method
What is a SQL view?
A method for data backup
A virtual table based on a SQL query
A type of SQL command
A physical table in the database
How do you use the GROUP BY clause in SQL?
To join multiple tables
To group rows with the same values
To filter results based on conditions
To sort the result set
What is an ORM?
Online Resource Management
Object-Relational Mapping
Operational Resource Management
Object-Relational Model
How do you handle errors when working with SQL in Python?
Using try-except blocks
By ignoring them
By using the assert statement
By logging them only
What is database normalization?
The process of backing up a database
The process of organizing a database to reduce redundancy
The process of encrypting data
The process of indexing a database
How can you use SQL with Pandas in Python?
Using the pandas_sql() function
Using the read_sql() and to_sql() functions
Using the sql_query() function
Using the execute_sql() function
What is the purpose of an index in a database?
To enforce data integrity
To create backups of the database
To speed up data retrieval operations
To store data in a compressed format
What is a stored procedure in SQL?
A command to create a new database
A type of database backup
A method for data encryption
A precompiled collection of SQL statements
What is the difference between UNION and UNION ALL in SQL?
There is no difference between them
UNION is used for joining tables, UNION ALL is for filtering
UNION removes duplicate records, UNION ALL includes them
UNION is faster than UNION ALL
What is the purpose of a foreign key in a database?
To store large amounts of data
To create indexes for faster queries
To establish a relationship between two tables
To uniquely identify a record in a table
What is the difference between a primary key and a unique key in SQL?
Both can accept null values
Primary key can have duplicates, unique key cannot
Primary key uniquely identifies a record, unique key enforces uniqueness
There is no difference between them
What is the purpose of the HAVING clause in SQL?
To filter records before grouping
To filter records after grouping
To join multiple tables
To sort the result set
What is the difference between global and local scope?
Global variables are only available in the main body of the code.
Global variables can be accessed from any scope.
Local variables are available from within any scope.
Local variables can be accessed from any function.
What is an iterator in Python?
A method for sorting data.
A type of variable.
A function that returns a list.
An object that can be iterated upon.
What is the __init__() function in Python?
A function that initializes a variable.
A function that is executed when a class is initiated.
A function that creates a new object.
A function that defines a class.
When should you use lambda functions in Python?
When an anonymous function is needed for a short time.
When defining a class.
When creating a module.
When a function is required for a long period.
What is the main difference between lists, tuples, and sets?
Lists are ordered and changeable, tuples are ordered and unchangeable, sets are unordered and unchangeable.
Lists are unordered, tuples are ordered, and sets are mutable.
Sets are ordered, lists are unchangeable, and tuples are mutable.
Tuples are mutable, sets are ordered, and lists are unchangeable.
How can you check if all characters in a string are alphanumeric?
Use the isspace() method.
Use the isdigit() method.
Use the isalnum() method.
Use the isalpha() method.
How can you convert a string to an integer?
Use the convert() function.
Use the float() function.
Use the str() function.
Use the int() function.
What is indentation in Python, and why is it important?
It is optional in Python.
It is for readability only.
It is used for comments.
It indicates a block of code.
What is the correct syntax to output the type of a variable in Python?
print(type(variable))
print(variable.type())
type.print(variable)
variable.type()
Which collection does not allow duplicate members?
List
Tuple
Set
Dictionary
What is Inheritance in Python?
A way to create a new class from an existing class.
A method to copy variables.
A function to delete classes.
A way to define a variable.
What is the output of the following code: x = 41; if x > 10: print('Above ten,'); if x > 20: print('and also above 20!') else: print('but not above 20.')?
No output.
Above ten!
Above ten, but not above 20.
Above ten, and also above 20!
Can you list Python's primary built-in data types?
Integer, Float, String, List, Tuple.
Text, Numeric, Sequence, Mapping, Set, Boolean, Binary.
Text, Numeric, List, Dictionary, Set.
String, Number, Collection, Object.
What are Membership Operators?
Operators used to create a class.
Operators used to define a function.
Operators used to compare two variables.
Operators used to test if a sequence is present in an object.
Which statement can be used to avoid errors if an if statement has no content?
The return statement.
The pass statement.
The continue statement.
The break statement.
What is the purpose of the 'self' parameter in Python class methods?
It refers to the instance of the class.
It is used to define a global variable.
It is used to call other classes.
It is a placeholder for the class name.
How do you handle exceptions in Python?
Using the error function.
Using the catch statement.
Using the if and else statements.
Using the try and except blocks.
What is the purpose of the 'with' statement in Python?
To simplify exception handling.
To manage resources efficiently.
To create a new thread.
To define a function.
What is the output of the following code: x = [1, 2, 3]; print(x[1])?
Error
3
2
1
How do you create a function in Python?
myFunction() = function:
create myFunction():
function myFunction():
def myFunction():
What does the 'break' statement do in a loop?
It restarts the loop.
It skips the current iteration.
It exits the loop.
It continues to the next iteration.
What is the purpose of the 'return' statement in a function?
To create a loop.
To define a new function.
To exit the function and return a value.
To print a value to the console.
How do you create a list in Python?
list = []
list = ()
list = {}
list = new List()
What is the purpose of the 'elif' statement in Python?
To define a function.
To exit a loop.
To handle multiple conditions in an if statement.
To create a loop.
The command to remove rows from a table ‘CUSTOMER’ is:
REMOVE FROM CUSTOMER …
DROP FROM CUSTOMER …
DELETE FROM CUSTOMER WHERE …
UPDATE FROM CUSTOMER …
The command to eliminate a table from a database is:
REMOVE TABLE CUSTOMER;
DROP TABLE CUSTOMER;
DELETE TABLE CUSTOMER;
UPDATE TABLE CUSTOMER;
To remove duplicate rows from the results of an SQL SELECT statement, the ________ qualifier specified must be included
ONLY
UNIQUE
DISTINCT
SINGLE
With SQL, how do you select all the columns from a table named "Persons"?
SELECT [all] FROM Persons
SELECT Persons
SELECT * FROM Persons
SELECT *.Persons
With SQL, how do you select all the records from a table named "Persons" where the value of the column "FirstName" is "Peter"?
SELECT [all] FROM Persons WHERE FirstName LIKE 'Peter'
SELECT [all] FROM Persons WHERE FirstName='Peter'
SELECT * FROM Persons WHERE FirstName='Peter'
SELECT * FROM Persons WHERE FirstName<>'Peter'
With SQL, how do you select all the records from a table named "Persons" where the value of the column "FirstName" starts with an "a"?
SELECT * FROM Persons WHERE FirstName='a'
SELECT * FROM Persons WHERE FirstName LIKE 'a%'
SELECT * FROM Persons WHERE FirstName='%a%'
SELECT * FROM Persons WHERE FirstName LIKE '%a'
With SQL, how do you select all the records from a table named "Persons" where the "FirstName" is "Peter" and the "LastName" is "Jackson"?
SELECT * FROM Persons WHERE FirstName='Peter' AND LastName='Jackson'
SELECT * FROM Persons WHERE FirstName<>'Peter' AND LastName<>'Jackson'
SELECT FirstName='Peter', LastName='Jackson' FROM Persons
With SQL, how do you select all the records from a table named "Persons" where the "LastName" is alphabetically between (and including) "Hansen" and "Pettersen"?
SELECT * FROM Persons WHERE LastName BETWEEN 'Hansen' AND 'Pettersen'
SELECT * FROM Persons WHERE LastName>'Hansen' AND LastName<'Pettersen'
SELECT LastName>'Hansen' AND LastName<'Pettersen' FROM Persons
With SQL, how can you return all the records from a table named "Persons" sorted descending by "FirstName"?
SELECT * FROM Persons SORT 'FirstName' DESC
SELECT * FROM Persons ORDER BY FirstName DESC
SELECT * FROM Persons SORT BY 'FirstName' DESC
SELECT * FROM Persons ORDER FirstName DESC
With SQL, how can you insert a new record into the "Persons" table?
INSERT ('Jimmy', 'Jackson') INTO Persons
INSERT VALUES ('Jimmy', 'Jackson') INTO Persons
INSERT INTO Persons VALUES ('Jimmy', 'Jackson')
How can you change "Hansen" into "Nilsen" in the "LastName" column in the Persons table?
MODIFY Persons SET LastName='Hansen' INTO LastName='Nilsen
MODIFY Persons SET LastName='Nilsen' WHERE LastName='Hansen'
UPDATE Persons SET LastName='Hansen' INTO LastName='Nilsen'
UPDATE Persons SET LastName='Nilsen' WHERE LastName='Hansen'
With SQL, how can you return the number of records in the "Persons" table?
Next ❯
SELECT NO(*) FROM Persons
SELECT COLUMNS(*) FROM Persons
SELECT COUNT(*) FROM Persons
SELECT LEN(*) FROM Persons
The NOT NULL constraint enforces a column to not accept empty values.
Next ❯
TRUE
FALSE
Which operator is used to search for a specified pattern in a column?
GET
FROM
LIKE
With SQL, how do you select all the records from a table named “Persons” where the value of the column “FirstName” ends with an “a”?
SELECT * FROM Persons WHERE FirstName=’a’
SELECT * FROM Persons WHERE FirstName LIKE ‘a%’
SELECT * FROM Persons WHERE FirstName LIKE ‘%a’
SELECT * FROM Persons WHERE FirstName=’%a%’
SELECT * FROM employee WHERE salary>10000 AND dept_id=101;
Which of the following fields are displayed as output?
Salary, dept_id
Employee
Salary
All the field of employee relation
In the given query which of the keyword has to be inserted?
INSERT INTO employee _____ (1002,Joey,2000);
Table
Values
Relation
Field
CREATE TABLE employee (id INTEGER,name VARCHAR(20),salary NOT NULL);
INSERT INTO employee VALUES (1005,Rach,0);
INSERT INTO employee VALUES (1007,Ross, );
INSERT INTO employee VALUES (1002,Joey,335);
Some of these insert statements will produce an error. Identify the statement.
Insert into employee values (1005,Rach,0);
Insert into employee values (1002,Joey,335);
Insert into employee values (1007,Ross, );
None of the mentioned
Which is not a DDL Command?
CREATE
ALTER
DROP
UPDATE
Which is not a DML Command?
SELECT
INSERT
ALTER
DELETE
Which of the following is not a valid SQL type?
DECIMAL
NUMERIC
FLOAT
CHARACTER
Which of the following are DCL commands?
COMMIT and ROLLBACK
UPDATE and TRUNCATE
GRANT and REVOKE
SELECT and UPDATE
SQL has how many main commands for DDL:
1
2
3
4
Which command is used for removing a table and all its data from the database:
Create command
Drop table command
Alter table command
All of the mentioned
Which of the following language is used to specify database Schema ?
DDL
DML
DCL
None
DDL Stands for ___________.
Data Management Language
Data Manupulation Language
Data Development Language
Data Definition Language
