You can use the SQL JOIN command to gather data from two different tables into a single, merged display table that includes these tables’ included columns. You should generally JOIN tables using one or more factors or values to identify them and try again.
You can join two different tables together. The condition in the first table is evaluated for each row in the second table and, if it matches the condition, the row
Let’s say that you have a database of customer information in a table called Customer_Info.
Every time you sell a product to a customer, you record the Customer Number, Product ID, Price, and Amount Paid for that customer.
The details of orders that are placed on the gateway by the customer are stored in another table called the orders table, which contains the Order Number, Order Date, Expected Shipping Date, and Customer Number.
Imagine that a customer places an order and the following thing that you need to find out is its delivery address.
Although the table only displays customer number data, you can use other column names such as “Customer_Number” and “Product_ID” to order
Join the tables Customers and Order_Number products based on the customer’s number information is necessary. To retrieve the customer’s address, you must use the JOIN
In this specific case, the Customer_Number column is used to compare values.
Customer_Number | Customer_Name | Age | Postal_Code | Address |
103 | Atelier | 27 | 44000 | 54, RueRoyal |
112 | Signal | 32 | 83030 | 8489 Strong |
114 | Collector | 27 | 3004 | 636 Kilda |
119 | La Roche | 27 | 44000 | 67, rue chimay |
121 | Baane mini | 32 | 4110 | Ering Shakkes |
customers
Order_Number | Order_Date | Expected_Shipping_Date | Customer_Number |
10345 | 31-01-2022 | 10-02-2022 | 103 |
10346 | 30-01-2022 | 15-02-2022 | 112 |
10120 | 05-02-2022 | 16-02-2022 | 114 |
10325 | 06-02-2022 | 10-02-2022 | 121 |
11211 | 08-02-2022 | 21-02-2022 | 4110 |
orders
Use the query shown here to connect all of the order numbers to customer names and addresses:
SELECT
a.Order_Number,
b.Customer_Name,
b.Postal_Code,
b.Address
FROM orders a
JOIN customers b
ON a.Customer_Number = b.Customer_Number;
Result:
Order_Number | Customer_Name | Postal_Code | Address |
10345 | Atelier | 44000 | 54, RueRoyal |
10346 | Signal | 83030 | 8489 Strong |
10120 | Collectors | 3004 | 636 Kilda |
10325 | Baane Mini | 4110 | Ering Shakkes |
Here’s how you would use JOIN to combine multiple tables into one. In this query, SQL uses Customer_Number as the JOIN condition. For each order, the matching customer number from orders is compared to the customer number in the customers table. The customer name, postal code, and address are retrieved from that order.