SQL INNER JOIN Example

Let’s look at some examples of the SQL INNER JOIN and understand it’s working.

First let’s create a demo database and table, on which we will perform the INNER JOIN.

SQL
CREATE DATABASE geeks;

USE geeks;

CREATE TABLE professor(
    ID int,
    Name varchar(20),
    Salary int
);

CREATE TABLE teaches(
    course_id int,
    prof_id int,
    course_name varchar(20)
);

INSERT INTO professor VALUES (1, 'Rohan', 57000);
INSERT INTO professor VALUES (2, 'Aryan', 45000);
INSERT INTO professor VALUES (3, 'Arpit', 60000);
INSERT INTO professor VALUES (4, 'Harsh', 50000);
INSERT INTO professor VALUES (5, 'Tara', 55000);

INSERT INTO teaches VALUES (1, 1, 'English');
INSERT INTO teaches VALUES (1, 3, 'Physics');
INSERT INTO teaches VALUES (2, 4, 'Chemistry');
INSERT INTO teaches VALUES (2, 5, 'Mathematics');

The resulting tables will be:

professor table:

IDNameSalary
1Rohan57000
2Aryan45000
3Arpit60000
4Harsh50000
5Tara55000

teaches Table

course_idprof_idcourse_name
11English
13Physics
24Chemistry
25Mathematics

INNER JOIN Query Example

In this example, we will use the INNER JOIN command on two tables.

Query

SELECT teaches.course_id, teaches.prof_id, professor.Name, professor.Salary
FROM professor INNER JOIN teaches ON professor.ID = teaches.prof_id;

Output :
Using the Inner Join we are able to combine the information in the two tables based on a condition and the tuples in the Cartesian product of the two tables that do not satisfy the required condition are not included in the resulting table.

course_idprof_idNameSalary
11Rohan57000
13Arpit60000
24Harsh50000
25Tara55000

SQL Inner Join

SQL INNER JOIN combines two or more tables based on specified columns and retrieves records with matching values in the common columns.

Similar Reads

INNER JOIN IN SQL

The INNER JOIN clause in SQL is used to combine multiple tables and fetch records that have the same values in the common columns....

Syntax

INNER JOIN Syntax is:...

SQL INNER JOIN Example

Let’s look at some examples of the SQL INNER JOIN and understand it’s working....

Important Points About SQL INNER JOIN

INNER JOIN is a SQL JOIN operation that allows users to retrieve data matching data from multiple tables.It returns all the common rows from the tables when the join condition is met.INNER JOIN simplifies the process of handling complex queries, making database management more efficient.INNER JOIN is crucial for tasks like managing customer orders, product inventories, or any relational dataset....