SQL INNER JOIN Keyword
SQL inner join is used to
retrieve only the matching rows from two or more tables based on a common
column between them. It returns only the rows where the specified column has
matching values in both tables being joined. The syntax for an inner join is as
follows:
SELECT
column1, column2, ...
FROM
table1
JOIN
table2
ON table1.common_column = table2.common_column;
IN
THIS SYNTAX:
· SELECT specifies the columns you want to
get from the resulting join.
· FROM specifies the first table you
want to join.
· JOIN is used to specify the type of
join, this case is an inner join.
· table2 is the second table you want to
join.
· ON is used to specify the condition
for joining the tables, which is the common column between them (common_column
in this example). This condition determines which rows from both tables will be
included in the result set.
For example, let's say you
have two tables, orders and customers, and you want to retrieve
information about orders along with the corresponding customer information for
orders that have a matching customer in the customers table. You can use
an inner join as follows:
SELECT
orders.order_id,
orders.order_date,
customers.customer_name
FROM
orders
JOIN
customers
ON orders.customer_id = customers.customer_id;
This query will retrieve
the order_id and order_date from the orders table, and the
customer_name from the customers table for orders that have a
matching customer_id in both tables. Only the rows where there is a
match in both tables will be included in the result set.
JOIN Three Tables
The following SQL statement selects all orders with customer and shipper information:
Example
SELECT Orders.OrderID, Customers.CustomerName, Shippers.ShipperName
FROM
((Orders
INNER JOIN
Customers ON
Orders.CustomerID
= Customers.CustomerID)
INNER JOIN
Shippers ON Orders.ShipperID = Shippers.ShipperID);
0 Comments