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

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

How many rows are in the result?

 nrow(q1)
## [1] 4
Why are some customers or orders not included in the result?

They did not have a match in the other table

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

q2 Perform a left join with customers as the left table and orders as the right table.

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

#Explain why this number differs from the inner join result. #Left join returns all rows from the left table and matching rows from the right table.

#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 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")

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

Customer 6 and 7

Dsiplay 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

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

q4 <- full_join(customers, orders, 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 through 8 have information from only one table. This is because for some rows their is only information for the customers or the order and not both.

#cDisplay the Results

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

Q5 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")

How many rows are in the result?

3

How does this differ from the inner join result

Inner join includes only matching customers and their order details, semi join includes matching customers but does not include details from orders.

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

Q6 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") 

Which customers are in the result?

David and Eve

Explain what this result tells you about these customers.

The anti-join displays customers who have never placed an order.

Display the Result

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

Q7 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 a left join because it ensures that all customers from the customers table are included, regardless of whether they have placed an order.

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

I would use a semi join because a semi join filters the customers table to keep only those who have at least one matching order in the order table. # Write the R code for both scenarios.

q7a <- left_join( customers, orders, by = "customer_id")
q7b <- semi_join(customers , orders, by = "customer_id")

Display the result

q7a
## # 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
q7b
## # 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