#1 Inner Join - perform an inner join between the customers and orders datasets.

q1 <- inner_join(customers, orders)

#1a How many rows are in the result?

The resulting data has 4 rows

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

Some customers and orders don’t match, they are not included

#1c Display the result

head(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 - Perform a left join with customers as the left table and orders as the right table

q2 <- left_join(customers, orders)

#2a How many rows are in the result?

The resulting data has 6 rows

#2b Explain why this number differs from the inner join result

A left join keeps all customers from the left table, even if there are no matches

#2c Display the result

head(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 - Perform a right join with customers as the left table and orders as the right table

q3 <- right_join(customers, orders)

#3a How many rows are in the result?

The resulting data has 6 rows

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

Customer_id 6 and 7 have NA name/city because those orders exist in orders, but there is no matching customer record

#3c Display the result

head(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 - Perform a full join between customers and orders

q4 <- full_join(customers, orders)

#4a How many rows are in the result?

The resulting data has 8 rows

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

Customer_id 4 and 5 customer info exists, but no matching orders Customer_id 6 and 7 order info exists, but no matching customer

#4c Display the result

head(q4)
## # 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

#5 Semi Join - perform a semi join with customers as the left table and orders as the right table

q5 <- semi_join(customers, orders)

#5a How many rows are in the result?

The resulting data has 3 rows

#5b How does this result differ from the inner join result?

Semi Join returns only columns from the left table, only customers with order information are returned and order data isnt included

#5c Display the result

head(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 - Perform an anti join with customers as the left table and orders as the right table

q6 <- anti_join(customers, orders)

#6a Which customers are in the result?

q6 %>% 
  pull(name)
## [1] "David" "Eve"

#6b Explain what this result tells you about these customers.

These customers exist in customers but have not placed any orders in orders

#6c Display the result

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

#7 Practical Application - Imagine youre analyzing customer behavior

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

I would use left join because it keeps every customer even if there’s no match in orders

allCustomers <- left_join(customers, orders)
head(allCustomers)
## # 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

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

I would use semi join because it filters to only customers that appear in orders, and returns each customer once

customersOrders <- semi_join(customers,orders)
head(customersOrders)
## # 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

#8 Challenge Question

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.

customerSummary <- customers %>%
  left_join(
    orders %>%
      group_by(customer_id) %>%
      summarise(
        total_orders = n(),
        total_amount_spent = sum(amount)
      ),
    by = "customer_id"
  ) %>%
  mutate(
    total_orders = coalesce(total_orders, 0),
    total_amount_spent = coalesce(total_amount_spent, 0)
  )

customerSummary
## # A tibble: 5 × 5
##   customer_id name    city        total_orders total_amount_spent
##         <dbl> <chr>   <chr>              <dbl>              <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