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

inner_cust_orders <- inner_join(customers, orders, by ='customer_id')

How many rows are in the result?

4 rows

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

They do not have a matching Primary and foreign key in the two tables

Display the result

inner_cust_orders
## # 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 (3 points) Perform a left join with customers as the left table and orders as the right table.

left_cust_orders <- left_join(customers, orders, by = 'customer_id')

2. How many rows are in the result?

6 rows

Explain why this number differs from the inner join result.

The number differs because inner only takes information that matches, but left join will take all inforatmion from the left table and put information that matches from the right

Display the result

left_cust_orders
## # 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.

right_cust_orders <- right_join(customers, orders, by ='customer_id')

How many rows are in the result?

There are 6 rows in the result

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

6 and 7, because they do not have that information in the customer table but have that information in the order table and its the right table in the join.

Display the result

right_cust_orders
## # 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.

full_cust_orders <- full_join(customers, orders, by ='customer_id')

How many rows are in the result?

There are 8 rows

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

Rows 5,6,7,8 have information have only one table but get included because it is a full join

##Display the result

full_cust_orders
## # 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

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

semi_cust_orders <- semi_join(customers, orders, by ='customer_id')

How many rows are in the result?

There are three rows as a result

How does this result differ from the inner join result?

Bob only has a singular row in the semi compared to the two rows in the inner

Display the result

semi_cust_orders
## # 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.

ant_cust_orders <- anti_join(customers, orders, by ='customer_id')

Which customers are in the result?

Dave and Eve

Explain what this result tells you about these customers.

They have information in the customers tab but not the orders tab

Display the result

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

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

A left join with customers as the left table since it will reutrn all customers even those who haven’t placed a order.

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

A inner join since it will return only customer ID’s that have matches in both tables

Write the R code for both scenarios

inner_cust_orders <- inner_join(customers, orders, by ='customer_id')
left_cust_orders <- left_join(customers, orders, by = 'customer_id')

Display the result

Left Join

left_cust_orders
## # 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

Inner Join

inner_cust_orders
## # 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