## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

QUESTION 1

  1. Inner Join (3 points) Perform an inner join between the customers and orders datasets.
q1 <- inner_join(customers, orders, by = "customer_id")
  1. How many rows are in the result?
nrow(q1)
## [1] 4
  1. Why are some customers or orders not included in the result?
    Because customers ids dont match with the other table and therefore are not returned
    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

QUESTION 2

  1. 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")
  1. How many rows are in the result?
nrow(q2)
## [1] 6

#C) Explain why this number differs from the inner join result.
The number differs because from inner join because it is now matching customers ids in the orders table with the customer names in customers.
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

QUESTION 3

  1. 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")
  1. How many rows are in the result?
nrow(q3)
## [1] 6
  1. Which customer_ids in the result have NULL for customer name and city? Explain why.
    6 and 7 because in customers there is no customer with the id 6 or 7 therefore it has nothing to return
    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

QUESTION 4

  1. Full Join (3 points) Perform a full join between customers and orders.
q4 <- full_join(customers, orders, by = "customer_id")
  1. How many rows are in the result?
nrow(q4)
## [1] 8
  1. Identify any rows where there’s information from only one table. Explain these results.
    rows 4 and 5 only have information from the customers table, and rows 6 and 7 only have information in the orders table
    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

QUESTION 5

  1. 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")
  1. How many rows are in the result?
nrow(q5)
## [1] 3

#C) How does this result differ from the inner join result?
It is only displaying where there is match between both data tables once.
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

QUESTION 6

  1. 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")
  1. Which customers are in the result?
nrow(q6)
## [1] 2
  1. Explain what this result tells you about these customers.
    Only Dave and Eve do not have orders
    Display the result
q6
## # 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?

#I would use left join because you are joining together all rows from the left table and matching it together with the right regardless if orders have been placed. Assuming you are tracking customers with customer names and ids. If you are looking for all orders placed regardless if the customer has a name or not I would use full join command as it joins both tables together regardless if there is a any name attatched to the customer id

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

I would use a semi join because it returns only the customers who have placed orders. Unlike an inner join, a semi join does not duplicate customer records when multiple matching orders exist; instead, it simply verifies the extistence of a match in the orders table and returns only the corresponding customers from the customer table.

#Write the R code for both scenarios.

LJ <- left_join(customers, orders,by = "customer_id")
SJ <- semi_join(customers, orders, by = "customer_id")

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_orders <- customers %>%
  left_join(orders, by = "customer_id")

customer_summary <- customer_orders %>%
group_by(customer_id, name, city) %>%
  summarize(
    total_orders=sum(!is.na(order_id)),
    total_amount_spent = sum(amount, na.rm = TRUE)
  )
## `summarise()` has grouped output by 'customer_id', 'name'. You can override
## using the `.groups` argument.
print(customer_summary)
## # A tibble: 5 × 5
## # Groups:   customer_id, name [5]
##   customer_id name    city        total_orders total_amount_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