In an INNER JOIN, the ON keyword specifies the condition that defines how the two tables are related. It is used to indicate which columns from each table should be compared to find matching rows.
Purpose of the ON Keyword:
-
Join Condition: The
ONclause contains the criteria that determine which rows from the first table match with rows from the second table. This is typically based on a foreign key relationship. -
Column Specification: You specify the columns from each table that should be compared. The condition usually involves equality (e.g.,
table1.column = table2.column).
Example:
Here’s a simple example of an INNER JOIN using the ON keyword:
SELECT orders.order_id, customers.first_name
FROM orders
INNER JOIN customers ON orders.customer_id = customers.customer_id;
In this example:
ordersis the first table, andcustomersis the second table.- The
ONclause specifies that thecustomer_idcolumn in theorderstable should match thecustomer_idcolumn in thecustomerstable. - Only rows where this condition is true will be included in the result set.
Overall, the ON keyword is essential for defining the relationship between the tables being joined.
