1. Inner Join (3 points) Perform an inner join between the customers and orders datasets.

q1 <- inner_join(customers, orders, by = 'customer_id')

a) How many rows are in the result?

nrow(q1)
## [1] 4

b) Why are some customers or orders not included in the result?

They do not match in both ‘orders’ and ‘customers’ tables.

c) Display the result.

q1
## # A tibble: 4 × 6
##   customer_id name    city        order_id product amount
##         <dbl> <chr>   <chr>          <dbl> <chr>    <dbl>
## 1           1 Alice   New York         101 Laptop    1200
## 2           2 Bob     Los Angeles      102 Phone      800
## 3           2 Bob     Los Angeles      104 Desktop   1500
## 4           3 Charlie Chicago          103 Tablet     300

2.Left Join (3 points) Perform a left join with customers as the left table and orders as the right table.

q2 <- left_join(customers, orders, by = 'customer_id')

a) How many rows are in the result?

nrow(q2)
## [1] 6

b) Explain why this number differs from the inner join result.

‘q2’ is finding all values from ‘customers’ and returning matches in ‘orders’ based on the customer id’s found in ‘customers’, which only goes to 5 and has customer id = 2 twice.

c) Display the result.

q2
## # A tibble: 6 × 6
##   customer_id name    city        order_id product amount
##         <dbl> <chr>   <chr>          <dbl> <chr>    <dbl>
## 1           1 Alice   New York         101 Laptop    1200
## 2           2 Bob     Los Angeles      102 Phone      800
## 3           2 Bob     Los Angeles      104 Desktop   1500
## 4           3 Charlie Chicago          103 Tablet     300
## 5           4 David   Houston           NA <NA>        NA
## 6           5 Eve     Phoenix           NA <NA>        NA

3.Right Join (3 points) Perform a right join with customers as the left table and orders as the right table.

q3 <- right_join(customers, orders, by = 'customer_id')

a) How many rows are in the result?

nrow(q3)
## [1] 6

b) Which customer_ids in the result have NULL for customer name and city? Explain why.

This happens because in ‘orders’ there is are customer id’s = 6, 7; but in ‘customers’ those customer id’s do not return customer information and are not found.

c) Display the result.

q3
## # A tibble: 6 × 6
##   customer_id name    city        order_id product amount
##         <dbl> <chr>   <chr>          <dbl> <chr>    <dbl>
## 1           1 Alice   New York         101 Laptop    1200
## 2           2 Bob     Los Angeles      102 Phone      800
## 3           2 Bob     Los Angeles      104 Desktop   1500
## 4           3 Charlie Chicago          103 Tablet     300
## 5           6 <NA>    <NA>             105 Camera     600
## 6           7 <NA>    <NA>             106 Printer    150

4. Full Join (3 points) Perform a full join between customers and orders.

q4 <- full_join(customers, orders, by = 'customer_id')

a) How many rows are in the result?

nrow(q4)
## [1] 8

b) Identify any rows where there’s information from only one table. Explain these results.

Rows 5-8 return information from ‘customers’ (5-6) and ‘orders’ (7-8). For rows 5-6, there is no corresponding information for those customer id’s in ‘orders’, and data in rows 7-8 has no corresponding data in ‘customers’ for those customer_ids.

c) Display the result.

q4
## # A tibble: 8 × 6
##   customer_id name    city        order_id product amount
##         <dbl> <chr>   <chr>          <dbl> <chr>    <dbl>
## 1           1 Alice   New York         101 Laptop    1200
## 2           2 Bob     Los Angeles      102 Phone      800
## 3           2 Bob     Los Angeles      104 Desktop   1500
## 4           3 Charlie Chicago          103 Tablet     300
## 5           4 David   Houston           NA <NA>        NA
## 6           5 Eve     Phoenix           NA <NA>        NA
## 7           6 <NA>    <NA>             105 Camera     600
## 8           7 <NA>    <NA>             106 Printer    150

5. Semi Join (3 points) Perform a semi join with customers as the left table and orders as the right table.

q5 <- semi_join(customers, orders, by = 'customer_id')

a) How many rows are in the result?

nrow(q5)
## [1] 3

b) How does this result differ from the inner join result?

This table is only returning data from ‘customers’ where there is matching data in ‘orders’, but only returning the data shown in the table ‘customers’.

c) Display the result

q5
## # A tibble: 3 × 3
##   customer_id name    city       
##         <dbl> <chr>   <chr>      
## 1           1 Alice   New York   
## 2           2 Bob     Los Angeles
## 3           3 Charlie Chicago

6. Anti Join (3 points) Perform an anti join with customers as the left table and orders as the right table.

q6 <- anti_join(customers, orders, by = 'customer_id')

a) Which customers are in the result?

David, Eve

b) Explain what this result tells you about these customers.

These are customers that have no corresponding order in the ‘order’ table.

c)Display the result

q6
## # A tibble: 2 × 3
##   customer_id name  city   
##         <dbl> <chr> <chr>  
## 1           4 David Houston
## 2           5 Eve   Phoenix

7. Practical Application (4 points) Imagine you’re analyzing customer behavior.

a) Which join would you use to find all customers, including those who haven’t placed any orders? Why?

left_join because it returns all customers and their corresponding orders, regardless of whether or not they placed an order.

b) Which join would you use to find only the customers who have placed orders? Why?

right_join because it returns all orders with order_id and corresponding customer_id’s.

c) Write the R code for both scenarios.

q2 <- left_join(customers, orders, by = 'customer_id')
q3 <- right_join(customers, orders, by = 'customer_id')

d) Display the result

q2
## # A tibble: 6 × 6
##   customer_id name    city        order_id product amount
##         <dbl> <chr>   <chr>          <dbl> <chr>    <dbl>
## 1           1 Alice   New York         101 Laptop    1200
## 2           2 Bob     Los Angeles      102 Phone      800
## 3           2 Bob     Los Angeles      104 Desktop   1500
## 4           3 Charlie Chicago          103 Tablet     300
## 5           4 David   Houston           NA <NA>        NA
## 6           5 Eve     Phoenix           NA <NA>        NA
q3
## # A tibble: 6 × 6
##   customer_id name    city        order_id product amount
##         <dbl> <chr>   <chr>          <dbl> <chr>    <dbl>
## 1           1 Alice   New York         101 Laptop    1200
## 2           2 Bob     Los Angeles      102 Phone      800
## 3           2 Bob     Los Angeles      104 Desktop   1500
## 4           3 Charlie Chicago          103 Tablet     300
## 5           6 <NA>    <NA>             105 Camera     600
## 6           7 <NA>    <NA>             106 Printer    150

Challenge Question (3 points) Create a summary that shows each customer’s name, city, total number of orders, and total amount spent. Include all customers, even those without orders. Hint: You’ll need to use a combination of joins and group_by/summarize operations.

q8 <- customers %>%
  left_join(orders, by = 'customer_id') %>%
  group_by(customer_id, name, city) %>%
  summarize(total_orders = n_distinct(order_id, na.rm = TRUE),total_spent = sum(amount, na.rm = TRUE)) %>%
  ungroup()
## `summarise()` has grouped output by 'customer_id', 'name'. You can override
## using the `.groups` argument.
q8
## # A tibble: 5 × 5
##   customer_id name    city        total_orders total_spent
##         <dbl> <chr>   <chr>              <int>       <dbl>
## 1           1 Alice   New York               1        1200
## 2           2 Bob     Los Angeles            2        2300
## 3           3 Charlie Chicago                1         300
## 4           4 David   Houston                0           0
## 5           5 Eve     Phoenix                0           0