🌟 Day 4: Mastering SQL – Joining Tables 🌟
Hey everyone! Welcome to Day 4 of our SQL series. We’ve covered the basics and learned how to filter and sort data. Today, we’re diving into one of the most powerful features of SQL—joining tables. This allows you to combine data from multiple tables to gain deeper insights.
Understanding SQL Joins
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 serving a different purpose.
1. INNER JOIN
The INNER JOIN keyword selects records that have matching values in both tables.
Example
:
Combine “Employees” and “Departments” tables where the Department ID matches
SELECT Employees.Name, Departments.DepartmentName
FROM Employees
INNER JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
2. LEFT JOIN (or LEFT OUTER JOIN)
The LEFT JOIN keyword returns all records from the left table, and the matched records from the right table. If there is no match, the result is NULL from the right table.
Example:
Retrieve all employees and their departments, even if some employees are not assigned to a department
SELECT Employees.Name, Departments.DepartmentName
FROM Employees
LEFT JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
3. RIGHT JOIN (or RIGHT OUTER JOIN)
The RIGHT JOIN keyword returns all records from the right table, and the matched records from the left table. If there is no match, the result is NULL from the left table.
Example:
Retrieve all departments and their employees, even if some departments have no employees
SELECT Employees.Name, Departments.DepartmentName
FROM Employees
RIGHT JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
4. FULL JOIN (or FULL OUTER JOIN)
The FULL JOIN keyword returns all records when there is a match in either left or right table. If there is no match, the result is NULL for missing matches in either table.
Example:
Combine all employees and departments, showing employees with no departments and departments with no employees
SELECT Employees.Name, Departments.DepartmentName
FROM Employees
FULL JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
Practice Challenge
Try these challenges to test your skills:
1. Use an INNER JOIN to find all employees and their department names.
2. Use a LEFT JOIN to list all employees, including those not assigned to any department.
3. Use a FULL JOIN to combine all employees and departments, displaying all possible matches.
That’s it for today! Tomorrow, we’ll explore subqueries and nested queries to perform more complex data retrieval. 💻✨
Got any questions or examples you’d like to share? Drop them in the comments. Your feedback is always appreciated! 😊🚀
Looking forward to seeing your progress! 🚀✨
Hey everyone! Welcome to Day 4 of our SQL series. We’ve covered the basics and learned how to filter and sort data. Today, we’re diving into one of the most powerful features of SQL—joining tables. This allows you to combine data from multiple tables to gain deeper insights.
Understanding SQL Joins
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 serving a different purpose.
1. INNER JOIN
The INNER JOIN keyword selects records that have matching values in both tables.
Example
:
Combine “Employees” and “Departments” tables where the Department ID matches
SELECT Employees.Name, Departments.DepartmentName
FROM Employees
INNER JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
2. LEFT JOIN (or LEFT OUTER JOIN)
The LEFT JOIN keyword returns all records from the left table, and the matched records from the right table. If there is no match, the result is NULL from the right table.
Example:
Retrieve all employees and their departments, even if some employees are not assigned to a department
SELECT Employees.Name, Departments.DepartmentName
FROM Employees
LEFT JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
3. RIGHT JOIN (or RIGHT OUTER JOIN)
The RIGHT JOIN keyword returns all records from the right table, and the matched records from the left table. If there is no match, the result is NULL from the left table.
Example:
Retrieve all departments and their employees, even if some departments have no employees
SELECT Employees.Name, Departments.DepartmentName
FROM Employees
RIGHT JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
4. FULL JOIN (or FULL OUTER JOIN)
The FULL JOIN keyword returns all records when there is a match in either left or right table. If there is no match, the result is NULL for missing matches in either table.
Example:
Combine all employees and departments, showing employees with no departments and departments with no employees
SELECT Employees.Name, Departments.DepartmentName
FROM Employees
FULL JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
Practice Challenge
Try these challenges to test your skills:
1. Use an INNER JOIN to find all employees and their department names.
2. Use a LEFT JOIN to list all employees, including those not assigned to any department.
3. Use a FULL JOIN to combine all employees and departments, displaying all possible matches.
That’s it for today! Tomorrow, we’ll explore subqueries and nested queries to perform more complex data retrieval. 💻✨
Got any questions or examples you’d like to share? Drop them in the comments. Your feedback is always appreciated! 😊🚀
Looking forward to seeing your progress! 🚀✨