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

inner <- inner_join(customers, orders)
## Joining with `by = join_by(customer_id)`

How many rows are in the result?

4

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

Some customers/orders are not included because there is no customer_id match (we don’t have customer ids 6 and 7 in the customer dataset)

Display the result

print(inner)
## # 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

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

left <- left_join(customers, orders)
## Joining with `by = join_by(customer_id)`

How many rows are in the result?

6

Explain why this number differs from the inner join result.

This number differs from the inner join result because it also includes the 4th and 5th employees even though they don’t have a match in the orders dataset

Display the result

print(left)
## # 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

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

right <- right_join(customers, orders)
## Joining with `by = join_by(customer_id)`

How many rows are in the result?

6

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

Customer_ids 6 and 7 because there is no information on them in the customers dataset.

Display the result

print(right)
## # 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

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

full <- full_join(customers, orders)
## Joining with `by = join_by(customer_id)`

How many rows are in the result?

8

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

Rows 5, 6, 7, and 8 have information from only one table. Rows 5 and 6 have customer ids with no matching order id, and 7 and 8 have order ids with no matching customer ids.

Display the result

print(full)
## # 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

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

semi <- semi_join(customers, orders)
## Joining with `by = join_by(customer_id)`

How many rows are in the result?

3

How does this result differ from the inner join result?

semi join returns rows in the customers where there’s a match in the orders table but not any columns from the right table unlike the inner table which does return columns that match in the orders table

Display the result

print(semi)
## # 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

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

anti <- anti_join(customers, orders)
## Joining with `by = join_by(customer_id)`

Which customers are in the result?

Customers 4 and 5

Explain what this result tells you about these customers.

These are the customers that have no customer_id match in the orders table.

Display the result

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

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

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

full_join because it includes all customers, including those who haven’t placed orders and those who have placed orders but whose names and city we do not know.

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

right_join because it includes all the orders and the customer_ids of those who placed the orders, even customer_ids 6 and 7 which we have no additional information on.

Write the R code for both scenarios.

all_customers <- full
customers_who_ordered <- right

Display the result

print(all_customers)
## # 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
print(customers_who_ordered)
## # 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.

customer_summary <- full %>%
  group_by(customer_id, name, city) %>%
  summarize(
    total_orders = sum(!is.na(order_id)),  # Count non-NA order_ids
    total_spent = sum(amount, na.rm = TRUE)  # Sum of amount, ignoring NA values
  )
## `summarise()` has grouped output by 'customer_id', 'name'. You can override
## using the `.groups` argument.
print(customer_summary)
## # A tibble: 7 × 5
## # Groups:   customer_id, name [7]
##   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
## 6           6 <NA>    <NA>                   1         600
## 7           7 <NA>    <NA>                   1         150