Skip to content
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] Nguyen Le Thien An #2

Open
wants to merge 1 commit into
base: database-assignment-1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions sol-1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-- Leetcode Problems

-- https://leetcode.com/problems/capital-gainloss/description/
select stock_name, sum(case when operation = "Buy" then -price
else +price end)
as capital_gain_loss
from Stocks
group by stock_name;

-- https://leetcode.com/problems/count-salary-categories/description/
select t.category, count(a.category) as accounts_count
from (
select 'Low Salary' as category
union
select 'Average Salary' as category
union
select 'High Salary' as category
) as t
left outer join (
select case when income < 20000 then 'Low Salary'
when income > 50000 then 'High Salary'
else 'Average Salary' end as category
from Accounts
) as a on a.category = t.category
group by t.category;

58 changes: 58 additions & 0 deletions sol-2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
create database be05;

create table be05.professor(
prof_id int not null,
prof_lname varchar(50),
prof_fname varchar(50),
primary key (prof_id)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should add auto increase primary id ( and for all tables too)

);

create table be05.student(
stud_id int not null,
stud_fname varchar(50),
stud_lname varchar(50),
stud_street varchar(255),
stud_city varchar(50),
stud_zip varchar(10),
primary key (stud_id)
);

create table be05.course(
course_id int not null,
course_name varchar(255),
primary key(course_id)
);

create table be05.room(
room_id int not null,
room_loc varchar(50),
room_cap varchar(50),
class_id int,
primary key (room_id)
-- foreign key (class_id) references class(class_id)
);

create table be05.class(
class_id int not null,
class_name varchar(255),
prof_id int,
course_id int,
room_id int,
primary key (class_id),
foreign key (prof_id) references professor(prof_id),
foreign key (course_id) references course(course_id),
foreign key (room_id) references room(room_id)
);

alter table be05.room
add foreign key (class_id) references class(class_id);

create table be05.enroll(
stud_id int not null,
class_id int not null,
grade varchar(3),
primary key (stud_id, class_id),
foreign key (stud_id) references student(stud_id),
foreign key (class_id) references class(class_id)
);

95 changes: 95 additions & 0 deletions sol-3.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
-- những cặp student-professor có dạy học nhau và số lớp mà họ có liên quan
select p.prof_id, e.stud_id, count(*) as 'no_of_class'
from professor as p join class as c on p.prof_id = c.prof_id
join enroll as e on e.class_id
group by p.prof_id, e.stud_id;

-- những course (distinct) mà 1 professor cụ thể đang dạy
select distinct c.course_id, c.course_name, p.prof_id, p.prof_fname, p.prof_lname
from professor as p join class as cl on p.prof_id = cl.prof_id
join course as c on cl.course_id = c.course_id;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this question meaning that, we already have prof_id, and find all distinct course that he teach


-- những course (distinct) mà 1 student cụ thể đang học
select distinct c.course_id, c.course_name, s.stud_id, s.stud_fname, s.stud_lname
from student as s join enroll as e on s.stud_id = e.stud_id
join class as cl on e.class_id = cl.class_id
join course as c on c.course_id = cl.course_id;

-- điểm số là A, B, C, D, E, F tương đương với 10, 8, 6, 4, 2, 0

-- điểm số trung bình của 1 học sinh cụ thể (quy ra lại theo chữ cái, và xếp loại học lực (weak nếu avg < 5, average nếu >=5 < 8, good nếu >=8 )
select t.stud_id, t.stud_fname, t.stud_lname,
case when t.avg_grade_num < 5 then 'Weak'
when t.avg_grade_num >= 8 then 'Good'
else 'Average'
end as classification,
case when t.avg_grade_num = 10 then 'A'
when t.avg_grade_num = 8 then 'B'
when t.avg_grade_num = 6 then 'C'
when t.avg_grade_num = 4 then 'D'
when t.avg_grade_num = 2 then 'E'
when t.avg_grade_num = 0 then 'F'
end as avg_grade_letter
from (
select s.stud_id, s.stud_fname, s.stud_lname, avg(e.grade_num) as 'avg_grade_num'
from (
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_num
from enroll
) as e join student as s on s.stud_id = e.stud_id
group by s.stud_id
) as t;

-- điểm số trung bình của các class (quy ra lại theo chữ cái)
select t.class_id, t.class_name,
case when t.avg_grade_num = 10 then 'A'
when t.avg_grade_num = 8 then 'B'
when t.avg_grade_num = 6 then 'C'
when t.avg_grade_num = 4 then 'D'
when t.avg_grade_num = 2 then 'E'
when t.avg_grade_num = 0 then 'F'
end as avg_grade_letter
from (
select cl.class_id, cl.class_name, avg(e.grade_num) as 'avg_grade_num'
from (
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_num
from enroll
) as e join class as cl on cl.class_id = e.class_id
group by cl.class_id
) as t;

-- điểm số trung bình của các course (quy ra lại theo chữ cái)
select t.course_id, t.course_name,
case when t.avg_grade_num = 10 then 'A'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should use >= , <= ... because avg may return float number

when t.avg_grade_num = 8 then 'B'
when t.avg_grade_num = 6 then 'C'
when t.avg_grade_num = 4 then 'D'
when t.avg_grade_num = 2 then 'E'
when t.avg_grade_num = 0 then 'F'
end as avg_grade_letter
from (
select c.course_id, c.course_name, avg(e.grade_num) as 'avg_grade_num'
from (
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_num
from enroll
) as e join class as cl on cl.class_id = e.class_id
join course as c on c.course_id = cl.course_id
group by c.course_id
) as t