Uploaded by Hooman Erfaghi

SQL Join

advertisement
Joining Tables in SQL
What is a join?
A join is a way to combine data from two or more tables in a database. This is useful when
you need to get information from multiple tables in a single query.
How does a join work?
Joins work by matching rows in two tables based on a common column. For example, you
could join a table of customers with a table of orders based on the customer ID column. This
would allow you to get information about both the customers and their orders in a single
query.
What are the different types of joins?
There are four main types of joins:





INNER JOIN: Returns only the rows that match on the specified column in both tables.
LEFT JOIN: Returns all rows from the left table, even if there is no match in the right
table.
RIGHT JOIN: Returns all rows from the right table, even if there is no match in the left
table.
FULL JOIN: Returns all rows from both tables, even if there is no match in either table.
How do I use a join?
To use a join, you use the JOIN keyword followed by the name of the second table. You then
specify the column that the two tables have in common using the following syntax:
table1.column_name = table2.column_name
For example, the following query uses an INNER JOIN to combine data from
the customers and orders tables:
SELECT *
FROM customers
INNER JOIN orders
ON customers.customer_id = orders.customer_id;
This query will return a table with all of the columns from both
the customers and orders tables. The table will contain only the rows that have a
matching customer_id in both tables.
Why would I use a join?
Joins are useful for getting information from multiple tables in a single query. This can be
helpful for tasks such as:



Getting a list of customers and their orders
Getting a list of products and their prices
Getting a list of employees and their salaries
Joins are a powerful tool that can be used to get a lot of information from a database.
Joining tables in SQL is a way to combine data from two or more tables based on a common
column. This is useful when you need to get information from multiple tables in a database.
Syntax
To join two tables, you use the JOIN keyword followed by the name of the second table. You
then specify the column that the two tables have in common using the following syntax:
table1.column_name = table2.column_name
Types of Joins
There are different types of joins, including:




INNER JOIN: Returns only the rows that match on the specified column in both tables.
LEFT JOIN: Returns all rows from the left table, even if there is no match in the right
table.
RIGHT JOIN: Returns all rows from the right table, even if there is no match in the left
table.
FULL JOIN: Returns all rows from both tables, even if there is no match in either table.
Example
The following query uses an INNER JOIN to combine data from
the employees and machines tables:
SELECT username, office, operating_system
FROM employees
INNER JOIN machines
ON employees.employee_id = machines.employee_id;
This query will return a table with the following columns:



username
office
operating_system
The table will contain only the rows that have a matching employee_id in both
the employees and machines tables.
Outer Joins
Outer joins are a type of join that returns all of the records from one or both of the tables
being joined, even if there is no matching value in the other table. This is in contrast to inner
joins, which only return records that have a matching value in both tables.
There are three types of outer joins:



LEFT JOIN returns all of the records from the left table (the first table in the join), and any
matching records from the right table (the second table in the join). If there is no matching
record in the right table, the columns from the right table will contain NULL values.
RIGHT JOIN returns all of the records from the right table, and any matching records
from the left table. If there is no matching record in the left table, the columns from the left
table will contain NULL values.
FULL OUTER JOIN returns all of the records from both the left and right tables,
regardless of whether there is a matching value in the other table. If there is no matching
value, the columns from the missing table will contain NULL values.
Outer joins are useful when you need to return all of the records from one or both of the
tables being joined, even if there is no matching value in the other table. For example, you
could use a left join to return all of the employees in a company, even if they do not have a
manager.
Here is an example of a left join in SQL:
SELECT *
FROM employees
LEFT JOIN managers
ON employees.manager_id = managers.id;
This query will return all of the employees in the company, even if they do not have a
manager. The manager_id column will contain NULL values for employees who do not have
a manager.
I hope this simplified explanation is helpful. Please let me know if you have any other
questions.
Types of Outer Joins:



LEFT JOIN: Returns all records from the left table and matching records from the right
table.
RIGHT JOIN: Returns all records from the right table and matching records from the left
table.
FULL OUTER JOIN: Returns all records from both tables, including unmatched records.
Syntax:
SELECT *
FROM table1
LEFT JOIN table2 ON table1.column = table2.column;
SELECT *
FROM table1
RIGHT JOIN table2 ON table1.column = table2.column;
SELECT *
FROM table1
FULL OUTER JOIN table2 ON table1.column = table2.column;
Example:
Consider the following tables:
Employees Machines
employee_idmachine_id
1190
1
1191
2
1192
3
LEFT JOIN:
SELECT *
FROM Employees
LEFT JOIN Machines ON Employees.employee_id = Machines.employee_id;
Result:
employee_idmachine_id
1190
NULL
1191
2
1192
3
RIGHT JOIN:
SELECT *
FROM Employees
RIGHT JOIN Machines ON Employees.employee_id = Machines.employee_id;
Result:
employee_idmachine_id
NULL
1
1191
2
1192
3
FULL OUTER JOIN:
SELECT *
FROM Employees
FULL OUTER JOIN Machines ON Employees.employee_id = Machines.employee_id;
Result:
employee_idmachine_id
1190
NULL
1191
2
1192
3
NULL
1
Download