DBMS Practicals
DBMS Practicals
9. SQL Subqueries
Concepts: Simple Subqueries, Correlated Subqueries
Practical: Using Subqueries for Data Extraction
A: Find students who are enrolled in more than 3 courses
SELECT StudentID, Name, COUNT(CourseID) AS TotalCourses
FROM Students
JOIN Enrollments ON Students.StudentID = Enrollments.StudentID
GROUP BY StudentID, Name
HAVING COUNT(CourseID) > 3;
1. Views in SQL
Concepts: Creating, Using, and Dropping Views
Practical: Managing Views for Better Query Performance
A: Create a view of all students enrolled in Computer Science
CREATE VIEW ComputerScienceStudents AS
SELECT s.StudentID, s.Name, s.Email, s.CourseID, d.DepartmentName
FROM Students s
JOIN Departments d ON s.DepartmentID = d.DepartmentID
WHERE d.DepartmentName = 'Computer Science';
DELIMITER ;
DELIMITER ;
DELIMITER $$
DELIMITER ;
3. Transactions and ACID Properties
Concepts: COMMIT, ROLLBACK, SAVEPOINT
Practical: Handling Transactions in SQL
A: Implement a transaction for course enrollments
START TRANSACTION;
SAVEPOINT sp1;
-- If we want to undo only the second insert but keep the first:
ROLLBACK TO sp1;
START TRANSACTION;
UPDATE courses SET Credits = 3 WHERE CourseID = 5;
UPDATE students SET Age = 23 WHERE StudentID = 101;
COMMIT;
START TRANSACTION;
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
SELECT * FROM enrollments WHERE Semester = 'Spring 2025';
COMMIT;