MySQL Assignment-3
Basic Questions
- Create a table students with columns id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100), age INT NOT NULL.
- Insert 5 students into the students table in a single INSERT statement.
- Create a table courses with course_id INT PRIMARY KEY AUTO_INCREMENT, course_name VARCHAR(100) UNIQUE.
- Insert 3 sample courses.
- Create a table enrollments with composite key (student_id, course_id).
- Insert enrollment data for 3 students in different courses.
- Write SQL to update the age of a student with id = 2 to 25.
- Write SQL to delete all students whose age is less than 18.
- Select student names along with their enrolled courses (JOIN).
- Write SQL to count how many students are enrolled in each course.
- Create a table teachers with id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100), subject VARCHAR(100).
- Insert 3 teachers into the teachers table.
- Write SQL to add a foreign key teacher_id to courses referencing teachers.id.
- Write SQL to fetch all courses with their teacher names.
- Add a created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP column to students.
- Write SQL to show the 2 most recently added students.
- Create an index on students.name.
- Drop the index on students.name.
- Insert a new course but rollback the transaction before commit.
- Insert a new teacher inside a transaction and commit the changes.
Intermediate Questions
- Create a table departments with columns dept_id INT PRIMARY KEY, dept_name VARCHAR(100).
- Insert 3 departments in one SQL statement.
- Add a column dept_id in teachers and set it as a foreign key referencing departments.
- Write SQL to fetch teacher names with their department names (JOIN).
- Write SQL to count how many teachers belong to each department.
- Create a unique index on teachers.name.
- Drop the unique index on teachers.name.
- Write SQL to find duplicate course names if any exist.
- Write SQL to list students who are not enrolled in any course (LEFT JOIN).
- Write SQL to list courses that have no students enrolled (RIGHT JOIN or NOT IN).
- Write SQL to select the top 3 highest ages from students.
- Create a table payments with pay_id INT PRIMARY KEY, student_id INT, amount DECIMAL(10,2), pay_date DATE.
- Insert 5 payment records for different students.
- Write SQL to calculate total payment amount for each student.
- Write SQL to fetch students who have paid more than 5000 total.
- Write SQL to update payment amount for a student and then rollback.
- Write SQL to create a savepoint in a transaction, insert data, then rollback to savepoint.
- Create a covering index for a query selecting student_id, amount from payments where pay_date > ‘2024-01-01’.
- Write SQL to delete all enrollments of a specific student inside a transaction and commit.
- Write SQL to transfer 1000 from one student’s payment record to another using a transaction.
Advanced Questions
- Create a table employees with emp_id INT PRIMARY KEY, emp_name VARCHAR(100), manager_id INT NULL.
- Insert 5 employees with a manager-subordinate hierarchy.
- Write SQL for a self-join to show employees with their managers.
- Create a table projects with proj_id INT PRIMARY KEY, proj_name VARCHAR(100), dept_id INT.
- Insert 3 projects linked to different departments.
- Write SQL to fetch all projects with department names (JOIN).
- Write SQL to assign employees to projects using a junction table emp_projects(emp_id, proj_id) with composite PK.
- Insert data into emp_projects showing at least 2 employees per project.
- Write SQL to fetch employee names with their project names (JOIN).
- Create a transaction in which you insert into employees, assign them to a project, then rollback the assignment but keep the employee.