-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Assignment 1 - DB - Vinh Nhan #3
Open
hiimnhan
wants to merge
1
commit into
EngineerProOrg:database-assignment-1
Choose a base branch
from
hiimnhan:database-assignment-1
base: database-assignment-1
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"workbench.colorCustomizations": { | ||
"activityBar.background": "#223213", | ||
"titleBar.activeBackground": "#30461B", | ||
"titleBar.activeForeground": "#F7FBF4" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
SELECT stock_name, | ||
SUM( | ||
CASE | ||
WHEN operation = 'Sell' THEN price | ||
WHEN operation = 'Buy' THEN -price | ||
END | ||
) AS capital_gain_loss | ||
FROM stocks | ||
GROUP BY stock_name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
SELECT | ||
'High Salary' AS category, | ||
SUM(CASE WHEN income>50000 THEN 1 ELSE 0 END) AS accounts_count | ||
FROM | ||
accounts | ||
|
||
UNION | ||
|
||
SELECT | ||
'Low Salary' AS category, | ||
SUM(CASE WHEN income<20000 THEN 1 ELSE 0 END) AS accounts_count | ||
FROM | ||
accounts | ||
|
||
UNION | ||
|
||
SELECT | ||
'Average Salary' AS category, | ||
SUM(CASE WHEN (income>=20000 AND income<=50000) THEN 1 ELSE 0 END) AS accounts_count | ||
FROM | ||
accounts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
|
||
CREATE TABLE IF NOT EXISTS Professor ( | ||
prof_id INT PRIMARY KEY NOT NULL AUTO_INCREMENT, | ||
prof_lname VARCHAR(50), | ||
prof_fname VARCHAR(50) | ||
); | ||
CREATE TABLE IF NOT EXISTS Student ( | ||
stud_id INT PRIMARY KEY NOT NULL AUTO_INCREMENT, | ||
stud_fname VARCHAR(50) NOT NULL, | ||
stud_lname VARCHAR(50) NOT NULL, | ||
stud_street VARCHAR(255), | ||
stud_city VARCHAR(50), | ||
stud_zip VARCHAR(10) | ||
); | ||
CREATE TABLE IF NOT EXISTS Course ( | ||
course_id INT PRIMARY KEY NOT NULL AUTO_INCREMENT, | ||
course_name VARCHAR(255) NOT NULL | ||
); | ||
CREATE TABLE IF NOT EXISTS Room( | ||
room_id INT PRIMARY KEY NOT NULL AUTO_INCREMENT, | ||
room_loc VARCHAR(50) NOT NULL, | ||
room_cap VARCHAR(50) NOT NULL, | ||
class_id INT NULL | ||
); | ||
CREATE TABLE IF NOT EXISTS Class ( | ||
class_id INT PRIMARY KEY NOT NULL AUTO_INCREMENT, | ||
class_name VARCHAR(255) NOT NULL, | ||
prof_id INT NOT NULL, | ||
course_id INT NOT NULL, | ||
room_id INT NOT NULL-- add constraint later | ||
); | ||
CREATE TABLE IF NOT EXISTS Enroll( | ||
stud_id INT NOT NULL, | ||
class_id INT NOT NULL, | ||
grade VARCHAR(3) NOT NULL, | ||
PRIMARY KEY (stud_id, class_id) | ||
); | ||
-- Add constraint to Class table | ||
ALTER TABLE Class ADD CONSTRAINT fk_class_professor_prof_id | ||
FOREIGN KEY (prof_id) REFERENCES Professor (prof_id); | ||
|
||
ALTER TABLE Class ADD CONSTRAINT fk_class_course_course_id | ||
FOREIGN KEY (course_id) REFERENCES Course (course_id); | ||
|
||
ALTER TABLE Class ADD CONSTRAINT fk_class_room_room_id | ||
FOREIGN KEY (room_id) REFERENCES Room (room_id); | ||
-- end | ||
|
||
|
||
-- add constraint to Enroll table | ||
ALTER TABLE Enroll ADD CONSTRAINT fk_enroll_student_stud_id | ||
FOREIGN KEY (stud_id) REFERENCES Student (stud_id); | ||
|
||
ALTER TABLE Enroll ADD CONSTRAINT fk_enroll_class_class_id | ||
FOREIGN KEY (class_id) REFERENCES Class (class_id); | ||
-- end | ||
|
||
-- add constraint to Room table | ||
ALTER TABLE Room ADD CONSTRAINT fk_room_class_class_id | ||
FOREIGN KEY (class_id) REFERENCES Class (class_id); | ||
-- end | ||
|
||
-- INSERT | ||
INSERT INTO Professor (prof_lname, prof_fname) | ||
VALUES | ||
('Smith', 'John'), | ||
('Johnson', 'Emily'), | ||
('Brown', 'Michael'), | ||
('Davis', 'Sarah'), | ||
('Miller', 'David'), | ||
('Wilson', 'Jennifer'), | ||
('Anderson', 'Christopher'), | ||
('Thomas', 'Jessica'), | ||
('Taylor', 'Matthew'), | ||
('Moore', 'Amanda'); | ||
|
||
INSERT INTO Student (stud_fname, stud_lname, stud_street, stud_city, stud_zip) | ||
VALUES | ||
('Emma', 'Johnson', '123 Main St', 'New York', '10001'), | ||
('Oliver', 'Williams', '456 Elm St', 'Los Angeles', '90001'), | ||
('Sophia', 'Jones', '789 Oak St', 'Chicago', '60601'), | ||
('Liam', 'Brown', '234 Maple St', 'Houston', '77001'), | ||
('Ava', 'Davis', '567 Pine St', 'Philadelphia', '19101'), | ||
('Noah', 'Wilson', '890 Cedar St', 'Phoenix', '85001'), | ||
('Isabella', 'Lee', '321 Birch St', 'San Antonio', '78201'), | ||
('Mason', 'Taylor', '654 Willow St', 'San Diego', '92101'), | ||
('Charlotte', 'Clark', '987 Spruce St', 'Dallas', '75201'), | ||
('Elijah', 'Moore', '654 Rose St', 'Austin', '78701'); | ||
|
||
INSERT INTO Course (course_name) | ||
VALUES | ||
('Mathematics'), | ||
('English'), | ||
('History'), | ||
('Science'), | ||
('Art'), | ||
('Computer Science'), | ||
('Physics'), | ||
('Biology'), | ||
('Chemistry'), | ||
('Music'); | ||
|
||
-- set class id null than update later on | ||
INSERT INTO Room (room_loc, room_cap, class_id) | ||
VALUE | ||
('Building A, Room 101', '30',NULL), | ||
('Building A, Room 102', '25',NULL), | ||
('Building B, Room 201', '40',NULL), | ||
('Building B, Room 202', '35',NULL), | ||
('Building C, Room 301', '20',NULL), | ||
('Building C, Room 302', '15',NULL), | ||
('Building D, Room 401', '30',NULL), | ||
('Building D, Room 402', '25',NULL), | ||
('Building E, Room 501', '40',NULL), | ||
('Building E, Room 502', '35',NULL); | ||
|
||
INSERT INTO Class (class_name, prof_id, course_id, room_id) | ||
VALUES | ||
('Class 1', 1, 1, 1), | ||
('Class 2', 2, 2, 2), | ||
('Class 3', 3, 3, 3), | ||
('Class 4', 4, 4, 4), | ||
('Class 5', 5, 5, 5), | ||
('Class 6', 6, 6, 6), | ||
('Class 7', 7, 7, 7), | ||
('Class 8', 8, 8, 8), | ||
('Class 9', 9, 9, 9), | ||
('Class 10',10,10,10); | ||
|
||
INSERT INTO Enroll (stud_id, class_id, grade) | ||
VALUES | ||
(1, 1, 'A'), | ||
(2, 2, 'B'), | ||
(3, 3, 'C'), | ||
(4, 4, 'A'), | ||
(5, 5, 'B'), | ||
(6, 6, 'C'), | ||
(7, 7, 'A'), | ||
(8, 8, 'B'), | ||
(9, 9, 'C'), | ||
(10, 10, 'A'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
version: "3.8" | ||
services: | ||
db: | ||
image: mysql:8.0 | ||
cap_add: | ||
- SYS_NICE | ||
restart: always | ||
environment: | ||
- MYSQL_DATABASE=root | ||
- MYSQL_ROOT_PASSWORD=root | ||
ports: | ||
- "3306:3306" | ||
volumes: | ||
- db:/var/lib/mysql | ||
- ./db/init.sql:/docker-entrypoint-initdb.d/init.sql | ||
volumes: | ||
db: | ||
driver: local |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
-- student, professor, class with class in common | ||
SELECT | ||
CONCAT(pr.prof_fname, '', pr.prof_lname) AS professor, | ||
CONCAT(st.stud_fname, '', st.stud_lname) AS student, | ||
cl.class_name | ||
FROM | ||
Professor pr | ||
JOIN Class cl ON pr.prof_id = cl.prof_id | ||
JOIN Enroll en ON cl.class_id = en.class_id | ||
JOIN Student st ON st.stud_id = en.stud_id; | ||
|
||
-- courses that 1 professor is teaching | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this question mean that, we already have prof_id as input |
||
SELECT DISTINCT | ||
c.course_name, | ||
CONCAT(pr.prof_fname, '', pr.prof_lname) AS professor | ||
FROM | ||
Professor pr | ||
JOIN Class cl USING (prof_id) | ||
JOIN Course c USING (course_id) | ||
|
||
-- courses that 1 student is enrolling | ||
SELECT DISTINCT | ||
c.course_name, | ||
CONCAT(st.stud_fname, '', st.stud_lname) AS student | ||
FROM | ||
Student st | ||
JOIN Enroll e USING (stud_id) | ||
JOIN Class cl USING (class_id) | ||
JOIN Course c USING (course_id); | ||
|
||
|
||
-- match grade in number with CHARACTER | ||
DROP VIEW IF EXISTS grade_in_number; | ||
CREATE VIEW grade_in_number AS | ||
SELECT | ||
( | ||
CASE WHEN grade = 'A' THEN | ||
10 | ||
WHEN grade = 'B' THEN | ||
8 | ||
WHEN grade = 'C' THEN | ||
6 | ||
WHEN grade = 'D' THEN | ||
4 | ||
WHEN grade = 'E' THEN | ||
2 | ||
WHEN grade = 'F' THEN | ||
0 | ||
END) AS grade_number, stud_id, class_id, grade | ||
FROM | ||
Enroll; | ||
|
||
SELECT * | ||
FROM grade_in_number | ||
|
||
SELECT st.stud_fname, gd.grade_number, e.class_id | ||
FROM | ||
Student st | ||
JOIN Enroll e USING (stud_id) | ||
JOIN grade_in_number gd USING (stud_id) | ||
|
||
-- avg grade in CHARACTER of 1 student | ||
SELECT | ||
CONCAT(stud_fname, " ", stud_lname) as stud_name, | ||
ROUND(AVG( | ||
CASE | ||
WHEN grade = 'A' THEN 10 | ||
WHEN grade = 'B' THEN 8 | ||
WHEN grade = 'C' THEN 6 | ||
WHEN grade = 'D' THEN 4 | ||
WHEN grade = 'E' THEN 2 | ||
WHEN grade = 'F' THEN 0 | ||
END | ||
), 2) as academic_ability | ||
FROM Enroll en | ||
JOIN Student st | ||
ON st.stud_id = en.stud_id | ||
GROUP BY CONCAT(stud_fname, " ", stud_lname); | ||
|
||
-- avg grade of a class | ||
SELECT | ||
class_name, | ||
ROUND(AVG( | ||
CASE WHEN grade = 'A' THEN | ||
10 | ||
WHEN grade = 'B' THEN | ||
8 | ||
WHEN grade = 'C' THEN | ||
6 | ||
WHEN grade = 'D' THEN | ||
4 | ||
WHEN grade = 'E' THEN | ||
2 | ||
WHEN grade = 'F' THEN | ||
0 | ||
END), 2) AS grade | ||
FROM | ||
Enroll en | ||
JOIN Class cl USING (class_id) | ||
GROUP BY | ||
class_name; | ||
|
||
-- avg grade of course | ||
SELECT | ||
cr.course_name, | ||
ROUND(AVG( | ||
CASE WHEN grade = 'A' THEN | ||
10 | ||
WHEN grade = 'B' THEN | ||
8 | ||
WHEN grade = 'C' THEN | ||
6 | ||
WHEN grade = 'D' THEN | ||
4 | ||
WHEN grade = 'E' THEN | ||
2 | ||
WHEN grade = 'F' THEN | ||
0 | ||
END), 2) AS grade | ||
FROM | ||
Enroll en | ||
JOIN Class cl USING (class_id) | ||
JOIN Course cr USING (course_id) | ||
GROUP BY | ||
course_name; | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we want number of courses which a pair of student/ teacher is joining