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

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

How many rows are in the result?

There are four rows in the result.

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

Inner join returns all rows from both tables where there is a match. Therefore the rows not returned did not have a match in the other table

Display the results

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

Left join: customers on the left, orders on the right

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

How many rows are in the result?

There are six rows in the result

Explain why this number differs from the inner join result.

Left join keeps all of the customers even if they don’t have an order. Inner join will excude those without orders (David & Eve)

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

How many rows are in the result?

There are six rows in the result

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

Customer 5 and 6 have NUll for customer name and city. These orders must haven’t collected the customer name and city

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 (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?

There are eight rows in the result

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

Charlie and David don’t have the product or the amount. Camera and Printer do not have a name or city. There is missing data in this data set so full join is leaving it blank.

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 (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?

There are three rows in the result

How does this result differ from the inner join result?

Inner join includes customer id, name, city, order id, product and amount. Semi join only takes from the left table, including customer id, name, and city.

How does this result differ from the inner join 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 (3 points) Perform an anti join with customers as the left table and orders as the right table.

q6<-full_join(customers, orders, by="customer_id")
Which customers are in the result?

Alice,Bob,Bob,Charlie,David,Eve

Explain what this result tells you about these customers.

Bob could have placed two orders, David and Eve order information isn’t included, and finally the last two don’t have customers.

Display the result
head(q6)
## # 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

6.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 do a full join because it includes customers that haven’t ordered yet and those who have.

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

I would do a inner join because it shows all the customers who have placed orders

Write the R code for both scenarios.
q7a<-inner_join(customers , orders)
## Joining with `by = join_by(customer_id)`
q7b<-full_join(customers, orders, by="customer_id")

Display the result

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