SQL-3(12th)
SQL-3(12th)
General Instructions:
Q3. The command to delete all rows from a table while keeping the structure is:
a. DROP TABLE table_name;
b. DELETE FROM table_name;
c. TRUNCATE TABLE table_name;
d. CLEAR TABLE table_name;
(1 Mark)
Q4. Which of these is a valid Python function to retrieve data from an SQL query?
a. fetchline()
b. fetchrow()
c. fetchone()
d. fetchquery()
(1 Mark)
Q8. Which of the following data types is used to store alphanumeric values in SQL?
a. FLOAT
b. VARCHAR
c. INT
d. DATE
(1 Mark)
Q10. What does the %s format specifier in Python signify when working with SQL queries?
a. A string value
b. An integer value
c. A placeholder for values
d. A database column
(1 Mark)
Q11. Explain the concept of foreign keys with an example. How do they help in maintaining data integrity? (3
Marks)
Q12. What is the difference between IS NULL and = NULL in SQL? Provide an example for each. (3 Marks)
Q14. Differentiate between the UPDATE and ALTER TABLE commands in SQL with examples. (4 Marks)
Q16. Write a Python program to connect to a database named Inventory, create a table Products with columns
ProductID (integer, primary key), ProductName (varchar(50)), and Price (float), and insert two records into it.
(10 Marks)
Q17. A table Orders has columns OrderID, CustomerName, ProductName, and Quantity. Write a Python
program to retrieve and display all rows from this table. (5 Marks)
Q18. Explain the purpose of the following Python-SQL commands with examples:
a. commit()
b. rowcount
c. execute()
(5 Marks)
Answer Key: Grade 12 Periodic Test (Including MCQs)
Q3. The command to delete all rows from a table while keeping the structure is:
Answer: c. TRUNCATE TABLE table_name
Explanation: TRUNCATE removes all rows in a table, but keeps the table structure intact, unlike DELETE which may
leave the structure intact but might not reset identity columns.
Q4. Which of these is a valid Python function to retrieve data from an SQL query?
Answer: c. fetchone()
Explanation: fetchone() retrieves the next row of a query result. Other options like fetchline() and
fetchrow() are not valid.
Q7. What is the maximum number of primary keys a table can have?
Answer: b. 1
Explanation: A table can have only one primary key, but the primary key can consist of multiple columns.
Q8. Which of the following data types is used to store alphanumeric values in SQL?
Answer: b. VARCHAR
Explanation: VARCHAR is used to store alphanumeric values (strings), whereas FLOAT, INT, and DATE are used for
numerical and date/time data.
Q10. What does the %s format specifier in Python signify when working with SQL queries?
Answer: c. A placeholder for values
Explanation: %s is a placeholder used for parameterized queries in Python to safely insert data into an SQL query.
Foreign Key: A foreign key in one table points to the primary key in another table.
Example: In an Employees table, the DeptID column could be a foreign key that references the DeptID
primary key in a Departments table.
Data Integrity: Foreign keys ensure that relationships between tables are consistent. They prevent orphan
records and maintain referential integrity by ensuring that only valid DeptID values are entered in the
Employees table.
Q12. What is the difference between IS NULL and = NULL in SQL? Provide an example for each. (3 Marks)
c. Find the total salary paid in each department using GROUP BY.
Q14. Differentiate between the UPDATE and ALTER TABLE commands in SQL with examples. (4 Marks)
Q16. Write a Python program to connect to a database named Inventory, create a table Products with columns
ProductID (integer, primary key), ProductName (varchar(50)), and Price (float), and insert two records into it.
(10 Marks)
import mysql.connector
# Establish connection
db = mysql.connector.connect(
host="localhost", user="root", password="password", database="Inventory"
)
cursor = db.cursor()
# Create table
cursor.execute("""
CREATE TABLE Products (
ProductID INT PRIMARY KEY,
ProductName VARCHAR(50),
Price FLOAT
);
""")
# Insert records
cursor.execute("INSERT INTO Products VALUES (1, 'Laptop', 50000.00);")
cursor.execute("INSERT INTO Products VALUES (2, 'Smartphone', 25000.00);")
Q17. A table Orders has columns OrderID, CustomerName, ProductName, and Quantity. Write a Python
program to retrieve and display all rows from this table. (5 Marks)
import mysql.connector
# Establish connection
db = mysql.connector.connect(
host="localhost", user="root", password="password", database="SalesDB"
)
cursor = db.cursor()
# Display results
for row in rows:
print(row)
# Close connection
db.close()
Q18. Explain the purpose of the following Python-SQL commands with examples:
a. commit(): Saves any changes made to the database (e.g., after an INSERT, UPDATE, or DELETE).
Example: db.commit()
b. rowcount: Returns the number of rows affected by the last operation.
Example: print(cursor.rowcount)
c. execute(): Executes an SQL command.
Example: cursor.execute("SELECT * FROM Employees;")