Isc 305 Assignment Explained
Isc 305 Assignment Explained
3. Create Tables
1. Design the Table Structure:
Example: For the Students table:
-- Courses table
CREATE TABLE Courses (
CourseCode VARCHAR(10) PRIMARY KEY,
Title VARCHAR(100) NOT NULL,
Dept VARCHAR(50) NOT NULL
);
-- Registration table
CREATE TABLE Registration (
CourseCode VARCHAR(10),
RegNo VARCHAR(15),
TotalMark INT,
PRIMARY KEY (CourseCode, RegNo),
FOREIGN KEY (CourseCode) REFERENCES Courses(CourseCode),
FOREIGN KEY (RegNo) REFERENCES Students(RegNo)
);
---
---
2. Query Examples:
Explanation
SELECT DISTINCT Students.StudName: This part of the query selects the StudName
column from the Students table. The DISTINCT keyword ensures that the result only
includes unique student names (i.e., no duplicates).
FROM Students: Indicates that the main table we are selecting data from is the Students
table.
WHERE Courses.Title = 'Database Systems': The WHERE clause filters the results to only
include students who have registered for the course titled 'Database Systems'.
What It Does:
This query retrieves the names of all students who have enrolled in the course called
'Database Systems'. The use of DISTINCT ensures that each student’s name appears only
once, even if they are registered in multiple courses or have multiple records in the
Registration table.
Explanation
SELECT Courses.Title, COUNT(Registration.RegNo) AS NumberOfStudents: This selects
the Title column from the Courses table and counts the number of registration records
(Registration.RegNo) for each course. The result is given an alias NumberOfStudents for
better readability.
FROM Courses: The main table for the query is Courses.
GROUP BY Courses.Title: This clause groups the data by the Title of each course, so the
count is calculated per course.
ORDER BY NumberOfStudents DESC: This orders the result in descending order based on
the number of students enrolled in each course, showing courses with the highest number of
registrations first.
What It Does:
This query counts and displays the number of students enrolled in each course and orders
the results from the highest to the lowest number of students.
Explanation
SELECT Students.StudName, AVG(Registration.TotalMark) AS AverageMark: This part of
the query selects the StudName from the Students table and calculates the average of the
TotalMark column from the Registration table. The result is labeled as AverageMark.
GROUP BY Students.StudName: This groups the results by StudName, ensuring that the
average mark is calculated for each individual student.
What It Does:
This query calculates and displays the average marks for each student based on their
registrations. The result includes the student’s name and their average score across all
registered courses.