- 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
- JavaScript Interview Questions for 5+ Years Experience
- JavaScript Interview Questions for 2–5 Years Experience
- JavaScript Interview Questions for 1–2 Years Experience
- JavaScript Interview Questions for 0–1 Year Experience
- JavaScript Interview Questions For Fresher
- SQL Interview Questions for 5+ Years Experience
- 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
Random Blogs
- Top 10 Blogs of Digital Marketing you Must Follow
- The Beginner’s Guide to Normalization and Denormalization in Databases
- Python Challenging Programming Exercises Part 1
- SQL Joins Explained: A Complete Guide with Examples
- The Ultimate Guide to Machine Learning (ML) for Beginners
- How Multimodal Generative AI Will Change Content Creation Forever
- What to Do When Your MySQL Table Grows Too Wide
- Quantum AI – The Future of AI Powered by Quantum Computing
- Understanding LLMs (Large Language Models): The Ultimate Guide for 2025
- 10 Awesome Data Science Blogs To Check Out
- Datasets for Speech Recognition Analysis
- AI Agents: The Future of Automation, Work, and Opportunities in 2025
- Datasets for analyze in Tableau
- Navigating AI Careers in 2025: Data Science, Machine Learning, Deep Learning, and More
- Mastering SQL in 2025: A Complete Roadmap for Beginners
Datasets for Machine Learning
- Awesome-ChatGPT-Prompts
- 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