-
Notifications
You must be signed in to change notification settings - Fork 30
[ThaiNgocThanhDat] database-assignment-1 #13
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
Open
unitoflazy
wants to merge
1
commit into
EngineerProOrg:database-assignment-1
Choose a base branch
from
unitoflazy: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 hidden or 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,75 @@ | ||
| create table if not exists professor ( | ||
| prof_id integer primary key, | ||
| prof_lname varchar(50), | ||
| prof_fname varchar(50) | ||
| ); | ||
|
|
||
| create table if not exists student ( | ||
| stud_id integer primary key, | ||
| stud_lname varchar(50), | ||
| stud_fname varchar(50), | ||
| stud_street varchar(255), | ||
| stud_city varchar(50), | ||
| stud_zip varchar(10) | ||
| ); | ||
|
|
||
| create table if not exists enroll ( | ||
| stud_id integer, | ||
| class_id integer, | ||
| grade enum ('A', 'B', 'C', 'D', 'F') | ||
| ); | ||
|
|
||
| create table if not exists class ( | ||
| class_id integer primary key, | ||
| class_name varchar(255), | ||
| prof_id integer, | ||
| course_id integer, | ||
| room_id integer | ||
| ); | ||
|
|
||
| create table if not exists room ( | ||
| room_id integer primary key, | ||
| room_loc varchar(50), | ||
| room_cap varchar(50), | ||
| class_id integer | ||
| ); | ||
|
|
||
| create table if not exists course ( | ||
| course_id integer primary key, | ||
| course_name varchar(255) | ||
| ); | ||
|
|
||
| alter table | ||
| enroll | ||
| add | ||
| constraint fk_student_enroll foreign key (stud_id) references student(stud_id); | ||
|
|
||
| alter table | ||
| enroll | ||
| add | ||
| constraint fk_class_enroll foreign key (class_id) references class(class_id); | ||
|
|
||
| alter table | ||
| enroll | ||
| add | ||
| primary key (stud_id, class_id); | ||
|
|
||
| alter table | ||
| class | ||
| add | ||
| constraint fk_prof_class foreign key (prof_id) references professor(prof_id); | ||
|
|
||
| alter table | ||
| class | ||
| add | ||
| constraint fk_course_class foreign key (course_id) references course(course_id); | ||
|
|
||
| alter table | ||
| class | ||
| add | ||
| constraint fk_room_class foreign key (room_id) references room(room_id); | ||
|
|
||
| alter table | ||
| room | ||
| add | ||
| constraint fk_class_room foreign key (class_id) references class(class_id) |
This file contains hidden or 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,138 @@ | ||
| ## 1 | ||
| ## những cặp student-professor có dạy học nhau và số lớp mà họ có liên quan | ||
| select | ||
| concat(p.prof_fname, ' ', p.prof_lname) prof_name, | ||
| concat(s.stud_fname, ' ', s.stud_lname) stud_name, | ||
| count(*) number_of_classes | ||
| from | ||
| student s | ||
| join enroll e on s.stud_id = e.stud_id | ||
| join class c on c.class_id = e.class_id | ||
| join professor p on p.prof_id = c.prof_id | ||
| group by | ||
| prof_name, | ||
| stud_name; | ||
|
|
||
| ## 2 | ||
| ## những course (distinct) mà 1 professor cụ thể đang dạy | ||
| select | ||
| distinct * | ||
| from | ||
| course | ||
| join class c on course.course_id = c.course_id | ||
| join professor p on c.prof_id = p.prof_id | ||
| where | ||
| p.prof_id = 1; | ||
|
|
||
| ## 3 | ||
| ## những course (distinct) mà 1 student cụ thể đang học | ||
| select | ||
| distinct course.course_id, | ||
| course_name | ||
| from | ||
| course | ||
| join class c on course.course_id = c.course_id | ||
| join enroll e on c.class_id = e.class_id | ||
| join student s on e.stud_id = s.stud_id | ||
| where | ||
| e.stud_id = 5; | ||
|
|
||
| ## 4 | ||
| ## điểm số trung bình của 1 học sinh cụ thể, quy ra lại theo chữ cái và học lực | ||
| select | ||
| case | ||
| when avg(score) >= 9 then 'A' | ||
| when avg(score) >= 7 then 'B' | ||
| when avg(score) >= 5 then 'C' | ||
| when avg(score) >= 3 then 'D' | ||
| else 'F' | ||
| end as avg_score, | ||
| case | ||
| when avg(score) >= 8 then 'GOOD' | ||
| when avg(score) < 5 then 'WEAK' | ||
| else 'AVERAGE' | ||
| end as ranked | ||
| from | ||
| enroll e1 | ||
| join ( | ||
| select | ||
| stud_id, | ||
| 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 score | ||
| from | ||
| enroll | ||
| ) e2 on e1.stud_id = e2.stud_id | ||
| where | ||
| e1.stud_id = 2 | ||
| group by | ||
| e1.stud_id; | ||
|
|
||
| ## 5 | ||
| ## điểm số trung bình của các class (quy ra lại theo chữ cái) | ||
| select | ||
| e1.class_id class_id, | ||
| case | ||
| when avg(score) >= 9 then 'A' | ||
| when avg(score) >= 7 then 'B' | ||
| when avg(score) >= 5 then 'C' | ||
| when avg(score) >= 3 then 'D' | ||
| else 'F' | ||
| end as avg_score | ||
| from | ||
| enroll e1 | ||
| join ( | ||
| select | ||
| class_id, | ||
| 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 score | ||
| from | ||
| enroll | ||
| ) e2 on e1.class_id = e2.class_id | ||
| group by | ||
| e1.class_id; | ||
|
|
||
| ## 6 | ||
| ## điểm số trung bình của các course (quy ra lại theo chữ cái) | ||
| select | ||
| cre.course_id, | ||
| cre.course_name, | ||
| case | ||
| when avg(score) >= 9 then 'A' | ||
| when avg(score) >= 7 then 'B' | ||
| when avg(score) >= 5 then 'C' | ||
| when avg(score) >= 3 then 'D' | ||
| else 'F' | ||
| end as avg_score | ||
| from | ||
| enroll e1 | ||
| join ( | ||
| select | ||
| class_id, | ||
| 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 score | ||
| from | ||
| enroll | ||
| ) e2 on e1.class_id = e2.class_id | ||
| join class c on c.class_id = e1.class_id | ||
| join course cre on cre.course_id = c.course_id | ||
| group by | ||
| cre.course_id, | ||
| cre.course_name | ||
This file contains hidden or 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,158 @@ | ||
| # My data examples for the assignment | ||
| # Use at your own risk | ||
| INSERT INTO | ||
| professor (prof_id, prof_lname, prof_fname) | ||
| VALUES | ||
| (1, 'Smith', 'John'), | ||
| (2, 'Doe', 'Jane'), | ||
| (3, 'Johnson', 'Michael'), | ||
| (4, 'Williams', 'Emily'), | ||
| (5, 'Jones', 'David'), | ||
| (6, 'Garcia', 'Sophia'); | ||
|
|
||
| INSERT INTO | ||
| student ( | ||
| stud_id, | ||
| stud_lname, | ||
| stud_fname, | ||
| stud_street, | ||
| stud_city, | ||
| stud_zip | ||
| ) | ||
| VALUES | ||
| ( | ||
| 1, | ||
| 'Brown', | ||
| 'Alice', | ||
| '123 Main St', | ||
| 'Springfield', | ||
| '12345' | ||
| ), | ||
| ( | ||
| 2, | ||
| 'Davis', | ||
| 'Bob', | ||
| '456 Elm St', | ||
| 'Shelbyville', | ||
| '54321' | ||
| ), | ||
| ( | ||
| 3, | ||
| 'Miller', | ||
| 'Charlie', | ||
| '789 Oak St', | ||
| 'Capital City', | ||
| '67890' | ||
| ), | ||
| ( | ||
| 4, | ||
| 'Rodriguez', | ||
| 'Daniel', | ||
| '246 Maple St', | ||
| 'Ogdenville', | ||
| '24680' | ||
| ), | ||
| ( | ||
| 5, | ||
| 'Wilson', | ||
| 'Emma', | ||
| '369 Pine St', | ||
| 'North Haverbrook', | ||
| '36912' | ||
| ), | ||
| ( | ||
| 6, | ||
| 'Martinez', | ||
| 'Olivia', | ||
| '159 Cedar St', | ||
| 'Brockway', | ||
| '15975' | ||
| ); | ||
|
|
||
| INSERT INTO | ||
| class (class_id, class_name, prof_id, course_id) | ||
| VALUES | ||
| (1, 'Intro to Computer Science', 1, 1), | ||
| (2, 'Calculus I', 2, 2), | ||
| (3, 'English Literature', 3, 3), | ||
| (4, 'Chemistry I', 4, 4); | ||
|
|
||
| INSERT INTO | ||
| room (room_id, room_loc, room_cap) | ||
| VALUES | ||
| (101, 'Building A', '30'), | ||
| (102, 'Building B', '40'), | ||
| (103, 'Building C', '35'), | ||
| (104, 'Building D', '45'); | ||
|
|
||
| UPDATE | ||
| room | ||
| set | ||
| class_id = 1 | ||
| where | ||
| room_id = 101; | ||
|
|
||
| UPDATE | ||
| room | ||
| set | ||
| class_id = 2 | ||
| where | ||
| room_id = 102; | ||
|
|
||
| UPDATE | ||
| room | ||
| set | ||
| class_id = 3 | ||
| where | ||
| room_id = 103; | ||
|
|
||
| UPDATE | ||
| room | ||
| set | ||
| class_id = 4 | ||
| where | ||
| room_id = 104; | ||
|
|
||
| INSERT INTO | ||
| course (course_id, course_name) | ||
| VALUES | ||
| (1, 'Computer Science'), | ||
| (2, 'Mathematics'), | ||
| (3, 'English'), | ||
| (4, 'Chemistry'); | ||
|
|
||
| INSERT INTO | ||
| enroll (stud_id, class_id, grade) | ||
| VALUES | ||
| (1, 1, 'A'), | ||
| (1, 2, 'B'), | ||
| (2, 1, 'C'), | ||
| (3, 2, 'A'), | ||
| (4, 1, 'B'), | ||
| (4, 2, 'A'), | ||
| (6, 2, 'C'), | ||
| (1, 3, 'A'), | ||
| (1, 4, 'B'), | ||
| (2, 3, 'C'), | ||
| (2, 4, 'A'), | ||
| (5, 1, 'C'), | ||
| (5, 2, 'A'), | ||
| (5, 6, 'A'); | ||
|
|
||
| INSERT INTO | ||
| room (room_id, room_loc, room_cap) | ||
| VALUES | ||
| (105, 'Building E', '50'), | ||
| (106, 'Building F', '55'); | ||
|
|
||
| INSERT INTO | ||
| class ( | ||
| class_id, | ||
| class_name, | ||
| prof_id, | ||
| course_id, | ||
| room_id | ||
| ) | ||
| VALUES | ||
| (5, 'History of Art', 5, 5, 105), | ||
| (6, 'Physics I', 6, 6, 106); |
This file contains hidden or 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 @@ | ||
| select | ||
| stock_name, | ||
| sum(if(operation = 'Sell', price, - price)) as capital_gain_loss | ||
| from | ||
| stocks | ||
| group by | ||
| stock_name |
Oops, something went wrong.
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.
because we know stud_id, why dont add it into the sub query ? so the number of rows need to be join can be reduced.