SQLite Join Tables: Inner, Natural, Left Outer, Cross (Examples)

SQLite supports different types of SQL Joins, like INNER JOIN, LEFT OUTER JOIN, and CROSS JOIN. Each type of JOIN is used for a different situation as we will see in this tutorial.

In this tutorial, you will learn-

Introduction to SQLite JOIN Clause

When you are working on a database with multiple tables, you often need to get data from these multiple tables.

With the JOIN clause, you can link two or more tables or subqueries by joining them. Also, you can define by which column you need to link the tables and by which conditions.

Any JOIN clause must have the following syntax:

SQLite Join - INNER, NATURAL, LEFT OUTER, CROSS

Each join clause contains:

Note that, for all the following examples, you have to run the sqlite3.exe and open a connection to the sample database as flowing:

Step 1) In this step,

  1. Open My Computer and navigate to the following directory "C:\sqlite" and
  2. Then open "sqlite3.exe":

SQLite Join - INNER, NATURAL, LEFT OUTER, CROSS

Step 2) Open the database "TutorialsSampleDB.db" by the following command:

SQLite Join - INNER, NATURAL, LEFT OUTER, CROSS

Now you are ready to run any type of query on the database.

SQLite INNER JOIN

The INNER JOIN returns only the rows that match the join condition and eliminate all other rows that don't match the join condition.

SQLite Join - INNER, NATURAL, LEFT OUTER, CROSS

Example

In the following example, we will join the two tables "Students" and "Departments" with DepartmentId to get the department name for each student, as follows:

SELECT
  Students.StudentName,
  Departments.DepartmentName
FROM Students
INNER JOIN Departments ON Students.DepartmentId = Departments.DepartmentId;

Explanation of code:

The INNER JOIN works as following:

Output:

SQLite Join - INNER, NATURAL, LEFT OUTER, CROSS

SQLite JOIN … USING

The INNER JOIN can be written using the "USING" clause to avoid redundancy, so instead of writing "ON Students.DepartmentId = Departments.DepartmentId", you can just write "USING(DepartmentID)".

You can use "JOIN .. USING" whenever the columns you will compare in the join condition are the same name. In such cases, there is no need to repeat them using the on condition and just state the column names and SQLite will detect that.

The Difference between the INNER JOIN and JOIN .. USING:

With "JOIN … USING" you don't write a join condition, you just write the join column which is in common between the two joined table, instead of writing table1 "INNER JOIN table2 ON table1.cola = table2.cola" we write it like "table1 JOIN table2 USING(cola)".

Example

In the following example, we will join the two tables "Students" and "Departments" with DepartmentId to get the department name for each student, as follows:

SELECT
  Students.StudentName,
  Departments.DepartmentName
FROM Students
INNER JOIN Departments USING(DepartmentId);

Explanation

Output

SQLite Join - INNER, NATURAL, LEFT OUTER, CROSS

SQLite NATURAL JOIN

A NATURAL JOIN is similar to a JOIN...USING, the difference is that it automatically tests for equality between the values of every column that exists in both tables.

The difference between INNER JOIN and a NATURAL JOIN:

Example

SELECT
  Students.StudentName,
  Departments.DepartmentName
FROM Students
Natural JOIN Departments;

Explanation

Output

SQLite Join - INNER, NATURAL, LEFT OUTER, CROSS

SQLite LEFT OUTER JOIN

The SQL standard defines three types of OUTER JOINs: LEFT, RIGHT, and FULL but SQLite supports only the LEFT OUTER JOIN.

In LEFT OUTER JOIN, all the values of the columns you select from the left table will be included in the result of the query, so regardless of the value matches the join condition or not, it will be included in the result.

So if the left table has 'n' rows, the results of the query will have 'n' rows. However, for the values of the columns coming from the right table, if any value that doesn't match the join condition it will contain a "null" value.

So, you will get a number of rows equivalent to the number of rows in the left join. So that you will get the matching rows from both tables (like the INNER JOIN results), plus the un-matching rows from the left table.

Example

In the following example, we will try the "LEFT JOIN" to join the two tables "Students" and "Departments":

SELECT
  Students.StudentName,
  Departments.DepartmentName
FROM Students             -- this is the left table
LEFT JOIN Departments ON Students.DepartmentId = Departments.DepartmentId;

Explanation

Output

SQLite Join - INNER, NATURAL, LEFT OUTER, CROSS

Let's give the previous query using the left join a deeper explanation using Van diagrams:

SQLite Join - INNER, NATURAL, LEFT OUTER, CROSS

The LEFT JOIN will give all the students names from the students table even if the student has a department id that doesn't exist in the departments table. So, the query won't give you only the matching rows as the INNER JOIN, but will give you the extra part which have the unmatching rows from the left table which is the students table.

Note that any student name that has no matching department will have a "null" value for department name, because there is no matching value for it, and those values are the values in the un-matching rows.

SQLite CROSS JOIN

A CROSS JOIN gives the Cartesian product for the selected columns of the two joined tables, by matching all the values from the first table with all the values from the second table.

So, for every value in the first table, you will get 'n' matches from the second table where n is the number of second table rows.

Unlike INNER JOIN and LEFT OUTER JOIN, with CROSS JOIN, you don't need to specify a join condition, because SQLite doesn't need it for the CROSS JOIN.

The SQLite will result in logical results set by combining all the values from the first table with all the values from the second table.

For example, if you selected a column from the first table (colA) and another column from the second table (colB). The colA contains two value (1,2) and the colB also contains two values (3,4).

Then the result of the CROSS JOIN will be four rows:

Example

In the following query we will try CROSS JOIN between the Students and Departments tables:

SELECT
  Students.StudentName,
  Departments.DepartmentName
FROM Students
CROSS JOIN Departments;

Explanation

Output:

SQLite Join - INNER, NATURAL, LEFT OUTER, CROSS

As you can see, the result is 40 rows; 10 values from the students table matched against the 4 departments from the departments table. As following:

Summary

Using SQLite JOINs, you can link one or more table or subquery together to select columns from both of the tables or subqueries.

 

YOU MIGHT LIKE: