Teaching Kids Programming – Introduction to SQL Join Queries (Inner, Left, Right, Full and Cross Joins)


Teaching Kids Programming: Videos on Data Structures and Algorithms

Previously: Teaching Kids Programming – Introduction to SQL and the SELECT

We can visualize the Different Types of Joins using the Venn Diagrams, for example, the Inner Joins can be seen as finding the common intersecting parts of two tables:

sql-joins-venn-diagrams-inner-join Teaching Kids Programming - Introduction to SQL Join Queries (Inner, Left, Right, Full and Cross Joins) database mysql programming languages sql SQL

sql-joins-venn-diagrams-inner-join

In SQL, joins are used to combine rows from two or more tables based on a related column between them. There are several types of joins. Each type of join serves a different purpose and is used based on the specific requirements of the query.

Let’s consider two tables: employees and departments, and we’ll explore each type of join with these tables.

employees table:

emp_id	emp_name	department_id
1	Alice	        101
2	Bob	        102
3	Charlie	        101
4	David	        NULL

departments table:

department_id	department_name
101	        Sales
102	        Marketing
103	        HR

SQL Inner Join

INNER JOIN: This type of join returns only the rows where there is a match in both tables based on the join condition.

FROM table1
INNER JOIN table2 ON table1.column = table2.column;

INNER JOIN: This will return only the rows where there’s a match in both tables.

SELECT employees.emp_id, employees.emp_name, departments.department_name
FROM employees
INNER JOIN departments ON employees.department_id = departments.department_id;

Output:

emp_id	emp_name	department_name
1	Alice	        Sales
2	Bob	        Marketing
3	Charlie	        Sales

We can also add the where conditions and here are the examples of adding the where statement:

Let’s suppose we want to retrieve employees who belong to the Sales department:

-- INNER JOIN with WHERE clause
SELECT employees.emp_id, employees.emp_name, departments.department_name
FROM employees
INNER JOIN departments ON employees.department_id = departments.department_id
WHERE departments.department_name = 'Sales';

Output:

emp_id	emp_name	department_name
1	Alice	        Sales
3	Charlie	        Sales

Implicit SQL Inner Join

In most SQL database systems, including MySQL, PostgreSQL, and SQL Server, when you use the keyword JOIN without specifying whether it’s an INNER JOIN, it defaults to an INNER JOIN.

So, in practice, writing INNER JOIN or just JOIN would produce the same result.

Here’s an example:

SELECT *
FROM table1
INNER JOIN table2 ON table1.column = table2.column;

This is equivalent to:

SELECT *
FROM table1
JOIN table2 ON table1.column = table2.column;

Both of these queries will perform an inner join between table1 and table2. However, some database systems might treat JOIN differently (such as SQLite). It’s always a good practice to specify INNER JOIN explicitly for clarity and to avoid any potential confusion.

Differences Between Explicit and Implicit Inner Joins

There is a subtle but important difference between using an INNER JOIN and using a comma in the FROM clause with a WHERE condition to specify the join.

Using INNER JOIN

When you explicitly use INNER JOIN, your intent is clear: you are joining two tables based on a specific condition.

SELECT * 
FROM A
INNER JOIN B ON A.id = B.id;
Using Comma and WHERE

Using a comma in the FROM clause with a WHERE condition is an older style of SQL join syntax known as an implicit join. This style can sometimes be less clear, especially in more complex queries.

SELECT * 
FROM A, B 
WHERE A.id = B.id;
Differences and Considerations

Readability and Intent:

  • Explicit JOIN (recommended): Makes the join operation and its conditions clear, enhancing readability and maintainability.
  • Implicit JOIN: Can be less clear, especially for those unfamiliar with this syntax, and can make the query harder to read and understand.

Complex Queries:

  • Explicit JOIN: Easier to manage when dealing with multiple joins and complex conditions.
  • Implicit JOIN: Can become confusing when multiple tables are joined or when using additional conditions in the WHERE clause.

SQL Standards:

  • Explicit JOIN: Conforms to modern SQL standards and is preferred in SQL Server, MySQL, PostgreSQL, and other relational databases.
  • Implicit JOIN: Supported for backward compatibility but is considered outdated.
Example Comparisons

Explicit INNER JOIN:

SELECT A.*, B.*
FROM A
INNER JOIN B ON A.id = B.id
WHERE A.some_column = 'some_value';

Implicit Join with Comma:

SELECT A.*, B.*
FROM A, B
WHERE A.id = B.id
AND A.some_column = 'some_value';

Both queries will return the same result set, but the explicit join syntax is clearer and less prone to errors, especially as the complexity of the query increases. While both approaches achieve the same result, using INNER JOIN is the preferred and more modern approach. It clearly indicates that a join operation is being performed and improves the readability and maintainability of the SQL code.

SQL LEFT JOIN (or LEFT OUTER JOIN)

This join returns all rows from the left table, and the matched rows from the right table. If there is no match, NULL values are returned for the columns from the right table.

SELECT * 
FROM table1
LEFT JOIN table2 ON table1.column = table2.column;

LEFT JOIN: This will return all rows from the left table (employees), and the matched rows from the right table (departments).

SELECT employees.emp_id, employees.emp_name, departments.department_name
FROM employees
LEFT JOIN departments ON employees.department_id = departments.department_id;

Output:

emp_id	emp_name	department_name
1	Alice	        Sales
2	Bob	        Marketing
3	Charlie	        Sales
4	David	         NULL

Now, let’s say we want to retrieve all employees and their departments except those in the HR department:

-- LEFT JOIN with WHERE clause
SELECT employees.emp_id, employees.emp_name, departments.department_name
FROM employees
LEFT JOIN departments ON employees.department_id = departments.department_id
WHERE departments.department_name <> 'HR' OR departments.department_name IS NULL;

Output:

emp_id	emp_name	department_name
1	Alice	        Sales
2	Bob	        Marketing
3	Charlie	        Sales
4	David	        NULL

In this example, we’re using the LEFT JOIN to ensure all employees are included, even if they don’t have a department assigned (David in this case). Then, we use a WHERE clause to exclude the HR department.

These examples demonstrate how you can combine joins with WHERE clauses to filter the results based on specific conditions.

SQL RIGHT JOIN (or RIGHT OUTER JOIN)

This join returns all rows from the right table, and the matched rows from the left table. If there is no match, NULL values are returned for the columns from the left table.

SELECT * 
FROM table1
RIGHT JOIN table2 ON table1.column = table2.column;

RIGHT JOIN: This will return all rows from the right table (departments), and the matched rows from the left table (employees).

SELECT employees.emp_id, employees.emp_name, departments.department_name
FROM employees
RIGHT JOIN departments ON employees.department_id = departments.department_id;

Output:

emp_id	emp_name	department_name
1	Alice	        Sales
2	Bob	        Marketing
3	Charlie	        Sales
NULL	NULL	        HR

SQL FULL JOIN (or FULL OUTER JOIN)

This join returns all rows when there is a match in either left or right table. If there is no match, NULL values are returned for columns from the table with no matching row.

SELECT * 
FROM table1
FULL JOIN table2 ON table1.column = table2.column;

FULL JOIN: This will return all rows when there is a match in either left or right table.

SELECT employees.emp_id, employees.emp_name, departments.department_name
FROM employees
FULL JOIN departments ON employees.department_id = departments.department_id;

Output:

emp_id	emp_name	department_name
1	Alice	        Sales
2	Bob	        Marketing
3	Charlie	        Sales
4	David	        NULL
NULL	NULL	        HR

SQL CROSS JOIN

This join returns the Cartesian product of the two tables, meaning it returns all possible combinations of rows from both tables.

SELECT * 
FROM table1
CROSS JOIN table2;

CROSS JOIN: This will return the Cartesian product of the two tables.

SELECT employees.emp_id, employees.emp_name, departments.department_name
FROM employees
CROSS JOIN departments;

Output:

emp_id	emp_name	department_name
1	Alice	        Sales
1	Alice	        Marketing
1	Alice	        HR
2	Bob	        Sales
2	Bob	        Marketing
2	Bob	        HR
3	Charlie	        Sales
3	Charlie	        Marketing
3	Charlie	        HR
4	David	        Sales
4	David	        Marketing
4	David	        HR

These examples demonstrate how each type of join works and how it affects the resulting dataset.

Cross Join on Multiple Tables

Yu can perform a CROSS JOIN on multiple tables in SQL. A CROSS JOIN returns the Cartesian product of the tables involved, meaning it returns all possible combinations of rows from the tables.

Here is an example involving three tables:

products table:

product_id	product_name
1	        Laptop
2	        Phone

customers table:

customer_id	customer_name
1	        Alice
2	        Bob

stores table:

store_id	store_name
1	        Store A
2	        Store B

CROSS JOIN on Multiple Tables

SELECT *
FROM products
CROSS JOIN customers
CROSS JOIN stores;

The result will include every possible combination of rows from the products, customers, and stores tables:

product_id	product_name	customer_id	customer_name	store_id	store_name
1	Laptop	1	Alice	1	Store A
1	Laptop	1	Alice	2	Store B
1	Laptop	2	Bob	1	Store A
1	Laptop	2	Bob	2	Store B
2	Phone	1	Alice	1	Store A
2	Phone	1	Alice	2	Store B
2	Phone	2	Bob	1	Store A
2	Phone	2	Bob	2	Store B

Each row in the products table is combined with each row in the customers table and each row in the stores table, resulting in the Cartesian product.

Key Points

  • Usage: CROSS JOINs can generate a large number of rows, so they should be used carefully, especially with large tables.
  • Performance: The number of rows in the result set is the product of the number of rows in each table. For example, if products has 2 rows, customers has 2 rows, and stores has 2 rows, the result will have 2^3 = 8 rows.
  • Applications: CROSS JOINs are useful when you need to combine all possible pairs of rows from two or more tables. For example, generating test data, permutations, or scenarios where all combinations need to be evaluated.

Using Aliases with CROSS JOIN

You can also use table aliases to simplify and clarify your query:

SELECT p.product_name, c.customer_name, s.store_name
FROM products p
CROSS JOIN customers c
CROSS JOIN stores s;

This query will produce the same result as the previous example but uses aliases (p, c, s) to make the query more concise and readable.

SQL Table Alias

Table aliases can make SQL queries more readable, especially when dealing with complex queries involving multiple tables. Here’s an example of using table aliases with a join:

SELECT e.emp_id, e.emp_name, d.department_name
FROM employees e
JOIN departments d ON e.department_id = d.department_id;

In this example:
e is an alias for the employees table.
d is an alias for the departments table.

Using aliases helps to simplify the query, especially when the table names are long or when you’re referencing the same table multiple times in a query.

You can also use aliases with other types of joins and include them in the WHERE clause or any other part of the query.

In SQL, you can use table aliases without explicitly using the AS keyword. So, employees e is equivalent to employees AS e, and departments d is equivalent to departments AS d.

Using aliases without AS is a common shorthand convention in SQL, making queries more concise and readable.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
2373 words
Last Post: The "defer" keyword in GoLang
Next Post: Introduction to Crypto Defi: Flexi Max (HTX)

The Permanent URL is: Teaching Kids Programming – Introduction to SQL Join Queries (Inner, Left, Right, Full and Cross Joins)

Leave a Reply