From 5a118e3892799f272b4e62486bc9cf1be948b60d Mon Sep 17 00:00:00 2001 From: Ben Brumm Date: Wed, 25 Sep 2024 14:26:56 +1000 Subject: [PATCH] Added module 7 --- .../exercises/exercises_module_7.sql | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 courses/course_postgres_mastery/exercises/exercises_module_7.sql diff --git a/courses/course_postgres_mastery/exercises/exercises_module_7.sql b/courses/course_postgres_mastery/exercises/exercises_module_7.sql new file mode 100644 index 0000000..58ef8fc --- /dev/null +++ b/courses/course_postgres_mastery/exercises/exercises_module_7.sql @@ -0,0 +1,42 @@ +/* +Exercises from the PostgreSQL Mastery course +Module 7 +*/ + +/* 49-01 */ + +SELECT +c.customer_id, +c.first_name, +c.last_name, +c.email, +s.order_id, +s.order_date +FROM customer c +JOIN LATERAL ( + SELECT co.* + FROM cust_order co + WHERE co.customer_id = c.customer_id + ORDER BY co.order_date ASC + FETCH FIRST 3 ROWS ONLY +) s ON TRUE +ORDER BY c.customer_id ASC; + +/* 51-01 */ + +SELECT +c.customer_id, +c.first_name, +c.last_name, +c.email, +s.order_id, +s.order_date +FROM customer c +LEFT JOIN LATERAL ( + SELECT co.* + FROM cust_order co + WHERE co.customer_id = c.customer_id + ORDER BY co.order_date ASC + FETCH FIRST 3 ROWS ONLY +) s ON TRUE +ORDER BY c.customer_id ASC; \ No newline at end of file