0% found this document useful (0 votes)
2 views

SQL_Practical_Solutions

The document provides SQL practical solutions for creating and manipulating database tables, specifically for Teachers, Courses, and Students. It includes commands for defining tables, inserting data, updating records, and retrieving information through various queries. Additionally, it covers advanced queries to analyze student grades and enrollment statistics.

Uploaded by

nkwameernest
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

SQL_Practical_Solutions

The document provides SQL practical solutions for creating and manipulating database tables, specifically for Teachers, Courses, and Students. It includes commands for defining tables, inserting data, updating records, and retrieving information through various queries. Additionally, it covers advanced queries to analyze student grades and enrollment statistics.

Uploaded by

nkwameernest
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

SQL Practical Solutions

Definition (DDL) - 10 Marks

Create the Teachers table


CREATE TABLE Teachers (
teacher_id INT PRIMARY KEY,
full_name VARCHAR(100),
department VARCHAR(100)
);

Create the Courses table with teacher_id as a foreign key


CREATE TABLE Courses (
course_id INT PRIMARY KEY,
course_name VARCHAR(100),
teacher_id INT,
FOREIGN KEY (teacher_id) REFERENCES Teachers(teacher_id)
);

Alter the Students table to add an email column


ALTER TABLE Students ADD COLUMN email VARCHAR(50);

Drop the email column from the Students table


ALTER TABLE Students DROP COLUMN email;

Manipulation (DML) - 10 Marks

Insert a new teacher named 'Mr. Kofi Ansah' in the Mathematics department
INSERT INTO Teachers (teacher_id, full_name, department)
VALUES (1, 'Mr. Kofi Ansah', 'Mathematics');

Insert a new course 'Algebra' taught by teacher_id = 3


INSERT INTO Courses (course_id, course_name, teacher_id)
VALUES (1, 'Algebra', 3);

Enroll student with student_id = 5 into course_id = 3 with grade = 75


INSERT INTO Enrollments (enrollment_id, student_id, course_id, grade)
VALUES (1, 5, 3, 75);

Update the age of the student with student_id = 5 to 20


UPDATE Students SET age = 20 WHERE student_id = 5;

Delete any course where teacher_id is NULL


DELETE FROM Courses WHERE teacher_id IS NULL;
Retrieval (SELECT) - 14 Marks

Retrieve full names and age of all male students


SELECT first_name, last_name, age FROM Students WHERE gender = 'Male';

List all courses and the teachers who teach them


SELECT Courses.course_name, Teachers.full_name
FROM Courses
JOIN Teachers ON Courses.teacher_id = Teachers.teacher_id;

Find the average grade for each course


SELECT course_id, AVG(grade) AS average_grade FROM Enrollments GROUP BY course_id;

List all students who are not enrolled in any course


SELECT first_name, last_name FROM Students
WHERE student_id NOT IN (SELECT student_id FROM Enrollments);

Retrieve names of students who scored less than 50 in any course


SELECT Students.first_name, Students.last_name
FROM Students
JOIN Enrollments ON Students.student_id = Enrollments.student_id
WHERE Enrollments.grade < 50;

List all departments and the total number of teachers in each department
SELECT department, COUNT(*) AS total_teachers FROM Teachers GROUP BY department;

Advanced Queries - 6 Marks

Retrieve the names of students and their respective grades in all courses, sorted by grade in
descending order
SELECT Students.first_name, Students.last_name, Enrollments.grade
FROM Students
JOIN Enrollments ON Students.student_id = Enrollments.student_id
ORDER BY Enrollments.grade DESC;

Retrieve the list of students who are enrolled in more than one course
SELECT Students.first_name, Students.last_name
FROM Students
JOIN Enrollments ON Students.student_id = Enrollments.student_id
GROUP BY Students.student_id
HAVING COUNT(Enrollments.course_id) > 1;

You might also like