sql

INNER JOIN SQL Query

When working with databases, there are often times when you need to combine data from two or more tables. This is where the SQL INNER JOIN statement comes in handy.

SELECT 
    Column1 FROM tbl1,
    Column2 From tbl1,
    Column1 FROM tbl2
FROM 
    table1 as tbl1
INNER JOIN 
    table2 as tbl2
ON tbl1.column_tbl1 = tbl2.column_tbl2

To apply INNER JOIN on two tables we can use the above SQL query. It will show the rows from 'table1' and 'table2' which has common values for "column_tbl1" from "tbl1" and "column_tbl2" from "tbl2"


Example 1: INNER JOIN Query on customers and orders tables

An INNER JOIN will combine rows from two or more tables based on a matching column value. For example, let's say you have a customers table and an orders table. Each table has a customer_id column. By using an INNER JOIN, you can combine all the rows from the "customers" table with all the rows from the orders table where the customer_id values match. 

Here's a basic example of an INNER JOIN SQL query: 

SELECT * FROM customers
INNER JOIN orders ON customers.customer_id = orders.customer_id

Example 2: INNER JOIN based on multiple columns

The below query will return all rows from both tables where there is a match on the customer_id and product_id columns:

SELECT *
FROM customers
INNER JOIN orders
ON customers.customer_id = orders.customer_id
AND customers.product_id = orders.product_id

Example 3: INNER JOIN query to find the employees and the departments they work in:

SELECT firstName, lastName, department_name 
FROM employees 
INNER JOIN departments 
ON employees.department_number = departments.department_number

Was this helpful?