Sql Inner Join

Sql Inner Join

Blog Bustle 02/04/2020

Sql inner join is used to fetch common data between two tables that have at least one common column value to define relationship between them. We often use sql inner join. It is one of the important sql joins. We can use multiple inner joins in single query.

Sql Inner Join syntax

SELECT table_1.*,table_2.* FROM table_1
INNER JOIN table_2
ON table_1.common_column = table_2.common_column;

Sql Inner Join examples

Let's look at some examples to understand sql inner join.

I have two tables named employee and employee_salary.

id name city_name country
1 justin New york US
2 robin Binghamton US
3 jessie Nottingham England
4 chris Manchester England
id emp_id emp_salary
1 1 30000
2 2 40000
3 3 45000
4 4 50000

Find name and salary of those employees who belong to England and sql query will be

SELECT employee.name,employee_salary.emp_salary
FROM employee
INNER JOIN employee_salary
ON employee.id = employee_salary.emp_id
WHERE employee.country = 'England';

We can also use sql aliases to write this query

SELECT e.name,s.emp_salary AS salary
from employee AS e
INNER JOIN employee_salary AS s
ON e.id = s.emp_id
WHERE e.country = 'England';

After running this sql query, result will be

name salary
jessie 45000
chris 50000

Joining 3 tables

I have third table named dialing_code.

id country code
1 US +1
2 England +44

Find name and salary of those employees who belong to US and new york city with country dialing code and sql query will be

SELECT e.name,s.emp_salary AS salary,d.code
from employee AS e
INNER JOIN employee_salary AS s
ON e.id = s.emp_id
INNER JOIN dialing_code AS d
ON e.country = d.country
WHERE e.country = 'US' and e.city_name = 'New York';

After running this sql query, result will be

name salary code
justin 30000 +1

After looking at these examples, we can easily understand and use sql inner join. Hope you like this tutorial.

Other important links

Sql Aliases

Sql Group By clause

Sql Limit clause

Sql Order By clause

Sql Aggregate Functions

Sql Inner Join searches:

Tags: #mysql