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

SQL-2(12th)

The document consists of a test with three sections: Theory, SQL Queries, and Python-SQL Interface, covering key concepts in relational databases, SQL commands, and Python database connectivity. It includes questions on candidate keys, foreign keys, SQL clauses, aggregate functions, and practical SQL queries using provided tables. Additionally, it requires writing Python scripts to interact with databases, demonstrating knowledge of database operations and terminology.

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-2(12th)

The document consists of a test with three sections: Theory, SQL Queries, and Python-SQL Interface, covering key concepts in relational databases, SQL commands, and Python database connectivity. It includes questions on candidate keys, foreign keys, SQL clauses, aggregate functions, and practical SQL queries using provided tables. Additionally, it requires writing Python scripts to interact with databases, demonstrating knowledge of database operations and terminology.

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/ 4

Section A: Theory (20 Marks)

Q1. What is the significance of a candidate key? How does it differ from a primary key? Give examples. (3
Marks)
Q2. Define degree and cardinality in the context of a relational database. Illustrate with an example. (2 Marks)
Q3. Explain the role of foreign keys in maintaining relationships between tables. How does it enforce referential
integrity? Provide an example. (3 Marks)
Q4. Describe the following SQL clauses with examples:
a. DISTINCT
b. ORDER BY
c. LIKE (pattern matching)
(6 Marks)
Q5. Explain the differences between DDL and DML commands with examples of at least two commands from
each category. (3 Marks)
Q6. Write a short note on aggregate functions in SQL. Explain with examples. (3 Marks)
Section B: SQL Queries (20 Marks)
Q7. Consider the following tables:
Table 1: Employees
EmpID Name DeptID Salary
1 Aman 101 50000
2 Bhavya 102 60000
3 Charu 101 55000
Table 2: Departments
DeptID DeptName Location
101 HR Mumbai
102 IT Delhi
Write SQL queries for the following:
a. Retrieve the names of employees working in the "HR" department. (2 Marks)
b. Find the total salary paid to employees in each department. (2 Marks)
c. Display all employee names along with their department names using a join operation. (3 Marks)
d. Add a new column Bonus (float) to the Employees table. (1 Mark)
Q8. Write the SQL command to:
a. Create a database named Company.
b. Use the database Company.
c. Drop the database Company.
(3 Marks)
Q9. Using the Employees table, write SQL commands to:
a. Insert a new record with values (4, 'Divya', 103, 45000).
b. Update the Salary of Bhavya to 65000.
c. Delete the record of an employee with EmpID = 3.
(3 Marks)
Q10. Write an SQL query to:
a. Retrieve the details of employees whose salary is between 45000 and 60000.
b. Display the details of employees who do not belong to the "IT" department.
c. Retrieve distinct department names from the Departments table.
(3 Marks)
Q11. Write SQL queries to demonstrate the use of:
a. IS NULL and IS NOT NULL
b. HAVING clause with GROUP BY
(3 Marks)
Section C: Python-SQL Interface (10 Marks)
Q12. Write a Python program to perform the following operations using MySQL:
a. Connect to a database named Library.
b. Create a table Books with columns: BookID (int, primary key), Title (varchar(100)), Author (varchar(50)).
c. Insert a record into the Books table.
(5 Marks)
Q13. Explain the following Python-SQL terms with examples:
a. fetchone() vs. fetchall()
b. Role of the cursor() object in database connectivity
(3 Marks)
Q14. A database table Sales contains the following columns: SaleID, Product, Quantity, and Amount. Write a
Python script to retrieve and display all records from this table using a SQL query. (2 Marks)
Answer Key: Grade 12 Periodic Test 2 (50 Marks)

Section A: Theory (20 Marks)


Q1. (3 Marks)
 Candidate Key: A set of attributes uniquely identifying a record (e.g., RollNo).
 Primary Key: A candidate key chosen to uniquely identify records.
Example: Candidate Keys: RollNo, Email. Primary Key: RollNo.
Q2. (2 Marks)
 Degree: Number of attributes in a relation.
 Cardinality: Number of tuples in a relation.
Example: A table with 4 columns (degree = 4) and 5 rows (cardinality = 5).
Q3. (3 Marks)
 Foreign Key: Enforces referential integrity by ensuring the referenced value exists.
Example: DeptID in Employees table referencing Departments table.
Q4. (6 Marks)
 DISTINCT: SELECT DISTINCT DeptID FROM Employees;
 ORDER BY: SELECT * FROM Employees ORDER BY Salary DESC;
 LIKE: SELECT * FROM Employees WHERE Name LIKE 'A%';
Q5. (3 Marks)
 DDL: Create, Drop.
Example: CREATE TABLE Students (...);
 DML: Insert, Update.
Example: INSERT INTO Students VALUES (...);
Q6. (3 Marks)
 Functions: MAX(), MIN(), AVG(), SUM(), COUNT().
Example: SELECT MAX(Salary) FROM Employees;
Section B: SQL Queries (20 Marks)
Q7. (8 Marks)
a. SELECT Name FROM Employees e JOIN Departments d ON e.DeptID = d.DeptID WHERE DeptName =
'HR';
b. SELECT DeptID, SUM(Salary) AS TotalSalary FROM Employees GROUP BY DeptID;
c. SELECT e.Name, d.DeptName FROM Employees e JOIN Departments d ON e.DeptID = d.DeptID;
d. ALTER TABLE Employees ADD Bonus FLOAT;
Q8. (3 Marks)
CREATE DATABASE Company;
USE Company;
DROP DATABASE Company;
Q9. (3 Marks)
a. INSERT INTO Employees VALUES (4, 'Divya', 103, 45000);
b. UPDATE Employees SET Salary = 65000 WHERE Name = 'Bhavya';
c. DELETE FROM Employees WHERE EmpID = 3;
Q10. (3 Marks)
a. SELECT * FROM Employees WHERE Salary BETWEEN 45000 AND 60000;
b. SELECT * FROM Employees WHERE DeptID != 102;
c. SELECT DISTINCT DeptName FROM Departments;
Q11. (3 Marks)
a.
SELECT * FROM Employees WHERE Bonus IS NULL;
SELECT * FROM Employees WHERE Bonus IS NOT NULL;
b.
SELECT DeptID, AVG(Salary) FROM Employees GROUP BY DeptID HAVING AVG(Salary) > 50000;
Section C: Python-SQL Interface (10 Marks)
Q12. (5 Marks)
import mysql.connector
db = mysql.connector.connect(host="localhost", user="root", password="password",
database="Library")
cursor = db.cursor()
cursor.execute("CREATE TABLE Books (BookID INT PRIMARY KEY, Title VARCHAR(100), Author
VARCHAR(50));")
cursor.execute("INSERT INTO Books VALUES (1, 'Python Basics', 'John Doe');")
db.commit()
db.close()
Q13. (3 Marks)
 fetchone():
Fetches the next row of a query result.
 fetchall(): Fetches all remaining rows of a query result.
 cursor(): Facilitates interaction with the database (execute queries, fetch results).
Q14. (2 Marks)
import mysql.connector
db = mysql.connector.connect(host="localhost", user="root", password="password",
database="SalesDB")
cursor = db.cursor()
cursor.execute("SELECT * FROM Sales;")
rows = cursor.fetchall()
for row in rows:
print(row)
db.close()

You might also like