SQL Joins

SQL JOIN is used to combine two or more tables based on primary key and foreign keys.

We use Join in conjunction with the table we normalize during database design. Consider a Department table we have in our previous example. We create a Department table that serves as our master file to be called from other table.

SQL JOIN can be very time consuming on large table, e.g. one with millions of records, since it has to match each row of one table, and check it with each row of other tables mentioned in the JOIN keyword.

Consider the following table for this exercise.

Users

Firstname Lastname Salary DeptID
John Smith 1000 1
Mathew Simon 3000 1
Bill Steve 2200 1
Amanda Rogers 1800 2
Steve Hills 2800 2
Steve jobs 2400 2
bill cosby 700 3

Departments

DeptID DepartmentName
1 Employee
2 Management
3 Staff

Please don't be confused with the previous table that we have used before. In the above example we have DeptID in the Users table and we also have DeptID in the Departments table. DeptID in the Departments table is the Primary key and DeptID in the Users table is the Foreign key. We can use those keys to create a join. They are responsible for combine two or more tables in our database.

Add new comment