MySQL Tutorial
MySQL Installation and Configuration
MySQL Database Operations
Database Design
MySQL Data Types
MySQL Storage Engines
MySQL Basic Operations of Tables
MySQL Constraints
MySQL Operators
MySQL Function
MySQL Manipulate Table Data
MySQL View
MySQL Indexes
MySQL Stored Procedure
MySQL Trigger
MySQL Transactions
MySQL Character Set
MySQL User Management
MySQL Database Backup and Recovery
MySQL Log
MySQL Performance Optimization
The INNER JOIN
keyword in MySQL is used to combine rows from two or more tables based on a related column between them. It returns rows that have matching values in both tables.
Here's the basic syntax of an INNER JOIN
:
SELECT column_name(s) FROM table1 INNER JOIN table2 ON table1.column_name = table2.column_name;
For example, suppose we have two tables: Orders
and Customers
, with the following structure:
Orders
table:
| OrderID | CustomerID | OrderDate | |---------|------------|-----------| | 1 | 3 | 2023-01-01| | 2 | 1 | 2023-02-02| | 3 | 2 | 2023-03-03|
Customers
table:
| CustomerID | CustomerName | |------------|--------------| | 1 | John | | 2 | Jane | | 3 | Alice |
You could use an INNER JOIN
to combine these tables based on the CustomerID
column like this:
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate FROM Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
This would return:
| OrderID | CustomerName | OrderDate | |---------|--------------|-----------| | 1 | Alice | 2023-01-01| | 2 | John | 2023-02-02| | 3 | Jane | 2023-03-03|
This result set contains each OrderID
, along with the CustomerName
for the customer who placed the order, and the OrderDate
. Note that it only includes orders where the CustomerID
in the Orders
table matches a CustomerID
in the Customers
table. If an order had a CustomerID
that wasn't in the Customers
table, that order wouldn't appear in the result set. Similarly, if a customer didn't have any orders, that customer wouldn't appear in the result set. That's why it's called an INNER JOIN
- it only returns rows where there's a match in both tables.
MySQL INNER JOIN example:
INNER JOIN
combines rows from two or more tables based on a specified condition, excluding non-matching rows.SELECT orders.order_id, customers.customer_name FROM orders INNER JOIN customers ON orders.customer_id = customers.customer_id;
How to use INNER JOIN in MySQL:
INNER JOIN
keyword to retrieve rows from tables that have matching values in specified columns.SELECT employees.employee_id, departments.department_name FROM employees INNER JOIN departments ON employees.department_id = departments.department_id;
Inner joining tables in MySQL:
SELECT products.product_name, categories.category_name FROM products INNER JOIN categories ON products.category_id = categories.category_id;
Joining multiple tables using INNER JOIN in MySQL:
INNER JOIN
to join multiple tables, linking them based on specified conditions.SELECT orders.order_id, customers.customer_name, products.product_name FROM orders INNER JOIN customers ON orders.customer_id = customers.customer_id INNER JOIN order_items ON orders.order_id = order_items.order_id INNER JOIN products ON order_items.product_id = products.product_id;
Filtering INNER JOIN results in MySQL:
WHERE
clause to filter INNER JOIN
results based on specific criteria.SELECT employees.employee_name, departments.department_name FROM employees INNER JOIN departments ON employees.department_id = departments.department_id WHERE employees.salary > 50000;
MySQL INNER JOIN vs LEFT JOIN:
INNER JOIN
returns only matched rows, while LEFT JOIN
returns all rows from the left table and matched rows from the right table.INNER JOIN
:SELECT customers.customer_name, orders.order_id FROM customers INNER JOIN orders ON customers.customer_id = orders.customer_id;
LEFT JOIN
:SELECT customers.customer_name, orders.order_id FROM customers LEFT JOIN orders ON customers.customer_id = orders.customer_id;
Examples of using INNER JOIN in MySQL queries:
INNER JOIN
statement in MySQL queries.SELECT employees.employee_name, departments.department_name FROM employees INNER JOIN departments ON employees.department_id = departments.department_id; SELECT users.username, roles.role_name FROM users INNER JOIN user_roles ON users.user_id = user_roles.user_id INNER JOIN roles ON user_roles.role_id = roles.role_id;
Nested INNER JOIN in MySQL:
INNER JOIN
operations to join multiple tables in a more complex query.SELECT orders.order_id, customers.customer_name, products.product_name FROM orders INNER JOIN customers ON orders.customer_id = customers.customer_id INNER JOIN order_items ON orders.order_id = order_items.order_id INNER JOIN products ON order_items.product_id = products.product_id WHERE orders.order_date >= '2023-01-01';