first_question <- inner_join(customers , orders , by = 'customer_id')
4
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
second_question <- left_join(customers, orders, by = "customer_id")
6
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
third_question <- right_join(customers, orders, by = "customer_id")
6
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
fourth_question <- full_join(customers, orders, by = "customer_id")
8
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
fifth_question <- semi_join(customers, orders, by = "customer_id")
3
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
sixth_question <- anti_join(customers, orders, by = "customer_id")
2
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
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.
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.
seventh_question_a <- left_join(customers, orders, by = "customer_id")
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
## [1] "Alice" "Bob" "Charlie" "David" "Eve"
seventh_question_b <- right_join(customers, orders, by = "customer_id")
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
## [1] "Alice" "Bob" "Charlie"
# 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