SQL JOIN
A INNER JOIN keyword selects records that have matching values in both tables.
Let's look at a selection from the "products" table:
Then look at a selection from the "employee" table:
We will join the Products table with the Categories table, by using the CategoryID field from both tables:
Example
Join Products and Categories with the INNER JOIN keyword: SELECT Product_id, Product_name, Category_name FROM products INNER JOIN category ON products.Category_id = category.Category_id;You can click on above box to edit the code and run again.
Output
Note:The INNER JOIN keyword returns only rows with a match in both tables. Which means that if you have a product with no Category_id, or with a Category_id that is not present in the Category table, that record would not be returned in the result.
Syntax
SELECT column_name(s) FROM table1 INNER JOIN table2 ON table1.column_name = table2.column_name;
Naming the Columns
It is a good practice to include the table name when specifying columns in the SQL statement.
Example
SELECT products . Product_id , products . Product_name , category . Category_name FROM products INNER JOIN category ON products.Category_id = category.Category_id;You can click on above box to edit the code and run again.
Output
The example above works without specifying table names, because none of the specified column names are present in both tables. If you try to include Category_id in the SELECT statement, you will get an error if you do not specify the table name (because Category_id is present in both tables).
JOIN or INNER JOIN
JOIN and INNER JOIN will return the same result.
INNER is the default join type for JOIN , so when you write JOIN the parser actually writes INNER JOIN
JOIN Three Tables
The following SQL statement selects all products with employee and category information:
Example
SELECT products . Product_id , employee . Employee_name , category . Category_name FROM (( products INNER JOIN employee ON products.Employee_id = employee.Employee_id) INNER JOIN category ON products.Category_id = category.Category_id);You can click on above box to edit the code and run again.