sql inner join

sql inner join

Posted by Ervin Adams on December 18, 2022

sql inner join

sql inner join

Posted by Ervin Adams on December 18, 2022

The INNER JOIN is a clause used to combine two or more tables in an sql query.

A new result table is created from the INNER JOIN when combining values from the tables.

Each row from the tables are compared to combine all the rows that will output the desired result.

Here is an example of a INNER JOIN in sql below:

SELECT tableA.columnA, tableB.columnB
FROM tableA
INNER JOIN tableB
ON tableA.column = tableB.column;

Here is an example:

Table A are students in a school

+----+----------+-----+------------+
| id | name     | age | student_num|
+----+----------+-----+------------+
|  1 | Kevin    |  15 | 078657     |
|  2 | Dane     |  16 | 076565     |
|  3 | James    |  15 | 087675     |
|  4 | Sandy    |  14 | 075657     |
|  5 | Ava      |  16 | 085654     |
|  6 | Zuri     |  15 | 075555     |
+----+----------+-----+------------+

Table B are adminstrations details

+----+-----------+--------+---------------------+------------+
| id | subject   | teacher| date                | student_id |
+----+-------------+----------------------------+------------+
|  1 | Math      | Mr Jack| 2022-10-08 00:00:00 | 1          |
|  2 | English   | Mrs Jen| 2022-10-08 00:00:00 | 1          |
|  3 | Physics   | Mrs Lin| 2021-09-08 00:00:00 | 4          |
|  4 | Geography | Mr Dan | 2020-09-08 00:00:00 | 3          |
+----+-------------+---------+------------------+------------+

Let us do the INNER JOIN on the tables above:

SELECT  id, name, student_num, subject, date
   FROM students
   INNER JOIN administrations
   ON students.id = administrations.student_id;

Result

+----+----------+-------------+--------------------------------+
| id | name     | student_num | subject  | date                |
+----+----------+-------------+--------------------------------+
|  1 | Kevin    | 078657      | Math     | 2022-10-08 00:00:00 |               
|  1 | Kevin    | 078657      | English  | 2022-10-08 00:00:00 |
|  4 | Sandy    | 075657      | Physics  | 2021-09-08 00:00:00 |
|  3 | James    | 087675      | Geography| 2020-09-08 00:00:00 |
+----+----------+-------------+--------------------------------+

 

Any question?
This field is required
Your question have been successfully submitted and will be reviewed before published
Please login to ask a question