Lab 1: Introduction to SQL & Database Creation
OBJECTIVE: Create a new database and understand the basic structure of SQL.
SQL Query:
CREATE DATABASE CollegeDB;
USE CollegeDB;
CREATE TABLE Students (
Student_ID INT PRIMARY KEY,
Name VARCHAR(50),
Age INT,
Department VARCHAR(50)
);
OUTPUT: Database CollegeDB
Student_ID Name Age Department
Lab 2: Creating and Managing Tables
OBJECTIVE: Learn to define tables with constraints and data types.
SQL Query:
CREATE TABLE Courses (
Course_ID INT PRIMARY KEY,
Course_Name VARCHAR(50) NOT NULL,
Credits INT CHECK (Credits BETWEEN 1 AND 5)
);
INSERT INTO Courses(Course_ID, Course_Name, Credits)
VALUES(101,’CS’,4),
(102, ‘Advanced Mathematics’,3),
(103, ‘Physics Principles’ ,5);
OUTPUT: Table Courses created with proper constraints.
Course_ID Course_Name Credits
101 CS 4
102 Advanced Mathematics 3
103 Physics Principles 5
Lab 3: Inserting and Retrieving Data using SELECT
OBJECTIVE: Perform INSERT and SELECT queries with different conditions.
SQL Query:
INSERT INTO Students VALUES (1, 'Alice', 20, 'CS');
INSERT INTO Students VALUES (2, 'Bob', 21, 'Math');
INSERT INTO Students VALUES (3, 'Charlie', 22, 'Physics');
SELECT * FROM Students;
SELECT * FROM Cources;
INSERT INTO Enrollments(Enrollment_ID, Student_ID, Course_ID,Enrollment_Date)
VALUES(1,1,101,’2024-06-01’),
(2,2,102,’2024-06-05’),
(3,3,103,’2024-06-15’);
SELECT * FROM Enrollments;
OUTPUT: Student Table
Student_ID Name Age Department
1 Alice 20 CS
2 Bob 22 Math
3 Charlie 23 Physics
Cources Table
Course_ID Course_Name Credits
101 CS 4
102 Advanced Mathematics 3
103 Physics Principles 5
Enrollment Table
Enroll_ID Student_ID Course_ID Enrollment_Date
1 1 101 2024-06-01
2 2 102 2024-06-05
3 3 103 2024-06-15
Lab 4: Filtering Data with WHERE and LIKE
OBJECTIVE: Apply WHERE , LIKE , In, BETWEEN, and ORDER by clauses.
SQL Query:
-- Select students older than 21
SELECT * FROM Students
WHERE Age > 21;
-- Select students whose names start with 'A'
SELECT * FROM Students
WHERE Name LIKE 'A%';
);
-- Select enrollments between specific dates
SELECT * FROM Enrollments
WHERE EnrollmentDate BETWEEN '2024-06-01' AND '2024-06-10';
-- Display all students ordered by Age descending
SELECT * FROM Students
ORDER BY Age DESC;
OUTPUT:Students older than 21:
Student_ID Name Age Course
2 Bob 21+ Maths
3 Charlie 21+ Physics
Students with names starting with "A": Alice
Student_ID Name Age Course
1 Alice 20 CS
Enrollment between 2024-06-01 and 2024-06-15:
Enroll_ID Student_ID Cource_ID Enrollment_Date
1 1 101 2024-06-01
2 2 102 2024-06-05
3 3 103 2024-06-15
Student order by Age
Student_ID Name Age Course
3 Charlie 23 CS
2 Bob 22 Maths
1 Alice 20 Physics
Lab 5: Updating and Deleting Data
OBJECTIVE: Use UPDATE and DELETE queries efficiently.
SQL Query:
UPDATE Students SET Age = 24 WHERE Name = 'Charlie';
DELETE FROM Students WHERE Name = 'Bob';
SELECT * FROM Students;
OUTPUT:
Student_ID Name Age Department
1 Alice 20 CS
3 Charlie 24 Physics
Lab 6: Applying Constraints
OBJECTIVE: Understand and apply various SQL constraints: NOT NULL, UNIQUE , CHECK , PRIMARY KEY ,
FOREIGN KEY.
SQL Query:
CREATE TABLE Faculty(
Faculty_ID INT PRIMARY KEY,
Name VARCHAR(50) NOT NULL,
Department VARCHAR(50) ,
Email VARCHAR(100) UNIQUE,
Salary DECIMAL(10,2) CHECK (Salary>=30000)
);
-- Inserting valid data
INSERT INTO Faculty (Faculty_ID, Name, Department, Email, Salary) VALUES
(1, 'Dr. Mehta', 'Computer Science', '[email protected]', 55000),
(2, 'Dr. Sharma', 'Mathematics', '[email protected]', 48000),
(3, 'Dr. Khan', 'Physics', '[email protected]', 60000);
OUTPUT: Facult y Table
Faculty_ID Name Department Email Salary
1 Dr. Mehta CS
[email protected] 55000.00
2 Dr. Sharma Maths
[email protected] 48000.00
m
3 Dr. Khan Physics
[email protected] 60000.00
Lab 7: Sorting and Aggregate Functions
OBJECTIVE: Use SQL aggregate functions ( Count() , Sum() , AVG() , MAX() and MIN() ) .
SQL Query:
SELECT
SELECT COUNT(*) FROM Students;
SELECT AVG(Age) FROM Students;
SELECT MAX(Age) FROM Students;
OUTPUT:
COUNT
Total Students
3
AVG
Average of Student(Age)
22
MAX
Maximum Age
24
Min
Minimum Age
20
Lab 8: Joins in SQL
OBJECTIVE : Understand and use different types of SQL joins to combine data from multiple tables.
SQL Query:
SELECT s.Name, c.CourseName
FROM Students s
JOIN Enrollments e ON s.Student_ID = e.Student_ID
JOIN Courses c ON e.Course_ID = c.Course_ID;
OUTPUT: List of students and their enrolled course
Student-ID Name Course Name Enrollment_Date
1 Alice CS 2024-06-01
2 Bob Advanced Mathematics 2024-06-05
3 Charlie Physics Priciples 2024-06-15
Lab 9: Subqueries and Nested Queries
OBJECTIVE: Use Subqueries to perform advanced data retrieval operations in SQL.
SQL Query:
SELECT Name FROM Students
WHERE Student_ID IN (
SELECT Student_ID FROM Enrollments WHERE Grade = 'A'
);
OUTPUT: Students who received grade "A".
Name
Alice
Bob
Charlie
Lab 10: Creating Views for Complex Queries
OBJECTIVE: Learn how to create and use SQL views to simplify complex queries and enhance reusability
and security.
SQL Query:
CREATE VIEW StudentCourseView AS
SELECT s.Name, c.CourseName, e.Grade
FROM Students s
JOIN Enrollments e ON s.Student_ID = e.Student_ID
JOIN Courses c ON e.Course_ID = c.Course_ID;
SELECT * FROM StudentCourseView;
OUTPUT:
Returns Student names, courses, and grades.
Name Course_Name Grade
Alice Maths A
Alice CS B
Bob Physics A
Bob Maths C
Charlie CS A
Lab 11: Stored Procedures and Functions
OBJECTIVE: Create reusable SQL stored procedures and user-defined functions for modular code.
SQL Query (SQL Server):
CREATE PROCEDURE GetStudentDetails
AS
BEGIN
SELECT * FROM Students;
END;
EXEC GetStudentDetails;
OUTPUT: Returns all student records.
Student_ID Name Age Grade
1 Alice 20 A
2 Bob 22 C
3 Charlie 24 A
Lab 12: Transactions and Rollback Mechanism
OBJECTIVE: Understand how to use BEGIN TRANSACTION, COMMIT, and ROLLBACK in SQL to ensure data
consistency.
SQL Query:
BEGIN TRANSACTION;
DELETE FROM Students WHERE Student_ID = 1;
ROLLBACK;
SELECT * FROM Students;
OUTPUT:
Student_ID Name Age Department Grade
2 Bob 22 Physics ‘A’
3 Charlie 24 CS ‘A’
Lab 13: Indexing for Performance Optimization
OBJECTIVE: Learn about indexes and their impact on query performance.
SQL Query:
CREATE INDEX idx_name ON Students(Name);
OUTPUT:
Index Name
1 Alice
2 Bob
3 Charlie
Lab 14: Triggers in SQL
OBJECTIVE: Create and message SQL triggers to automate tasks.
SQL Query (MySQL-style):
CREATE TABLE LogTable (Message VARCHAR(100));
CREATE TRIGGER after_student_insert
AFTER INSERT ON Students
FOR EACH ROW
BEGIN
INSERT INTO LogTable VALUES ('Student record inserted');
END;
OUTPUT:
Message
Student record inserted
Lab 15: Backup and Restore Databases
OBJECTIVE: Learn how to take database backups and restore them .
SQL Query (MySQL example):
-- Backup:
mysqldump -u root -p CollegeDB > CollegeDB_backup.sql;
-- Restore:
mysql -u root -p CollegeDB < CollegeDB_backup.sql;
OUTPUT:
Database backup and restored successfully.