Question 1

Perform an inner join between the customers and orders datasets.
first_question <- inner_join(customers , orders , by = 'customer_id')
How many rows are in the result?

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

Perform a left join with customers as the left table and orders as the right table.
second_question <- left_join(customers, orders, by = "customer_id")
How many rows are in the result?

6

Explain why this number differs from the inner join result.

The result has the same number of rows as the customers table since all customers are retained, with unmatched orders appearing as NA.

# Display the result
second_question
## # 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

Perform a right join with customers as the left table and orders as the right table.
third_question <- right_join(customers, orders, by = "customer_id")
How many rows are in the result?

6

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

The right join keeps all orders, including those without a matching customer, resulting in NULL values for customer name and city.

# Display the result
third_question
## # 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

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

A Full Join includes all rows from both tables, with NA where there’s no match. Rows with NA contain data from only one table.

# Display the result
fourth_question
## # 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

Perform a semi join with customers as the left table and orders as the right table.
fifth_question <- semi_join(customers, orders, by = "customer_id")
How many rows are in the result?

3

How does this result differ from the inner join result?

A semi join returns only the customers who have matching orders, excluding order-related columns.

# Display the result
fifth_question
## # 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

Perform an anti join with customers as the left table and orders as the right table.
sixth_question <- anti_join(customers, orders, by = "customer_id")
Which customers are in the result?

2

Explain what this result tells you about these customers.

The Anti Join shows that David and Eve haven’t placed any orders.

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

Question 7

Which join should be used to find all customers-including those who haven’t placed any orders?

To include all customers, even those without orders, I used a Left Join, which returns all rows from the customers table and only the matching rows from the orders table.

Which join should be used to find only customers who have placed orders?

To make sure all orders are included, even if they don’t have a matching customer, I used a Right Join, which returns all rows from the orders table and only the matching rows from the customers table.

Left join with customers as left table and orders as right table.
seventh_question_a <- left_join(customers, orders, by = "customer_id")
Display result of left join.
print(seventh_question_a)
## # 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
Customer names.
## [1] "Alice"   "Bob"     "Charlie" "David"   "Eve"
Right join with customers as left table and orders as right table.
seventh_question_b <- right_join(customers, orders, by = "customer_id")
Display result of right join.
print(seventh_question_b)
## # 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
Customers that have placed orders.
## [1] "Alice"   "Bob"     "Charlie"

Question 8 - Challenge Question

Make a summary displaying each customer’s name, city, total orders, and total amount spent, ensuring all customers are included, even those without orders.
Display Results
# Display the result
eighth_question
## # A tibble: 7 × 5
## # Groups:   customer_id, name [7]
##   customer_id name    city        total_orders total_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          NA
## 5           5 Eve     Phoenix                0          NA
## 6           6 <NA>    <NA>                   1         600
## 7           7 <NA>    <NA>                   1         150