สร้างตารางชั่วคราว
ค้นหาบริษัทใน Fortune 500 ที่มีกำไรอยู่ใน 20% บนสุดของแต่ละภาคธุรกิจ (เมื่อเทียบกับบริษัท Fortune 500 อื่น ๆ)
เริ่มต้นด้วยการหาเปอร์เซ็นไทล์ที่ 80 ของกำไรในแต่ละภาคธุรกิจโดยใช้
percentile_disc(fraction)
WITHIN GROUP (ORDER BY sort_expression)
แล้วบันทึกผลลัพธ์ไว้ในตารางชั่วคราว
จากนั้น JOIN fortune500 กับตารางชั่วคราวเพื่อเลือกบริษัทที่มีกำไรสูงกว่าค่าตัดที่เปอร์เซ็นไทล์ที่ 80
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Exploratory Data Analysis ใน SQL
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
-- To clear table if it already exists; fill in name of temp table
DROP TABLE IF EXISTS ___;
-- Create the temporary table
___ ___ ___ ___ AS
-- Select the two columns you need; alias as needed
SELECT ___,
___(___) ___ (___) AS ___
-- What table are you getting the data from?
___ ___
-- What do you need to group by?
___ ___ ___;
-- See what you created: select all columns and rows from the table you created
SELECT *
FROM ___;