Join All The Things
The following questions are related to creating basic data tables that Danny and his team can use to quickly derive insights without needing to join the underlying tables using SQL.
Recreate the following table output using the available data:
customer_id | order_date | product_name | price | member |
---|---|---|---|---|
A | 2021-01-01 | curry | 15 | N |
A | 2021-01-01 | sushi | 10 | N |
A | 2021-01-07 | curry | 15 | Y |
A | 2021-01-10 | ramen | 12 | Y |
A | 2021-01-11 | ramen | 12 | Y |
A | 2021-01-11 | ramen | 12 | Y |
B | 2021-01-01 | curry | 15 | N |
B | 2021-01-02 | curry | 15 | N |
B | 2021-01-04 | sushi | 10 | N |
B | 2021-01-11 | sushi | 10 | Y |
B | 2021-01-16 | ramen | 12 | Y |
B | 2021-02-01 | ramen | 12 | Y |
C | 2021-01-01 | ramen | 12 | N |
C | 2021-01-01 | ramen | 12 | N |
C | 2021-01-07 | ramen | 12 | N |
Solution
SELECT sales.customer_id,
order_date,
product_name,
price,
(
CASE join_date IS NULL
WHEN TRUE THEN 'N'
ELSE
(
CASE order_date >= join_date
WHEN TRUE THEN 'Y'
ELSE 'N'
END
)
END
) member
FROM sales
LEFT OUTER JOIN members m ON sales.customer_id = m.customer_id
INNER JOIN menu m2 ON sales.product_id = m2.product_id
ORDER BY customer_id, order_date, product_name;
customer_id | order_date | product_name | price | member |
---|---|---|---|---|
A | 2021-01-01 | curry | 15 | N |
A | 2021-01-01 | sushi | 10 | N |
A | 2021-01-07 | curry | 15 | Y |
A | 2021-01-10 | ramen | 12 | Y |
A | 2021-01-11 | ramen | 12 | Y |
A | 2021-01-11 | ramen | 12 | Y |
B | 2021-01-01 | curry | 15 | N |
B | 2021-01-02 | curry | 15 | N |
B | 2021-01-04 | sushi | 10 | N |
B | 2021-01-11 | sushi | 10 | Y |
B | 2021-01-16 | ramen | 12 | Y |
B | 2021-02-01 | ramen | 12 | Y |
C | 2021-01-01 | ramen | 12 | N |
C | 2021-01-01 | ramen | 12 | N |
C | 2021-01-07 | ramen | 12 | N |
Walkthrough
Join all the tables, ensuring a LEFT OUTER JOIN
from sales
to members
on customer_id
so that
even those customers that have never become a member show up in the table.
Additionally, CASE
out the order_date
and return Y
if the customer was a member on the day of the order, and N
otherwise. Finally, order the rows by (customer_id, order_date, product_name)
.
SELECT sales.customer_id,
order_date,
product_name,
price,
(
CASE join_date IS NULL
WHEN TRUE THEN 'N'
ELSE
(
CASE order_date >= join_date
WHEN TRUE THEN 'Y'
ELSE 'N'
END
)
END
) member
FROM sales
LEFT OUTER JOIN members m ON sales.customer_id = m.customer_id
INNER JOIN menu m2 ON sales.product_id = m2.product_id
ORDER BY customer_id, order_date, product_name;
customer_id | order_date | product_name | price | member |
---|---|---|---|---|
A | 2021-01-01 | curry | 15 | N |
A | 2021-01-01 | sushi | 10 | N |
A | 2021-01-07 | curry | 15 | Y |
A | 2021-01-10 | ramen | 12 | Y |
A | 2021-01-11 | ramen | 12 | Y |
A | 2021-01-11 | ramen | 12 | Y |
B | 2021-01-01 | curry | 15 | N |
B | 2021-01-02 | curry | 15 | N |
B | 2021-01-04 | sushi | 10 | N |
B | 2021-01-11 | sushi | 10 | Y |
B | 2021-01-16 | ramen | 12 | Y |
B | 2021-02-01 | ramen | 12 | Y |
C | 2021-01-01 | ramen | 12 | N |
C | 2021-01-01 | ramen | 12 | N |
C | 2021-01-07 | ramen | 12 | N |