An INNER JOIN in SQL is used to combine rows from two or
more tables based on a related column between them. It returns only the rows
that have matching values in both tables. The join condition specifies how the
tables are related. If there are no matching values, the rows from both tables
are excluded from the result set. INNER JOIN is the default join type in SQL.
It ensures that only the common rows between the tables are included in the
output, effectively filtering out unmatched rows.
Consider two tables, Orders and Customers, with
columns as follows:
- Orders:
OrderID, CustomerID, OrderDate
- Customers:
CustomerID, CustomerName, City
We want to retrieve the orders along with the corresponding
customer information (customer name and city).
SELECT Orders.OrderID,
Orders.OrderDate, Customers.CustomerName, Customers.City FROM Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
In this example, the INNER JOIN combines rows from the Orders
and Customers
tables where the CustomerID
matches in
both tables. The result set will include columns from both tables, such as OrderID
, OrderDate
,
CustomerName
, and City
,
based on the matching CustomerID
. This query
returns only the orders that have corresponding customer information.
Comments
Post a Comment