INSERTINTO "CREDIT_PURCHASE"( user_id, credit_package_id, purchased_credits, price_paid ) VALUES( ( SELECT id FROM "USER" WHERE name ='王小明' ), ( SELECT id FROM "CREDIT_PACKAGE" WHERE name ='14 堂組合包方案' ), ( SELECT credit_amount FROM "CREDIT_PACKAGE" WHERE name ='14 堂組合包方案' ), ( SELECT price FROM "CREDIT_PACKAGE" WHERE name ='14 堂組合包方案' ) ), ( ( SELECT id FROM "USER" WHERE name ='王小明' ), ( SELECT id FROM "CREDIT_PACKAGE" WHERE name ='21 堂組合包方案' ), ( SELECT credit_amount FROM "CREDIT_PACKAGE" WHERE name ='21 堂組合包方案' ), ( SELECT price FROM "CREDIT_PACKAGE" WHERE name ='21 堂組合包方案' ) ), ( ( SELECT id FROM "USER" WHERE name ='好野人' ), ( SELECT id FROM "CREDIT_PACKAGE" WHERE name ='14 堂組合包方案' ), ( SELECT credit_amount FROM "CREDIT_PACKAGE" WHERE name ='14 堂組合包方案' ), ( SELECT price FROM "CREDIT_PACKAGE" WHERE name ='14 堂組合包方案' ) );
INSERTINTO "COACH"(user_id, experience_years) VALUES ( ( SELECT id FROM "USER" WHERE email ='lee2000@hexschooltest.io' ), 2 ), ( ( SELECT id FROM "USER" WHERE email ='muscle@hexschooltest.io' ), 2 ), ( ( SELECT id FROM "USER" WHERE email ='starplatinum@hexschooltest.io' ), 2 );
-- 建立資料表 CREATETABLE student ( id serial PRIMARY KEY, name varchar(50) NOTNULL, class varchar(20) NOTNULL, created_at timestampNOTNULLDEFAULTCURRENT_TIMESTAMP );
CREATETABLE subject ( id serial PRIMARY KEY, name varchar(50) NOTNULL, created_at timestampNOTNULLDEFAULTCURRENT_TIMESTAMP );
SELECT student.name, exam_score.score FROM exam_score INNERJOIN student ON exam_score.student_id = student.id INNERJOIN subject ON exam_score.subject_id = subject.id WHERE student.name ='小明' AND subject.name ='國文';
-- 建立資料表 CREATETABLE student ( id serial PRIMARY KEY, name varchar(50) NOTNULL, class varchar(20) NOTNULL, created_at timestampNOTNULLDEFAULTCURRENT_TIMESTAMP );
CREATETABLE subject ( id serial PRIMARY KEY, name varchar(50) NOTNULL, created_at timestampNOTNULLDEFAULTCURRENT_TIMESTAMP );
SELECT name, SUM(score) AS total FROM student INNERJOIN exam_score ON student.id = exam_score.student_id GROUPBY student.name;
Q2:計算每科的平均分數並依分數排序 A2:
1 2 3 4 5 6 7 8 9
SELECT subject.name, AVG(exam_score.score) AS average FROM exam_score INNERJOIN subject ON exam_score.subject_id = subject.id GROUPBY subject.name ORDERBY total DESC;
SELECT student.name, SUM(exam_score.score) AS student_total, ( SELECT AVG(total_table.total) FROM ( SELECT SUM(exam_score.score) AS total FROM exam_score GROUPBY exam_score.student_id ) AS total_table ) AS class_avg, SUM(exam_score.score) - ( SELECT AVG(total_table.total) FROM ( SELECT SUM(exam_score.score) AS total FROM exam_score GROUPBY exam_score.student_id ) AS total_table ) AS difference FROM exam_score INNERJOIN student ON exam_score.student_id = student.id GROUPBY student.name ORDERBY student_total DESC;
上面這邊也可以在 AS total_table 的部分使用 t 替代,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
SELECT student.name, SUM(exam_score.score) AS student_total, ( SELECT AVG(total_table.total) FROM ( SELECT SUM(exam_score.score) AS total FROM exam_score GROUPBY exam_score.student_id ) t ) AS class_avg, -- ...下略
最終作業
最終作業有兩種解題方式,一種是透過之前的 線上 DB 操作練習網站 先去匯入資料,然後進行問題的操作
update "COACH" set experience_years =3 where "COACH".id = ( select "sub_COACH".id from "USER" as "sub_USER" innerjoin "COACH" as "sub_COACH" on "sub_USER".id = "sub_COACH".user_id where "sub_USER".name ='肌肉棒子' );
update "COACH" set experience_years =5 where "COACH".id = ( select "sub_COACH".id from "USER" as "sub_USER" innerjoin "COACH" as "sub_COACH" on "sub_USER".id = "sub_COACH".user_id where "sub_USER".name ='Q太郎' );
其實可以透過 CASE 語句來進行一次的操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
update "COACH" set experience_years =case when "sub_USER".name ='肌肉棒子'then3 when "sub_USER".name ='Q太郎'then5 else experience_years end from "USER" as "sub_USER" innerjoin "COACH" as "sub_COACH" on "sub_USER".id = "sub_COACH".user_id where "sub_USER".name in ('肌肉棒子', 'Q太郎') and "COACH".id = "sub_COACH".id;
insert into "COURSE_BOOKING"(user_id, course_id, booking_at, status) values ( ( select id from "USER" where name ='王小明' ), ( select "COURSE".id from "COURSE" innerjoin "USER" on "COURSE".user_id = "USER".id where "USER".name ='李燕容' ), '2024-11-24 16:00:00', '即將授課' ), ( ( select id from "USER" where name ='好野人' ), ( select "COURSE".id from "COURSE" innerjoin "USER" on "COURSE".user_id = "USER".id where "USER".name ='李燕容' ), '2024-11-24 16:00:00', '即將授課' );
也可以透過以下方式簡寫
1 2 3 4 5 6 7 8 9 10 11 12 13
insertinto "COURSE_BOOKING" (user_id, course_id, booking_at, status) select "USER".id, "COURSE".id, '2024-11-24 16:00:00', '即將授課' from "USER" crossjoin "COURSE" innerjoin "USER" as "COACH_USER" on "COURSE".user_id = "COACH_USER".id where "USER".name in ('王小明', '好野人') and "COACH_USER".name ='李燕容';