0% found this document useful (0 votes)
10 views18 pages

Assignment SQL

Uploaded by

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

Assignment SQL

Uploaded by

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

Assignment No:

Milestone 2 of Coursework 1

Assignment Title:
Milestone 2 GB Training
Part A Ans:

CREATE TABLE CourseType (


Coursetypid INT PRIMARY KEY,
Coursetypname VARCHAR(50) NOT NULL
);

CREATE TABLE AllocatedStudent (


Allocatedstuid INT PRIMARY KEY,
Cucourseid INT,
FOREIGN KEY (Cucourseid) REFERENCES CurrentCourse(Cucourseid)
);
CREATE TABLE CurrentCourse (
Cucourseid INT PRIMARY KEY,
Cucoursename VARCHAR(10) NOT NULL,
Cucoursetitle VARCHAR(100) NOT NULL,
Coursetypid INT,
Cucoursestartdate DATE,
Cucourseenddate DATE,
Cucoursecost DECIMAL(10,2),
FOREIGN KEY (Coursetypid) REFERENCES CourseType(Coursetypid)
);
CREATE TABLE Allocation (
Allocatedstuid INT PRIMARY KEY,
Cucourseid INT,
Rid INT,
Trnrid INT,
Weekallocstartdate DATE,
FOREIGN KEY (Cucourseid) REFERENCES CurrentCourse(Cucourseid),
FOREIGN KEY (Rid) REFERENCES Room(Rid),
FOREIGN KEY (Trnrid) REFERENCES Trainer(Trnrid)
);
CREATE TABLE Room (
Rid INT PRIMARY KEY,
Rname VARCHAR(50) NOT NULL,
Rtype VARCHAR(50)
);
CREATE TABLE Trainer (
Trnrid INT PRIMARY KEY,
Trnrname VARCHAR(100) NOT NULL
);
Part B Ans:

INSERT INTO CourseType (Coursetypid, Coursetypname) VALUES


(1, 'Workshop'),
(2, 'Seminar'),
(3, 'Conference');
INSERT INTO Trainer (Trnrid, Trnrname) VALUES
(1, 'John Smith'),
(2, 'Jane Doe');
INSERT INTO Room (Rid, Rname, Rtype) VALUES
(1, 'Room 101', 'AV Facilities'),
(2, 'Room 102', 'Regular Room');

INSERT INTO CurrentCourse (Cucourseid, Cucoursename, Cucoursetitle, Coursetypid,


Cucoursestartdate, Cucourseenddate, Cucoursecost) VALUES
(1, 'CIMW', 'Introduction to Web Development', 1, '2023-01-01', '2023-02-01', 100.00),
(2, 'CAMW', 'Advanced Web Development', 2, '2023-02-15', '2023-03-15', 150.00);
INSERT INTO Allocation (Allocatedstuid, Cucourseid, Rid, Trnrid, Weekallocstartdate) VALUES
(1, 1, 1, 1, '2023-01-01'),
(2, 2, 2, 2, '2023-02-15');
INSERT INTO AllocatedStudent (Allocatedstuid , Cucourseid) VALUES
(101, 1),
(102, 2);

Part C Ans:

Part C(1):
SELECT Cucoursename, Cucoursestartdate, Cucourseenddate
FROM CurrentCourse
ORDER BY Cucoursename, Cucoursestartdate;
Part C (2)
SELECT Cucoursename, Cucoursetitle
FROM CurrentCourse
WHERE Cucoursetitle LIKE '%Introduction%';
Part C (3)
SELECT MAX(Cucoursecost) AS HighCost, MIN(Cucoursecost) AS LowCost
FROM CurrentCourse;

Part C (4)
SELECT c.Cucoursename, COUNT(s.Allocatedstuid ) AS StudentsNo
FROM CurrentCourse c
JOIN AllocatedStudent s ON c.Cucourseid = s.Cucourseid
GROUP BY c.Cucoursename;
Part C (5)
SELECT Cucoursename, Cucoursestartdate, Cucourseenddate
FROM CurrentCourse
WHERE Cucoursestartdate BETWEEN '2023-01-01' AND '2023-12-31';
Part C (6)
SELECT c.Cucoursename, COUNT(*) AS CoursesNo
FROM CurrentCourse c
WHERE Cucoursestartdate BETWEEN '2023-01-01' AND '2023-12-31'
GROUP BY c.Cucoursename;

Part C (7)
SELECT a.Cucourseid, r.Rname, r.Rtype, t.Trnrname, a.Weekallocstartdate
FROM Allocation a
JOIN Room r ON a.Rid = r.Rid
JOIN Trainer t ON a.Trnrid = t.Trnrid;
Part C (8)
SELECT Allocatedstuid
FROM AllocatedStudent
WHERE Cucourseid = (SELECT Cucourseid FROM CurrentCourse WHERE Cucoursename = 'CIMW'
AND Cucoursestartdate = '2018-11-12');
Part C (9)
SELECT c.Cucoursename, c.Cucoursestartdate, c.Cucoursecost, COUNT(s.Allocatedstuid ) AS
NumberOfStudents,
(COUNT(s.Allocatedstuid ) * c.Cucoursecost) AS Revenue
FROM CurrentCourse c
JOIN AllocatedStudent s ON c.Cucourseid = s.Cucourseid
GROUP BY c.Cucoursename, c.Cucoursestartdate, c.Cucoursecost;

You might also like