SQL Joins: A Comprehensive Beginner’s Guide

img
Ajay Ohri
Share

Introductionย 

SQL joins is a method of extracting data from one or more data tables. In a relational database, this operation is used to link and combine columns from tables based on logical relationships between the tables. This creates a new set of data which can then be used as-is or saved for further use. This join query in SQL can be applied to base tables, views, or joined tables within a single database.ย 

This guide explains how data from separate tables can be combined into a single data set in SQL. However, commands may vary based on the specific database management system in use. So, it is best to look up the official documentation for exact syntax and related command options.

In this article let us look at:

  1. What is SQL Joins?
  2. Types of Joins in SQL
  3. Use of Join in SQL

1. What is SQL Joins?

SQL joins specify how the SQL Server can utilize the data from one data table to base the selection of rows from another data table. This is done based on values common to both tables. Typically this is used when the tables have a one-to-many mapping or a many-to-one mapping to identify relationships between them.ย 

There are two aspects in the join operation in SQL to specify how tables are related:

  • Column name: The join function requires the column from each of the tables to be specified. Typically this is a foreign key- primary key association between the tables.ย 
  • Logical operator: The join function in SQL can be accompanied by a logical operator such as โ€˜equal to (=)โ€™, โ€˜less than (<)โ€™, or โ€˜greater than (>)โ€™ to compare values from the specified columns.

Next, let us understand the difference between joins in SQL.ย 

2. Types of Joins in SQL

Often we are faced with situations where we need to know how to join two tables in SQL or need to join multiple tables in SQL to retrieve relevant data. For this reason, there are various types of joins in SQL which are followed by specific clauses. The clauses are created based on the logical relation between the tables that are being used. This is possible in a relational database setup.ย 

To understand the different types of joins in SQL, let us consider a situation where we need to extract data from two tables with a matching column- Table A and Table B with โ€˜Table Aโ€™, being the first table specified in the join syntax and โ€˜Table Bโ€™ being the second one.ย 

  • Inner join

Also called โ€˜Simple Joinโ€™, this type of join retrieves rows which has matching data in both specified tables. This is the default join in SQL.

  • Left outer joinย 

Also called โ€˜Left Joinโ€™, applying this type of join retrieves all rows from Table A even if there is no data that matches in Table B.

  • Right outer join

Also called โ€˜Right Joinโ€™, applying this type of join retrieves all rows from Table B even if there is no data that matches in Table A

  • Full outer join

Also called โ€˜Full Joinโ€™, applying this type of join retrieves rows if there is a match in at least one of the tables, A or B.ย 

  • Cross join

Also called โ€˜Cartesian Joinโ€™, applying this join gives the Cartesian product of the rows in the specified tables. This means that a โ€˜Cross Joinโ€™ will retrieve a table that has rows that combine each row of Table A with each row of Table B.

  • Self Join

Apart from the different joins in SQL, we have reviewed, in special cases, a table can be joined to itself. This usually requires the table to be referred to by an alias (a temporary name) at least once in the SQL statement. The operation is then executed as if performed on two different tables.

3. Use of Join in SQL

Data is spread across multiple tables in a relational database. The join concept in SQL becomes necessary when you need to get a meaningful set of data in such a database. Thus, for all joins in SQL, the clause that follows the join operator specifies the relation based on which the data need to be extracted from the tables.ย 

Inner join function can be mentioned in either the โ€˜FROMโ€™ and โ€˜WHEREโ€™ clauses. However, Outer Join as well as Cross Join can be specified only in the โ€˜FROMโ€™ clause. Do try using all the different types of joins in SQL with examples on a database.ย 

For example, let us consider a database that stores Employee information and Department information in two different tables- โ€˜Employeeโ€™ and โ€˜Departmentโ€™. There could be a scenario where:

  1. We need the list of employees and the department to which they belong
  2. if the Employee and that mentioned in โ€˜Departmentโ€™ do not match.ย 

Both these situations can be tackled with the help of joins. We can simply join 2 tables in SQL to extract the rows using a โ€˜WHEREโ€™ clause that specifies:ย 

  1. Employee ID in Employee = EmployeeID in Department
  2. Employee ID in Employee <>EmployeeID in Department

Similarly, we can find a relevant application in real life for every type of join.ย 

How to join multiple tables

There may be instances when you need a combination of data from more the two tables. But any type of join can be applied to two tablets at a time. So, for SQL to join multiple tables with conditions, any number of tables can be joined by embedding one JOIN clause within another.ย 

Do keep in mind that:

ยทThe join conditions, the โ€˜WHEREโ€™ clause, and โ€˜HAVINGโ€™ conditions all come together to identify and control the rows selected from the table mentioned in the โ€˜FROMโ€™ clause.ย 

  • Many a time, other search conditions are usually mentioned in the โ€˜WHEREโ€™ clause. To detach the join condition from these, it is recommended that the join condition in SQL is specified in the โ€˜FROMโ€™ clause.ย 

We touched upon join conditions with only two tables. However, when dealing with data in real life, we will need to collect information in more than just two tables. It is therefore imperative that the database is designed and presented well. This could be by use of good naming conventions or by following rules that ensure minimal overlapping of data collected. All these are critical when we attempt to join multiple tables.

Conclusion

By now you have understood how join works in SQL, how many types of joins in SQL, and how we can utilize them to extract data in a relational database. To further your understanding, we encourage you to form queries based on table relations to extract specific rows. Start with a simple SQL query to join two tables. Next, attempt to join 4 tables in SQL and then progress to more complex ones!

If you are interested in making it big in the world of data and evolve as a Future Leader, you may consider ourย Integrated Program in Business Analytics, a 10-month online program, in collaboration with IIM Indore!

Also, Read

What is SQL: Comprehensive Beginner’s Guide

Related Articles

loader
Please wait while your application is being created.
Request Callback