Database Mangement Cs 12 PDF
Database Mangement Cs 12 PDF
✅ 1. Database Concepts
The Relational Model organizes data into tables (also called relations), which consist of rows and
columns.
Basic Terminology:
• Candidate Key: A column or a combination of columns that can uniquely identify each
record. A table can have multiple candidate keys.
• Primary Key: The candidate key chosen to uniquely identify records. It cannot contain
NULL values.
• Alternate Key: The candidate keys that are not chosen as the primary key.
• Foreign Key: An attribute in one table that refers to the primary key of another table,
establishing a relationship between the two tables.
SQL Commands:
2 of 17
fi
fi
🔥 DML Commands:
🔥 Operators in SQL:
• Mathematical: +, -, *, /, %
• Relational: =, !=, <, >, <=, >=
• Logical: AND, OR, NOT
🎯 Aliasing:
🔥 Distinct Clause:
🔥 Where Clause:
🔥 IN and BETWEEN:
🔥 Meaning of NULL:
🔥 LIKE Clause:
🔥 UPDATE Command:
🔥 Aggregate Functions:
🔥 Joins:
1. Cartesian Product: Combines all rows of one table with all rows of another.
sql
SELECT * FROM students, courses;
2. Equi-Join: Matches rows based on equality condition.
sql
SELECT students.name, courses.course_name
FROM students
JOIN courses ON students.student_id = courses.student_id;
3. Natural Join: Automatically joins tables with the same column name.
sql
SELECT * FROM students
NATURAL JOIN courses;
🐍 4. Python-SQL Connectivity
5 of 17
fi
Steps:
conn = mysql.connector.connect(
host="localhost",
user="root",
password="your_password",
database="school"
)
cursor = conn.cursor()
3. Perform SQL Operations:
python
# Insert record
cursor.execute("INSERT INTO students (name, age) VALUES
('Bob', 20)")
conn.commit()
4. Fetching Data:
Python
cursor.execute("SELECT * FROM students")
rows = cursor.fetchall()
for row in rows:
print(row)
6 of 17
✅ Sample SQL Queries – CBSE 12th Board Exam
1⃣ Create a Database:
sql
CREATE DATABASE school;
2⃣ Use the Database:
sql
USE school;
3⃣ Create a Table:
sql
CREATE TABLE students (
student_id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
age INT,
marks FLOAT,
email VARCHAR(100) UNIQUE
);
4⃣ Describe the Table:
sql
DESCRIBE students;
5⃣ Alter the Table:
• Remove a column:
sql
ALTER TABLE students DROP COLUMN phone_number;
7 of 17
fi
fi
sql
DROP TABLE students;
1⃣ Insert Records:
sql
INSERT INTO students (student_id, name, age, marks, email)
VALUES (101, 'Alice', 18, 85.5, '[email protected]');
sql
UPDATE students
SET marks = 90
WHERE student_id = 101;
4⃣ Delete Records:
sql
DELETE FROM students
WHERE student_id = 102;
8 of 17
fi
sql
SELECT * FROM students
WHERE marks > 80;
2⃣ Use IN and BETWEEN:
• Ascending order:
sql
SELECT * FROM students
ORDER BY marks ASC;
• Descending order:
sql
SELECT * FROM students
ORDER BY name DESC;
• Maximum marks:
sql
SELECT MAX(marks) FROM students;
• Minimum marks:
sql
SELECT MIN(marks) FROM students;
• Average marks:
sql
SELECT AVG(marks) FROM students;
• Sum of marks:
sql
SELECT SUM(marks) FROM students;
• Count of students:
sql
9 of 17
SELECT COUNT(*) FROM students;
2⃣ Grouping and Filtering:
• Group by age:
sql
SELECT age, COUNT(*)
FROM students
GROUP BY age;
🔥 5. Joins
🔥 6. Null Handling
sql
sql
SELECT * FROM students
WHERE email IS NOT NULL;
Here’s how you can connect Python with SQL and run queries:
python
import mysql.connector
cursor = conn.cursor()
# Insert query
cursor.execute("INSERT INTO students (student_id, name, age,
marks) VALUES (103, 'Charlie', 20, 88.5)")
conn.commit()
11 of 17
# Select query
cursor.execute("SELECT * FROM students")
rows = cursor.fetchall()
# Displaying records
for row in rows:
print(row)
Here are some important SQL practice questions based on the CBSE 12th syllabus to help you
prepare effectively.
Q1. Write an SQL query to create a table named employees with the following structure:
12 of 17
fi
• age (Integer)
• salary (Float)
• department (Varchar(30))
👉 Try it before checking the solution!
✅ Solution:
sql
CREATE TABLE employees (
emp_id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
age INT,
salary FLOAT,
department VARCHAR(30)
);
Q2. Write an SQL query to alter the employees table and add a column email of type
VARCHAR(100).
✅ Solution:
sql
ALTER TABLE employees ADD email VARCHAR(100);
Q3. Write an SQL query to drop the department column from the employees table.
✅ Solution:
sql
ALTER TABLE employees DROP COLUMN department;
Q4. Write an SQL query to delete the employees table.
✅ Solution:
sql
DROP TABLE employees;
Q5. Write an SQL query to insert the following records into the employees table:
emp_i nam ag
salary email
d e e
101 John 28 50000.0 [email protected]
[email protected]
102 Mary 30 60000.0
m
103 Alex 25 45000.0 [email protected]
13 of 17
✅ Solution:
sql
INSERT INTO employees (emp_id, name, age, salary, email)
VALUES
(101, 'John', 28, 50000.0, '[email protected]'),
(102, 'Mary', 30, 60000.0, '[email protected]'),
(103, 'Alex', 25, 45000.0, '[email protected]');
sql
UPDATE employees
SET salary = 55000.0
WHERE name = 'John';
Q7. Write an SQL query to delete the record of Alex from the employees table.
✅ Solution:
sql
DELETE FROM employees
WHERE name = 'Alex';
Q8. Write an SQL query to display all records from the employees table.
✅ Solution:
sql
SELECT * FROM employees;
Q9. Write an SQL query to display only name and salary of all employees.
✅ Solution:
sql
SELECT name, salary FROM employees;
Q10. Write an SQL query to list employees whose salary is greater than 50000.
✅ Solution:
sql
SELECT * FROM employees
WHERE salary > 50000;
14 of 17
🔥 4. Filtering Data
Q11. Write an SQL query to list employees whose age is between 25 and 30.
✅ Solution:
sql
SELECT * FROM employees
WHERE age BETWEEN 25 AND 30;
Q12. Write an SQL query to display employees working in "Sales" or "HR" department.
✅ Solution:
sql
SELECT * FROM employees
WHERE department IN ('Sales', 'HR');
Q13. Write an SQL query to display employees whose name starts with 'J'.
✅ Solution:
sql
SELECT * FROM employees
WHERE name LIKE 'J%';
Q14. Write an SQL query to display employees whose name contains 'a' in any position.
✅ Solution:
sql
SELECT * FROM employees
WHERE name LIKE '%a%';
🔥 5. Sorting Data
Q15. Write an SQL query to display all employees sorted by salary in descending order.
✅ Solution:
sql
SELECT * FROM employees
ORDER BY salary DESC;
🔥 6. Aggregate Functions
15 of 17
Q16. Write an SQL query to nd the highest salary in the employees table.
✅ Solution:
sql
SELECT MAX(salary) FROM employees;
Q17. Write an SQL query to nd the total salary of all employees.
✅ Solution:
sql
SELECT SUM(salary) FROM employees;
Q18. Write an SQL query to nd the average salary of employees.
✅ Solution:
sql
SELECT AVG(salary) FROM employees;
Q19. Write an SQL query to count the number of employees in each department.
✅ Solution:
sql
SELECT department, COUNT(*)
FROM employees
GROUP BY department;
Q20. Write an SQL query to list departments where the average salary is greater than 50000.
✅ Solution:
sql
SELECT department, AVG(salary)
FROM employees
GROUP BY department
HAVING AVG(salary) > 50000;
🔥 8. Joins
dept_i department_nam
d e
1 HR
2 Sales
3 IT
16 of 17
fi
fi
fi
Q21. Write an SQL query to join the employees table with departments table on
department.
✅ Solution:
sql
SELECT employees.name, employees.salary,
departments.department_name
FROM employees
JOIN departments
ON employees.department = departments.department_name;
Q22. Write a Python program to connect to an SQL database and fetch all records from the
employees table.
✅ Solution:
python
import mysql.connector
# Connect to MySQL
conn = mysql.connector.connect(
host="localhost",
user="root",
password="your_password",
database="school"
)
cursor = conn.cursor()
# Close connection
cursor.close()
conn.close()
17 of 17