|
| 1 | +--- |
| 2 | +id: sql-inner-join |
| 3 | +title: Inner Join in SQL |
| 4 | +sidebar_label: Inner Join |
| 5 | +sidebar_position: 14 |
| 6 | +tags: [sql, database, operation] |
| 7 | +description: In this tutorial, we will learn about inner joins in sql. |
| 8 | +--- |
| 9 | + |
| 10 | +## What is an inner join? |
| 11 | +An inner join of 2 tables, say table_1 and table_2 on a column would return all rows with same values in common columns.An inner join may or may not have an 'on' clause. An inner join without an 'on' clause returns the cross join of the tables. |
| 12 | + |
| 13 | +## Syntax |
| 14 | + |
| 15 | +```sql |
| 16 | +select * |
| 17 | +from table_1 inner join table_2 |
| 18 | +on table_1.col=table_2.col; |
| 19 | +``` |
| 20 | + |
| 21 | +##### Note that the columns of table_1 and table_2 in the on clause must be the same attribute. |
| 22 | + |
| 23 | +## Example |
| 24 | + |
| 25 | +Consider the following tables: |
| 26 | + |
| 27 | +```sql |
| 28 | +select * from students; |
| 29 | ++---------+-----------+ |
| 30 | +| stud_id | stud_name | |
| 31 | ++---------+-----------+ |
| 32 | +| 101 | Shreeya | |
| 33 | +| 102 | Aakash | |
| 34 | +| 103 | Mansi | |
| 35 | +| 104 | Aditya | |
| 36 | ++---------+-----------+ |
| 37 | + |
| 38 | + select * from grades; |
| 39 | ++---------+-------+ |
| 40 | +| stud_id | grade | |
| 41 | ++---------+-------+ |
| 42 | +| 101 | A | |
| 43 | +| 104 | A+ | |
| 44 | ++---------+-------+ |
| 45 | +``` |
| 46 | + |
| 47 | +Now , lets try to obtain a result using inner join with and without the on clause. |
| 48 | + |
| 49 | +##### With 'on' clause: |
| 50 | +```sql |
| 51 | +select s.stud_id, s.stud_name, g.grade |
| 52 | +from students s inner join grades g |
| 53 | +on s.stud_id=g.stud_id; |
| 54 | + |
| 55 | +Output: |
| 56 | ++---------+-----------+-------+ |
| 57 | +| stud_id | stud_name | grade | |
| 58 | ++---------+-----------+-------+ |
| 59 | +| 101 | Shreeya | A | |
| 60 | +| 104 | Aditya | A+ | |
| 61 | ++---------+-----------+-------+ |
| 62 | +``` |
| 63 | +We can observe that only the rows with matching values in common column (stud_id) are returned. |
| 64 | + |
| 65 | +##### Without 'on' clause: |
| 66 | +```sql |
| 67 | +select s.stud_id, s.stud_name, g.grade |
| 68 | +from students s inner join grades g; |
| 69 | + |
| 70 | +Output: |
| 71 | ++---------+-----------+-------+ |
| 72 | +| stud_id | stud_name | grade | |
| 73 | ++---------+-----------+-------+ |
| 74 | +| 101 | Shreeya | A | |
| 75 | +| 101 | Shreeya | A+ | |
| 76 | +| 102 | Aakash | A | |
| 77 | +| 102 | Aakash | A+ | |
| 78 | +| 103 | Mansi | A | |
| 79 | +| 103 | Mansi | A+ | |
| 80 | +| 104 | Aditya | A | |
| 81 | +| 104 | Aditya | A+ | |
| 82 | ++---------+-----------+-------+ |
| 83 | +``` |
| 84 | +Here we can see that the output is the cross join of both the tables. |
| 85 | + |
| 86 | +## Conclusion |
| 87 | +In this tutorial, we learnt how to use the inner join with and without the 'on' clause. |
| 88 | +Inner joins are used when we want to retrieve all the rows with same values in common column(s). |
0 commit comments