Inner Joins

An inner join combines rows in the first table with rows in the second table based on a common value. The results of an inner join include only those rows that share the common column (or field) value.

An explicit inner join uses the INNER JOIN and ON keywords. Here's an example of an explicit inner join:

          SELECT customer.entityid, customer.email, employee.entityid
FROM customer
INNER JOIN employee ON customer.salesrep = employee.id 

        

This explicit join is equivalent to the following implicit inner join, which uses the WHERE keyword instead of the INNER JOIN and ON keywords:

          SELECT customer.entityid, customer.email, employee.entityid
FROM customer, employee
WHERE customer.salesrep = employee.id 

        

Using the example database tables, these joins produce four result rows:

customer.entityid

customer.email

employee.entityid

Big Computers

bigcomputers@example.com

Eugene Evans

Fantastic Laptops

fantasticlaptops@example.com

Carol Connors

Tall Manufacturing

tallmanufacturing@example.com

Allie Anderson

Vision Corporation

visioncorporation@example.com

Allie Anderson

In this example inner join, a result row is included only if the value of the salesrep field in the customer table matches the value of the id field in the employee table.

The following diagram illustrates an inner join:

                                                       

Related Topics

General Notices