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

LAB4

Uploaded by

binhboonglv2
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)
7 views

LAB4

Uploaded by

binhboonglv2
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/ 5

LAB 4

HE180149-Nguyễn Đức Bình

Question1
CREATE TABLE Departments (
DeptID CHAR(2) PRIMARY KEY,
Name NVARCHAR(50) NOT NULL,
NoOfStudents INT
);

CREATE TABLE Students (


StudentID VARCHAR(4) PRIMARY KEY,
LastName NVARCHAR(10) NOT NULL,
FirstName NVARCHAR(30) NOT NULL,
Sex VARCHAR(1) CHECK (Sex IN ('M', 'F')),
DateOfBirth DATE,
PlaceOfBirth NVARCHAR(30),
DeptID CHAR(2),
Scholarship FLOAT,
AverageScore NUMERIC(4,2),
FOREIGN KEY (DeptID) REFERENCES Departments(DeptID)
);

CREATE TABLE Courses (


CourseID VARCHAR(4) PRIMARY KEY,
Name NVARCHAR(35) NOT NULL,
Credits TINYINT
);

CREATE TABLE Results (


StudentID VARCHAR(4),
CourseID VARCHAR(4),
Year INT,
Semester INT,
Mark FLOAT CHECK (Mark BETWEEN 0 AND 10),
Grade VARCHAR(6),
PRIMARY KEY (StudentID, CourseID, Year, Semester),
FOREIGN KEY (StudentID) REFERENCES Students(StudentID),
FOREIGN KEY (CourseID) REFERENCES Courses(CourseID)
);

INSERT INTO Departments (DeptID, Name, NoOfStudents) VALUES


('IS', 'Information Systems', NULL),
('NC', 'Network and Communication', NULL),
('SE', 'Software Engineering', NULL),
('CE', 'Computer Engineering', NULL),
('CS', 'Computer Science', NULL);

INSERT INTO Students (StudentID, LastName, FirstName, Sex, DateOfBirth, PlaceOfBirth, DeptID,
Scholarship, AverageScore) VALUES
('S001', 'Lê', 'Kim Lan', 'F', '1990-02-23', 'Hà nội', 'IS', 130000, NULL),
('S002', 'Trần', 'Minh Chánh', 'M', '1992-12-24', 'Bình Định', 'NC', 150000, NULL),
('S003', 'Lê', 'An Tuyết', 'F', '1991-02-21', 'Hải phòng', 'IS', 170000, NULL),
('S004', 'Trần', 'Anh Tuấn', 'M', '1993-12-20', 'TpHCM', 'NC', 80000, NULL),
('S005', 'Trần', 'Thị Mai', 'F', '1991-08-12', 'TpHCM', 'SE', 0, NULL),
('S006', 'Lê', 'Thị Thu Thủy', 'F', '1991-01-02', 'An Giang', 'IS', 0, NULL),
('S007', 'Nguyễn', 'Kim Thư', 'F', '1990-02-02', 'Hà Nội', 'SE', 180000, NULL),
('S008', 'Lê', 'Văn Long', 'M', '1992-12-08', 'TpHCM', 'IS', 190000, NULL);

INSERT INTO Courses (CourseID, Name, Credits) VALUES


('DS01', 'Database Systems', 3),
('AI01', 'Artificial Intelligence', 3),
('CN01', 'Computer Network', 3),
('CG01', 'Computer Graphics', 4),
('DSA1', 'Data Structures and Algorithms', 4);

INSERT INTO Results (StudentID, CourseID, Year, Semester, Mark, Grade) VALUES
('S001', 'DS01', 2017, 1, 3, NULL),
('S001', 'DS01', 2017, 2, 6, NULL),
('S001', 'AI01', 2017, 1, 4.5, NULL),
('S001', 'AI01', 2017, 2, 6, NULL),
('S001', 'CN01', 2017, 3, 5, NULL),
('S002', 'DS01', 2016, 1, 4.5, NULL),
('S002', 'DS01', 2017, 1, 7, NULL),
('S002', 'CN01', 2016, 3, 10, NULL),
('S002', 'DSA1', 2016, 3, 9, NULL),
('S003', 'DS01', 2017, 1, 2, NULL),
('S003', 'DS01', 2017, 3, 5, NULL),
('S003', 'CN01', 2017, 2, 2.5, NULL),
('S003', 'CN01', 2017, 3, 4, NULL),
('S004', 'DS01', 2017, 3, 4.5, NULL),
('S004', 'DSA1', 2018, 1, 10, NULL),
('S005', 'DS01', 2017, 2, 7, NULL),
('S005', 'CN01', 2017, 2, 2.5, NULL),
('S005', 'CN01', 2018, 1, 5, NULL),
('S006', 'AI01', 2018, 1, 6, NULL),
('S006', 'CN01', 2018, 2, 10, NULL);

Question2: Update NoOfStudents of each department in Departments


table where NoOfStudents is the total number of students of each
departments. Note that for department that has no student, the
NoOfStudents should be 0.
UPDATE Departments

SET NoOfStudents FROM Students = (SELECT COUNT(*)

WHERE Departments. DeptID=Students. DeptID)


Question3:Update AverageScore for each student so that for each
course, we take only his/her highest Mark and the AverageScore of the
student is calculated as the average mark of all the courses that the
student joins.

UPDATE Students

SET AverageScore = ( SELECT Sum (Mark)/COUNT(StudentID) FROM Results

WHERE Students. StudentID=Results. StudentID)

Question 4: Update Grade in table Results so that:

+)Grade = 'Passed' if 5<= Mark <= 10

+)Grade = 'Failed' if 0<= Mark < 5

Update Results

Set Grade = case

when Mark <=10 and Mark >= 5 then 'Passed'

else 'Failed'

End

Question 5. List (StudentID, Fullname, DateOfBirth, PlaceOfBirth, DeptID, Scholarship) of all


students having Scholarship not greater than 160000, in descending order of Scholarship.
Note that FullName is the concatenation of LastName and FirstName. For example, if
LastName = 'Lê' and FirstName = 'Kim Lan', then Fullname should be 'Kim Lan Lê'.

Select StudentID, CONCAT (FirstName, LastName) as FullName, DateOfBirth, PlaceOfBirth,


DeptID, Scholarship from Students

where Scholarship<=160000

Question 6.

List (DeptID, DepartmentName, StudentID, LastName, FirstName) of all departments (KHOA)


so that we see also departments which have no students.

select Departments.DeptID Name as DepartmentName, StudentID, LastName, FirstName

from Departments
left join Students on
Departments.DeptID=Students. DeptID

ORDER BY Departments. DeptID


Question 7. List (StudentID, LastName, FirstName, NumberOfCourses) of all students, show
the results in ascending order of NumberOfCourses where NumberOfCourses is the total
number of courses studied by each student.

select Students. StudentID, LastName, FirstName,


count (Results. Semester)as NumberOfCourses
from Students

join Results on Students. StudentID=Results. StudentID Group by Students. StudentID,


LastName, FirstName
Question 8. List (DeptID, DepartmentName, NumberOfFemaleStudents,
NumberOfMaleStudents) of all departments.

C1:

select Departments. DeptID, name,

SUM(case when Sex like 'M' then 1 else 0 end) as NumberOfMaleStudents,


SUM (case when Sex like'F' then 1 else 0 end) as NumberOfFemaleStudents
from Departments

left join Students on Students. DeptID=Departments. DeptID group by Departments.


DeptID, name

C2:

select Departments. DeptID, name, COUNT (Sex) as NumberOfFemaleStudents, null as


NumberOfMaleStudents
from Departments

join Students on Departments. DeptID=Students. DeptID

where Sex like'F'

group by Departments. DeptID, name

UNION

select Departments. DeptID, name, null as NumberOfFemaleStudents, COUNT (Sex) as


NumberOfMaleStudents

from Departments

left join Students on where Sex like'M' Departments. DeptID=Students. DeptID

group by Departments. DeptID, name


Question 9.

Show the list of students which are not in the department 'Information Systems' but having
Mark of Database Systems greater than at least one student of department 'Information
Systems'.
select students. StudentID, LastName, FirstName, DeptID

You might also like