SQLite Join
SQLite's Join The JOIN clause is used to combine records from two or more tables in a database. JOIN is a means of combining fields from two tables by using common values.
SQL defines three main types of joins:
-
Cross join - CROSS JOIN
-
Inner join - INNER JOIN
-
Outer join - OUTER JOIN
Before we continue, let's assume there are two tables, COMPANY and DEPARTMENT. We have seen the INSERT statements used to populate the COMPANY table. Now let's assume the list of records in the COMPANY table is as follows:
ID NAME AGE ADDRESS SALARY
---------- ---------- ---------- ---------- ----------
1 Paul 32 California 20000.0
2 Allen 25 Texas 15000.0
3 Teddy 23 Norway 20000.0
4 Mark 25 Rich-Mond 65000.0
5 David 27 Texas 85000.0
6 Kim 22 South-Hall 45000.0
7 James 24 Houston 10000.0
The other table is DEPARTMENT, defined as follows:
CREATE TABLE DEPARTMENT(
ID INT PRIMARY KEY NOT NULL,
DEPT CHAR(50) NOT NULL,
EMP_ID INT NOT NULL
);
The following is the INSERT statement to populate the DEPARTMENT table:
INSERT INTO DEPARTMENT (ID, DEPT, EMP_ID)
VALUES (1, 'IT Billing', 1 );
INSERT INTO DEPARTMENT (ID, DEPT, EMP_ID)
VALUES (2, 'Engineering', 2 );
INSERT INTO DEPARTMENT (ID, DEPT, EMP_ID)
VALUES (3, 'Finance', 7 );
Finally, we have the following list of records in the DEPARTMENT table:
ID DEPT EMP_ID
---------- ---------- ----------
1 IT Billing 1
2 Engineerin 2
3 Finance 7
Cross join - CROSS JOIN
A cross join (CROSS JOIN) matches each row of the first table with each row of the second table. If the two input tables have x and y rows respectively, the result table has x*y rows. Since a cross join (CROSS JOIN) can produce very large tables, it must be used with caution, and only when appropriate.
The operations of a cross join all return the Cartesian product of all data rows of the two joined tables; the number of data rows returned equals the number of rows in the first table that meet the query condition multiplied by the number of rows in the second table that meet the query condition.
The following is the syntax of a cross join (CROSS JOIN):
SELECT ... FROM table1 CROSS JOIN table2 ...
Based on the tables above, we can write a CROSS JOIN as follows:
sqlite> SELECT EMP_ID, NAME, DEPT FROM COMPANY CROSS JOIN DEPARTMENT;
The above query will produce the following result:
EMP_ID NAME DEPT
---------- ---------- ----------
1 Paul IT Billing
2 Paul Engineerin
7 Paul Finance
1 Allen IT Billing
2 Allen Engineerin
7 Allen Finance
1 Teddy IT Billing
2 Teddy Engineerin
7 Teddy Finance
1 Mark IT Billing
2 Mark Engineerin
7 Mark Finance
1 David IT Billing
2 David Engineerin
7 David Finance
1 Kim IT Billing
2 Kim Engineerin
7 Kim Finance
1 James IT Billing
2 James Engineerin
7 James Finance
Inner join - INNER JOIN
An inner join (INNER JOIN) creates a new result table by combining the column values of two tables (table1 and table2) based on the join predicate. The query compares each row of table1 with each row of table2 to find all matching pairs of rows that satisfy the join predicate. When the join predicate is satisfied, the column values of each matching pair of rows A and B are combined into a single result row.
The inner join (INNER JOIN) is the most common type of join and is the default join type. The INNER keyword is optional.
The following is the syntax of an INNER JOIN:
SELECT ... FROM table1 [INNER] JOIN table2 ON conditional_expression ...
To avoid redundancy and keep the wording shorter, you can use the USING expression to declare the inner join (INNER JOIN) condition. This expression specifies a list of one or more columns:
SELECT ... FROM table1 JOIN table2 USING ( column1 ,... ) ...
A natural join (NATURAL JOIN) is similar to JOIN...USING, except that it automatically tests for equality between the values of each column that exists in both tables:
SELECT ... FROM table1 NATURAL JOIN table2...
Based on the above tables, we can write an inner join (INNER JOIN), as follows:
sqlite> SELECT EMP_ID, NAME, DEPT FROM COMPANY INNER JOIN DEPARTMENT
ON COMPANY.ID = DEPARTMENT.EMP_ID;
The above query will produce the following result:
EMP_ID NAME DEPT
---------- ---------- ----------
1 Paul IT Billing
2 Allen Engineerin
7 James Finance
Outer join - OUTER JOIN
An outer join (OUTER JOIN) is an extension of the inner join (INNER JOIN). Although the SQL standard defines three types of outer joins: LEFT, RIGHT, and FULL, SQLite only supports left outer join (LEFT OUTER JOIN)。
The way an outer join (OUTER JOIN) declares conditions is the same as an inner join (INNER JOIN), expressed using the ON, USING, or NATURAL keywords. The initial result table is computed in the same way. Once the main join computation is complete, the outer join (OUTER JOIN) merges in any unmatched rows from one or both tables, with NULL values for the outer join columns, appending them to the result table.
The following is the syntax of a left outer join (LEFT OUTER JOIN):
SELECT ... FROM table1 LEFT OUTER JOIN table2 ON conditional_expression ...
To avoid redundancy and keep the wording shorter, you can use the USING expression declares the outer join (OUTER JOIN) condition. This expression specifies a list of one or more columns:
SELECT ... FROM table1 LEFT OUTER JOIN table2 USING ( column1 ,... ) ...
Based on the above tables, we can write an outer join (OUTER JOIN), as follows:
sqlite> SELECT EMP_ID, NAME, DEPT FROM COMPANY LEFT OUTER JOIN DEPARTMENT
ON COMPANY.ID = DEPARTMENT.EMP_ID;
The above query will produce the following result:
EMP_ID NAME DEPT
---------- ---------- ----------
1 Paul IT Billing
2 Allen Engineerin
Teddy
Mark
David
Kim
7 James Finance