- SQL Basics
-
Overview
- Introduction to Databases and SQL
- Creating and Managing Tables in SQL
- Data Types and Constraints
- SELECT Queries and Filtering Data
- GROUP BY, HAVING, and ORDER BY
- SQL Joins (INNER, LEFT, RIGHT, FULL)
- Subqueries and Nested Queries
- UNION, INTERSECT, and EXCEPT
- Common Table Expressions (CTE)
- SQL Views and Stored Procedures
SQL Joins (INNER, LEFT, RIGHT, FULL)
Add to BookmarkIntroduction
Joins in SQL allow us to combine data from multiple tables based on a related column. By using joins, we can efficiently retrieve relevant data from relational databases. SQL supports several types of joins, each serving different use cases.
Types of SQL Joins
1. INNER JOIN
An INNER JOIN returns only the matching records from both tables based on the specified condition. If there is no match, the rows are excluded from the result.
Syntax:
SELECT columns
FROM table1
INNER JOIN table2
ON table1.common_column = table2.common_column;
Example:
SELECT employees.name, departments.department_name
FROM employees
INNER JOIN departments
ON employees.department_id = departments.department_id;
This query retrieves employees and their respective departments where a match exists.
2. LEFT JOIN (LEFT OUTER JOIN)
A LEFT JOIN returns all records from the left table and only matching records from the right table. If there is no match, NULL values are returned for columns from the right table.
Syntax:
SELECT columns
FROM table1
LEFT JOIN table2
ON table1.common_column = table2.common_column;
Example:
SELECT students.name, enrollments.course_id
FROM students
LEFT JOIN enrollments
ON students.student_id = enrollments.student_id;
This query retrieves all students, including those who haven't enrolled in any course.
3. RIGHT JOIN (RIGHT OUTER JOIN)
A RIGHT JOIN is similar to a LEFT JOIN but retrieves all records from the right table and only matching records from the left table. If there is no match, NULL values appear for columns from the left table.
Syntax:
SELECT columns
FROM table1
RIGHT JOIN table2
ON table1.common_column = table2.common_column;
Example:
SELECT employees.name, projects.project_name
FROM employees
RIGHT JOIN projects
ON employees.employee_id = projects.employee_id;
This query retrieves all projects, including those that are not assigned to any employee.
4. FULL JOIN (FULL OUTER JOIN)
A FULL JOIN returns all records when there is a match in either the left or the right table. If there is no match, NULL values appear for missing columns from either table.
Syntax:
SELECT columns
FROM table1
FULL JOIN table2
ON table1.common_column = table2.common_column;
Example:
SELECT students.name, courses.course_name
FROM students
FULL JOIN courses
ON students.course_id = courses.course_id;
This query retrieves all students and courses, including students who haven't enrolled in any course and courses that have no students enrolled.
Choosing the Right Join
Join Type | Use Case |
---|---|
INNER JOIN | When you only need matching records from both tables. |
LEFT JOIN | When you need all records from the left table and matching records from the right table. |
RIGHT JOIN | When you need all records from the right table and matching records from the left table. |
FULL JOIN | When you need all records from both tables, even if there are no matches. |
Conclusion
Understanding SQL joins is essential for working with relational databases. By using INNER, LEFT, RIGHT, and FULL JOINs, you can efficiently retrieve related data from multiple tables based on specific conditions. Choosing the right type of join depends on the structure of your data and the results you need.
Prepare for Interview
- SQL Interview Questions for 2–5 Years Experience
- SQL Interview Questions for 1–2 Years Experience
- SQL Interview Questions for 0–1 Year Experience
- SQL Interview Questions for Freshers
- Design Patterns in Python
- Dynamic Programming and Recursion in Python
- Trees and Graphs in Python
- Linked Lists, Stacks, and Queues in Python
- Sorting and Searching in Python
- Debugging in Python
- Unit Testing in Python
- Asynchronous Programming in PYthon
- Multithreading and Multiprocessing in Python
- Context Managers in Python
- Decorators in Python
Random Blogs
- Understanding HTAP Databases: Bridging Transactions and Analytics
- Ideas for Content of Every niche on Reader’s Demand during COVID-19
- Variable Assignment in Python
- Why to learn Digital Marketing?
- Store Data Into CSV File Using Python Tkinter GUI Library
- Understanding AI, ML, Data Science, and More: A Beginner's Guide to Choosing Your Career Path
- Top 10 Knowledge for Machine Learning & Data Science Students
- Datasets for Exploratory Data Analysis for Beginners
- Top 10 Blogs of Digital Marketing you Must Follow
- Career Guide: Natural Language Processing (NLP)
- Robotics & AI – How AI is Powering Modern Robotics
- Extract RGB Color From a Image Using CV2
- 15 Amazing Keyword Research Tools You Should Explore
- The Ultimate Guide to Machine Learning (ML) for Beginners
- Types of Numbers in Python
Datasets for Machine Learning
- Amazon Product Reviews Dataset
- Ozone Level Detection Dataset
- Bank Transaction Fraud Detection
- YouTube Trending Video Dataset (updated daily)
- Covid-19 Case Surveillance Public Use Dataset
- US Election 2020
- Forest Fires Dataset
- Mobile Robots Dataset
- Safety Helmet Detection
- All Space Missions from 1957
- OSIC Pulmonary Fibrosis Progression Dataset
- Wine Quality Dataset
- Google Audio Dataset
- Iris flower dataset
- Artificial Characters Dataset