0% found this document useful (0 votes)
3 views

SQL-3(12th)

The document outlines a Grade 12 periodic test in Computer Science focusing on Database Concepts, with a total of 50 marks and a time allowance of 1.5 hours. It consists of multiple-choice questions, short answer questions, and Python-SQL interface tasks, covering topics such as SQL syntax, data integrity, and database operations. An answer key is provided for the multiple-choice section, along with explanations for short answer questions and Python code examples.

Uploaded by

pdfcliff
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

SQL-3(12th)

The document outlines a Grade 12 periodic test in Computer Science focusing on Database Concepts, with a total of 50 marks and a time allowance of 1.5 hours. It consists of multiple-choice questions, short answer questions, and Python-SQL interface tasks, covering topics such as SQL syntax, data integrity, and database operations. An answer key is provided for the multiple-choice section, along with explanations for short answer questions and Python code examples.

Uploaded by

pdfcliff
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Grade 12 Periodic Test

Subject: Computer Science


Topic: Database Concepts
Maximum Marks: 50
Time Allowed: 1.5 hours

General Instructions:

1. All questions are compulsory.


2. Marks for each question are indicated against it.
3. Use proper SQL syntax wherever required.

Section A: Multiple Choice Questions (MCQs) [10 Marks]

Q1. In a relational database, a tuple refers to:


a. A column in a table
b. A row in a table
c. A table itself
d. A key in a table
(1 Mark)

Q2. Which of the following is a valid SQL constraint?


a. PRIMARY TABLE
b. UNIQUE
c. DISTINCT
d. RENAME
(1 Mark)

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)

Q5. In SQL, the HAVING clause is used with:


a. WHERE clause
b. GROUP BY clause
c. ORDER BY clause
d. SELECT clause
(1 Mark)

Q6. Which of the following is a Data Definition Language (DDL) command?


a. SELECT
b. DELETE
c. CREATE
d. INSERT
(1 Mark)
Q7. What is the maximum number of primary keys a table can have?
a. 0
b. 1
c. 2
d. Unlimited
(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)

Q9. Which SQL command is used to display the structure of a table?


a. SHOW TABLE
b. DESCRIBE table_name
c. LIST TABLE table_name
d. STRUCTURE TABLE table_name
(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)

Section B: Short Answer Questions (20 Marks)

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)

Q13. Write SQL queries for the following:


a. Display all employee names and their departments using a natural join.
b. Retrieve the maximum salary of employees.
c. Find the total salary paid in each department using GROUP BY.
(6 Marks)

Q14. Differentiate between the UPDATE and ALTER TABLE commands in SQL with examples. (4 Marks)

Q15. Write SQL commands to:


a. Add a column Age (INT) to a table Students.
b. Remove the Age column from the table.
(4 Marks)

Section C: Python-SQL Interface (20 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)

Subject: Computer Science


Topic: Database Concepts
Maximum Marks: 50

Section A: Multiple Choice Questions (MCQs) [10 Marks]

Q1. In a relational database, a tuple refers to:


Answer: b. A row in a table
Explanation: A tuple represents a single record or row in a table in a relational database.

Q2. Which of the following is a valid SQL constraint?


Answer: b. UNIQUE
Explanation: UNIQUE is a constraint that ensures all values in a column are distinct. PRIMARY KEY is also a
unique constraint but the best answer here is UNIQUE.

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.

Q5. In SQL, the HAVING clause is used with:


Answer: b. GROUP BY clause
Explanation: The HAVING clause is used to filter groups created by GROUP BY.

Q6. Which of the following is a Data Definition Language (DDL) command?


Answer: c. CREATE
Explanation: CREATE is a DDL command used to define or create database structures like tables.

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.

Q9. Which SQL command is used to display the structure of a table?


Answer: b. DESCRIBE table_name
Explanation: DESCRIBE is used to show the structure of a table (columns, data types, constraints).

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.

Section B: Short Answer Questions (20 Marks)


Q11. Explain the concept of foreign keys with an example. How do they help in maintaining data integrity? (3
Marks)

 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)

 IS NULL: Used to check if a value is NULL.


Example: SELECT * FROM Employees WHERE DeptID IS NULL;
 = NULL: This is incorrect as NULL cannot be compared using the = operator. Always use IS NULL.

Q13. Write SQL queries for the following:


a. Display all employee names and their departments using a natural join.

SELECT e.Name, d.DeptName


FROM Employees e
NATURAL JOIN Departments d;

b. Retrieve the maximum salary of employees.

SELECT MAX(Salary) AS MaxSalary FROM Employees;

c. Find the total salary paid in each department using GROUP BY.

SELECT DeptID, SUM(Salary) AS TotalSalary


FROM Employees
GROUP BY DeptID;

Q14. Differentiate between the UPDATE and ALTER TABLE commands in SQL with examples. (4 Marks)

 UPDATE: Used to modify existing data in a table.


Example: UPDATE Employees SET Salary = 55000 WHERE EmpID = 101;
 ALTER TABLE: Used to modify the structure of a table, such as adding or removing columns.
Example: ALTER TABLE Employees ADD Bonus FLOAT;

Q15. Write SQL commands to:


a. Add a column Age (INT) to a table Students.

ALTER TABLE Students ADD Age INT;

b. Remove the Age column from the table.

ALTER TABLE Students DROP COLUMN Age;

Section C: Python-SQL Interface (20 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);")

# Commit changes and close connection


db.commit()
db.close()

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()

# Retrieve all rows


cursor.execute("SELECT * FROM Orders;")
rows = cursor.fetchall()

# 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;")

You might also like