University Management System - Student Table
Question
Consider a table named Student with the following structure and data:
Student_id | Name | Department | Year |
---|---|---|---|
101 | Rahim | CSE | 2 |
102 | Fatima | EEE | 3 |
103 | Karim | CSE | 1 |
Answer the following:
- Write an SQL INSERT statement to add a new student named “Salma” in the “BBA” department for year 2 with student_id 104.
- Write an SQL UPDATE statement to change “Karim”'s year from 1 to 2.
- Write an SQL DELETE statement to remove the student whose student_id is 102.
- Write an SQL SELECT query to retrieve the names of all students in the “CSE” department.
Answer
i) SQL INSERT statement to add a new student
INSERT INTO Student (Student_id, Name, Department, Year)
VALUES (104, 'Salma', 'BBA', 2);
Explanation: This inserts a new student named Salma in the BBA department, year 2, with ID 104.
ii) SQL UPDATE statement to change Karim's year from 1 to 2
UPDATE Student
SET Year = 2
WHERE Name = 'Karim';
Explanation: Updates Karim's year to 2 in the Student table.
iii) SQL DELETE statement to remove the student with student_id 102
DELETE FROM Student
WHERE Student_id = 102;
Explanation: Deletes the student record with ID 102 (Fatima).
iv) SQL SELECT query to retrieve names of students in the “CSE” department
SELECT Name
FROM Student
WHERE Department = 'CSE';
Explanation: Retrieves names of all students whose department is CSE.