diff --git a/sol-1.sql b/sol-1.sql new file mode 100644 index 0000000..f27db68 --- /dev/null +++ b/sol-1.sql @@ -0,0 +1,20 @@ +''' +Bai 1 +''' +select stock_name, sum( +case + when operation = 'buy' then -price + when operation = 'sell' then price +end +) as capital_gain_loss +from Stocks +group by stock_name + +''' +Bai 2 +''' +SELECT 'Low Salary' AS category , COUNT(*) AS accounts_count FROM accounts WHERE income<20000 +UNION +SELECT 'Average Salary' AS category , COUNT(*) AS accounts_count FROM accounts WHERE income BETWEEN 20000 and 50000 +UNION +SELECT 'High Salary' AS category , COUNT(*) AS accounts_count FROM accounts WHERE income>50000 ; diff --git a/sol-2.sql b/sol-2.sql new file mode 100644 index 0000000..cef0e4c --- /dev/null +++ b/sol-2.sql @@ -0,0 +1,50 @@ +create table PROFESSOR ( + PROF_ID int primary key, + PROF_LNAME varchar(50), + PROF_FNAME varchar(50) +) + +create table STUDENT ( + STUD_ID int primary key, + STUD_LNAME varchar(50), + STUD_FNAME varchar(50), + STUD_STREET varchar(255), + STUD_CITY varchar(50), + STUD_ZIP varchar(10) +) + +create table CLASS ( + CLASS_ID int primary key, + CLASS_NAME varchar(255),, + PROF_ID int, + COURSE_ID int, + ROOM_ID int, + foreign key (PROF_ID) references PROFESSOR(PROF_ID), + foreign key (COURSE_ID) references COURSE(COURSE_ID), + foreign key (ROOM_ID) references ROOM(ROOM_ID) +) + +create table ENROLL( + STUD_ID int, + CLASS_ID int, + GRADE varchar(3), + foreign key (STUD_ID) references STUDENT(STUD_ID), + foreign key (CLASS_ID) references CLASS(CLASS_ID), + PRIMARY KEY (STUD_ID, CLASS_ID) +) + + +create table COURSE ( + COURSE_ID int primary key, + COURSE_NAME varchar(255) +) + +create table ROOM ( + ROOM_ID int primary key, + ROOM_LOC varchar(50), + ROOM_CAP varchar(50), + CLASS_ID int +) + +ALTER TABLE ROOM +ADD CONSTRAINT FK_ROOM_CLASS FOREIGN KEY (CLASS_ID) REFERENCES CLASS(CLASS_ID) \ No newline at end of file diff --git a/sol-3.sql b/sol-3.sql new file mode 100644 index 0000000..edb6eb3 --- /dev/null +++ b/sol-3.sql @@ -0,0 +1,185 @@ +''' +Hãy viết câu query để tìm: +những cặp student-professor có dạy học nhau và số lớp mà họ có liên quan +những course (distinct) mà 1 professor cụ thể đang dạy +những course (distinct) mà 1 student cụ thể đang học +đ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 ) +điểm số trung bình của các class (quy ra lại theo chữ cái) +điểm số trung bình của các course (quy ra lại theo chữ cái) +''' + +--những cặp student-professor có dạy học nhau và số lớp mà họ có liên quan +select pr.PROF_ID, st.STUD_ID, cl.CLASS_ID +from PROFESSOR pr inner join CLASS cl on pr.PROF_ID = cl.PROF_ID + inner join ENROLL en on cl.CLASS_ID = en.CLASS_ID + inner join STUDENT st on en.STUD_ID = st.STUD_ID + +--những course (distinct) mà 1 professor cụ thể đang dạy. VD professor có id = 1 +select distinct co.COURSE_NAME +from PROFESSOR pr inner join CLASS cl on pr.PROF_ID = cl.PROF_ID + inner join COURSE co on cl.COURSE_ID = co.COURSE_ID +where pr.PROF_ID = 1 + +--những course (distinct) mà 1 student cụ thể đang học. VD student có id = 1 +select distinct co.COURSE_NAME +from STUDENT st inner join ENROLL en on st.STUD_ID = en.STUD_ID + inner join CLASS cl on en.CLASS_ID = cl.CLASS_ID + inner join COURSE co on cl.COURSE_ID = co.COURSE_ID +where st.STUD_ID = 1 + +--điểm số là A, B, C, D, E, F tương đương với 10, 8, 6, 4, 2, 0 +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 +from STUDENT st inner join ENROLL en on st.STUD_ID = en.STUD_ID + +--đ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 ( + case + when 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 + ) < 5 then 'weak' + when 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 + ) >= 5 and 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 + ) < 8 then 'average' + when 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 + ) >= 8 then 'good' + end +) as GRADE +from STUDENT st inner join ENROLL en on st.STUD_ID = en.STUD_ID +group by st.STUD_ID + +--điểm số trung bình của các class (quy ra lại theo chữ cái) +select cl.CLASS_ID, ( + case + when 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 + ) < 5 then 'weak' + when 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 + ) >= 5 and 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 + ) < 8 then 'average' + when 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 + ) >= 8 then 'good' + end +) as GRADE +from CLASS cl inner join ENROLL en on cl.CLASS_ID = en.CLASS_ID +group by cl.CLASS_ID + +--điểm số trung bình của các course (quy ra lại theo chữ cái) + +select co.COURSE_NAME, ( + case + when 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 + ) < 5 then 'weak' + when 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 + ) >= 5 and 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 + ) < 8 then 'average' + when 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 + ) >= 8 then 'good' + end +) as GRADE +from COURSE co inner join CLASS cl on co.COURSE_ID = cl.COURSE_ID + inner join ENROLL en on cl.CLASS_ID = en.CLASS_ID +group by co.COURSE_ID \ No newline at end of file