A guide to the SQL Inner Join Keyword

A guide to the SQL Inner Join Keyword
0

Example of use

For this guide we’ll discuss the SQL (INNER) Joins

Join (same as Inner Join)

The student table will be in the FROM clause so it will be a starting or LEFT table.

We’ll JOIN this to the student contact table or RIGHT table. You’ll see that all of the students appear that ALSO are in the contact table. As shown in the tables below, studentID 9 is in the student table but NOT in the contact table so won’t appear in a join.

SQL Statement

SELECT a.studentID, a.FullName, a.programOfStudy,
b.`student-phone-cell`, b.`student-US-zipcode`
FROM student AS a
INNER JOIN `student-contact-info` AS b ON a.studentID = b.studentID;

“Joined” data

+-----------+------------------------+------------------+--------------------+--------------------+
| studentID | FullName               | programOfStudy   | student-phone-cell | student-US-zipcode |
+-----------+------------------------+------------------+--------------------+--------------------+
|         1 | Monique Davis          | Literature       | 555-555-5551       |              97111 |
|         2 | Teri Gutierrez         | Programming      | 555-555-5552       |              97112 |
|         3 | Spencer Pautier        | Programming      | 555-555-5553       |              97113 |
|         4 | Louis Ramsey           | Programming      | 555-555-5554       |              97114 |
|         5 | Alvin Greene           | Programming      | 555-555-5555       |              97115 |
|         6 | Sophie Freeman         | Programming      | 555-555-5556       |              97116 |
|         7 | Edgar Frank "Ted" Codd | Computer Science | 555-555-5557       |              97117 |
|         8 | Donald D. Chamberlin   | Computer Science | 555-555-5558       |              97118 |
+-----------+------------------------+------------------+--------------------+--------------------+

Complete table listings for reference

Student table SQL

SELECT a.studentID, a.FullName, sat_score, a.programOfStudy, schoolEmailAdr 
FROM student AS a;

student or LEFT table

+-----------+------------------------+-----------+------------------+------------------------+
| studentID | FullName               | sat_score | programOfStudy   | schoolEmailAdr         |
+-----------+------------------------+-----------+------------------+------------------------+
|         1 | Monique Davis          |       400 | Literature       | [email protected] |
|         2 | Teri Gutierrez         |       800 | Programming      | [email protected]    |
|         3 | Spencer Pautier        |      1000 | Programming      | [email protected] |
|         4 | Louis Ramsey           |      1200 | Programming      | [email protected]   |
|         5 | Alvin Greene           |      1200 | Programming      | [email protected]   |
|         6 | Sophie Freeman         |      1200 | Programming      | [email protected]  |
|         7 | Edgar Frank "Ted" Codd |      2400 | Computer Science | [email protected]   |
|         8 | Donald D. Chamberlin   |      2400 | Computer Science | [email protected]  |
|         9 | Raymond F. Boyce       |      2400 | Computer Science | [email protected] |
+-----------+------------------------+-----------+------------------+------------------------+
9 rows in set (0.00 sec)


```sql
SELECT * FROM `student-contact-info` AS b;

student contact table or RIGHT table

+-----------+----------------------------------+--------------------+--------------------+
| studentID | studentEmailAddr                 | student-phone-cell | student-US-zipcode |
+-----------+----------------------------------+--------------------+--------------------+
|         1 | [email protected]   | 555-555-5551       |              97111 |
|         2 | [email protected]  | 555-555-5552       |              97112 |
|         3 | [email protected] | 555-555-5553       |              97113 |
|         4 | [email protected]    | 555-555-5554       |              97114 |
|         5 | [email protected]     | 555-555-5555       |              97115 |
|         6 | [email protected]  | 555-555-5556       |              97116 |
|         7 | [email protected]    | 555-555-5557       |              97117 |
|         8 | [email protected]   | 555-555-5558       |              97118 |
+-----------+----------------------------------+--------------------+--------------------+
8 rows in set (0.00 sec)

Conclusion

As with all of these SQL things there is MUCH MORE to them than what’s in this introductory guide.

I hope this at least gives you enough to get started.

Please see the manual for your database manager and have fun trying different options yourself.