• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Document preview thumbnail
Preview 2 out of 5 pages
Other

SQL Joins: Combining Data from Multiple Tables

Document preview thumbnail
Preview 2 out of 5 pages

This document explains SQL joins, which are used to combine data from multiple tables based on related columns. It covers different types of joins, including INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN, with simple examples to illustrate their use.

Content preview

SQL Joins
1. Introduction to Joins
Joins are used to combine rows from two or more tables in a relational database
based on a related column. A join operation allows you to query data from
multiple tables simultaneously. There are different types of joins, each serving a
specific purpose for handling how rows from different tables are combined.



2. Types of Joins
a. INNER JOIN

 Purpose: The INNER JOIN returns records that have matching values in
both tables. If there’s no match, those rows are excluded from the result.
 Use Case: When you want to get data that exists in both tables.

SQL Example:

SELECT Employees.Name, Departments.Name
FROM Employees
INNER JOIN Departments ON Employees.DepartmentID = Departments.ID;

 This query fetches the names of employees and their corresponding
department names, but only for employees that belong to a department.



b. LEFT JOIN (or LEFT OUTER JOIN)

 Purpose: The LEFT JOIN returns all records from the left table (the first
table in the query) and the matched records from the right table. If there’s
no match, NULL values are returned for columns from the right table.
 Use Case: When you want all records from the left table and only the
matched records from the right table (even if no match exists).

, SQL Example:

SELECT Employees.Name, Departments.Name
FROM Employees
LEFT JOIN Departments ON Employees.DepartmentID = Departments.ID;

 This query returns all employee names and their corresponding department
names, including employees who do not belong to any department (with
NULL for department name).



c. RIGHT JOIN (or RIGHT OUTER JOIN)

 Purpose: The RIGHT JOIN returns all records from the right table (the
second table in the query) and the matched records from the left table. If
there’s no match, NULL values are returned for columns from the left table.
 Use Case: When you want all records from the right table and the matched
records from the left table (even if no match exists).

SQL Example:

SELECT Employees.Name, Departments.Name
FROM Employees
RIGHT JOIN Departments ON Employees.DepartmentID = Departments.ID;

 This query returns all department names and their corresponding employee
names, including departments that do not have any employees (with NULL
for employee names).



d. FULL OUTER JOIN

 Purpose: The FULL OUTER JOIN returns all records when there is a match in
either the left or right table. If there’s no match, the result will contain
NULL values for the missing side.
 Use Case: When you want to retrieve all records from both tables, including
those with no matching counterpart.

Document information

Uploaded on
January 30, 2025
Number of pages
5
Written in
2024/2025
Type
Other
Person
Unknown
$5.39

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Sold
0
Followers
0
Items
252
Last sold
-




Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions