VOOZH about

URL: https://www.analyticsvidhya.com/blog/2020/02/understanding-sql-joins/

โ‡ฑ Types of SQL Joins - Analytics Vidhya


India's Most Futuristic AI Conference Is Back โ€“ Bigger, Sharper, Bolder

  • d
  • :
  • h
  • :
  • m
  • :
  • s

Types of SQL Joins

Alakh Sethi Last Updated : 10 Feb, 2025
6 min read

SQL joins might appear daunting, especially for newcomers. If programming is unfamiliar, comprehending SQL joins and the types of SQL joins could be challenging. Yet, as a data science enthusiast or practitioner, understanding these joins is crucial. They empower efficient data retrieval and manipulation across tables. This article simplifies SQL joinsโ€™ learning curve, unveiling their simplicity. In this article you will get to know all about the SQL Join and types of SQL joins.

What are SQL Joins?

SQL Joins are used to combine rows from two or more tables based on a related column between them, allowing retrieval of data from multiple tables in a single query. Common types include INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN.

Also Read: Top 10 SQL Projects for Data Analysis

Different Types Of Joins in SQL

  • INNER JOIN
  • RIGHT JOIN
  • LEFT JOIN
  • FULL JOIN
  • Natural Join

Want to learn the basics of what SQL is and how it can be applied in data science? Check out the popular course SQL for Data Science.

SQL INNER JOIN

Returns rows only when there is a match in both tables, based on a common column. If there are records in one table that do not have matches in the other table, there would be no result set

This is what we covered in the above section. Inner Join returns records that have matching values in both tables:

Letโ€™s see what the output is using the above example:

SELECT * FROM BOYS INNER JOIN MESSI
ON BOYS.id = MESSI.id;

Output:

The inner join gives the intersection of two tables, i.e. rows which are common in both the tables.

Right or Outer in SQL

Returns all rows from the right table, and the matching rows from the left table. If there is no match in the left table, NULL values are returned for the columns of the left table

Suppose we want ID and Name of all the people who love watching Messi play. Obviously, there are many ways of writing this query but weโ€™ll understand with the help of joins.

Letโ€™s see what the output is:

SELECT  *  FROM BOYS RIGHT JOIN MESSI 
ON BOYS.id = MESSI.id;

Output:

Can you figure out what happened here? The right outer join gives us the rows that are common in both the tables as well as extra rows from the Messi table which are not present in the intersection. In other words, a right join returns all records from the right table and the matched records from the left table.

Left Join in SQL

Returns all rows from the left table, and the matching rows from the right table. If there is no match in the right table, NULL values are returned for the columns of the right table

Letโ€™s say we want the list of all the boys who love watching Messi play as well as not love watching Messi play using joins.

I want you to guess the final output before you read further.

SELECT FROM BOYS LEFT JOIN MESSI
ON BOYS.id = MESSI.id;

OUTPUT:

The left outer join gives us the rows that are common in both the tables as well as extra rows from the Boys table which are not present in the intersection. In other words, a left join returns all records from the left table and the matched records from the right table.

Full Join in SQL

Combines the results of both left and right outer joins. The joined table will contain all records from both the tables and fill in NULLs for missing matches on either side

Finally, letโ€™s say we want the list of all the people, including boys who love watching Messi play.

SELECT FROM BOYS FULL OUTER JOIN MESSI
ON BOYS.id = MESSI.id;

Output:

Perfect! A full outer join gives us the rows that are common in both the tables as well as extra rows from both tables which are not present in the intersection. We get all records when there is a match on either the left or the right table.

NATURAL JOIN

Matches columns with the same name in both tables. The common columns on which the tables are joined must have the same name and data type across the tables.

Assuming both tables have a column named CustomerID with the same data type:

SELECT *
FROM Customers
NATURAL JOIN Orders;

Output:

CustomerIDCustomerNameCityOrderIDOrderDate
1JohnNew York1012024-01-20
1JohnNew York1032024-03-10
2AliceLondon1022024-02-15

When to Use What?

Situation: Youโ€™re managing a database for an online store, and you want to analyze sales data along with customer information. The goal is to identify which products are popular among different customer segments.

Explanation (INNER JOIN) 

Use an INNER JOIN when you want to match records that exist in both tables. In this scenario, youโ€™ll retrieve sales data along with customer information for customers who have made purchases.

Example:

SELECT customers.name, products.product_name, sales.quantity

FROM customers

INNER JOIN sales ON customers.id = sales.customer_id

INNER JOIN products ON sales.product_id = products.id;

Explanation (LEFT JOIN) 

Use a LEFT JOIN when you want to retrieve all records from the left table and matching records from the right. This is useful to see which products have been purchased and by whom, including customers who havenโ€™t made any purchases.

Example:

SELECT customers.name, products.product_name, sales.quantity

FROM customers

LEFT JOIN sales ON customers.id = sales.customer_id

LEFT JOIN products ON sales.product_id = products.id;

Explanation (RIGHT JOIN) 

Use a RIGHT JOIN when you want to retrieve all records from the right table and matching records from the left. This can be helpful to identify products that have been purchased and by whom, including products that havenโ€™t been purchased by any customer.

Example:

SELECT customers.name, products.product_name, sales.quantity

FROM customers

RIGHT JOIN sales ON customers.id = sales.customer_id

RIGHT JOIN products ON sales.product_id = products.id;

Explanation (FULL JOIN)

Use a FULL JOIN when you want to retrieve all records from both tables, including products that have been purchased and by whom, and also customers who havenโ€™t made any purchases.

Example:

SELECT customers.name, products.product_name, sales.quantity

FROM customers

FULL JOIN sales ON customers.id = sales.customer_id

FULL JOIN products ON sales.product_id = products.id;

Select the appropriate join type based on your analysis goals and the relationship between tables to obtain the desired insights from your data.

Conclusion

In conclusion, mastering SQL joins opens the door to powerful data manipulation and analysis capabilities. Whether youโ€™re a budding data enthusiast or a seasoned analyst, understanding the nuances of INNER, LEFT, RIGHT, and FULL joins empowers you to extract valuable insights from complex relational databases. By seamlessly combining data from multiple tables, you gain the ability to unravel intricate relationships, spot trends, and make informed decisions. Embrace the flexibility of SQL joins to efficiently explore your dataโ€™s potential, turning raw information into actionable knowledge. With this fundamental skill in your toolkit, youโ€™re well-equipped to navigate the intricate landscape of database management and elevate your data-driven journey.

Do you want to learn how SQL can be used in data science? We highly recommend checking out this amazing course โ€“ Structured Query Language (SQL) for Data Science.

Frequently Asked Questions

Q1. How to join 10 tables in SQL?

A. Joining multiple tables is like connecting puzzle pieces. You start with one table and link the others one by one using join conditions. Each connection adds more information to the combined result. The process is the same regardless of how many tables youโ€™re joining.

Q2. What is the difference between inner join and natural join?

A. An inner join returns only matching rows from both tables. A natural join tries to match rows based on identical column names, which can be unreliable. Itโ€™s better to explicitly define the join condition with an inner join.

Q3. What is a Join?

A. A join combines data from two or more tables based on a related column. It lets you retrieve related information from multiple sources in a single query.

Q3. Can we join 2 same tables in SQL?

A. Yes, we can join two instances of the same table in SQL. Itโ€™s termed a self-join, useful when analyzing relationships within a single table, often utilizing aliases to differentiate between the instances.

Aspiring Data Scientist with a passion to play and wrangle with data and get insights from it to help the community know the upcoming trends and products for their better future.With an ambition to develop product used by millions which makes their life easier and better.

Login to continue reading and enjoy expert-curated content.

Free Courses

Generative AI - A Way of Life

Explore Generative AI for beginners: create text and images, use top AI tools, learn practical skills, and ethics.

Getting Started with Large Language Models

Master Large Language Models (LLMs) with this course, offering clear guidance in NLP and model training made simple.

Building LLM Applications using Prompt Engineering

This free course guides you on building LLM apps, mastering prompt engineering, and developing chatbots with enterprise data.

Improving Real World RAG Systems: Key Challenges & Practical Solutions

Explore practical solutions, advanced retrieval strategies, and agentic RAG systems to improve context, relevance, and accuracy in AI-driven applications.

Microsoft Excel: Formulas & Functions

Master MS Excel for data analysis with key formulas, functions, and LookUp tools in this comprehensive course.

Responses From Readers

What is the difference between right join and select from the second table?

123 1
Alakh Sethi

Answer from both the queries will be the same in this case but can differ in another case when the attributes in the first table are not the same as second table then null values have to be introduced in tuples of the second table after performing right join.

123 456

in output it should show Gender field as well. Please explain if not then why?

Flagship Programs

GenAI Pinnacle Program| GenAI Pinnacle Plus Program| AI/ML BlackBelt Program| Agentic AI Pioneer Program

Free Courses

Generative AI| DeepSeek| OpenAI Agent SDK| LLM Applications using Prompt Engineering| DeepSeek from Scratch| Stability.AI| SSM & MAMBA| RAG Systems using LlamaIndex| Building LLMs for Code| Python| Microsoft Excel| Machine Learning| Deep Learning| Mastering Multimodal RAG| Introduction to Transformer Model| Bagging & Boosting| Loan Prediction| Time Series Forecasting| Tableau| Business Analytics| Vibe Coding in Windsurf| Model Deployment using FastAPI| Building Data Analyst AI Agent| Getting started with OpenAI o3-mini| Introduction to Transformers and Attention Mechanisms

Popular Categories

AI Agents| Generative AI| Prompt Engineering| Generative AI Application| News| Technical Guides| AI Tools| Interview Preparation| Research Papers| Success Stories| Quiz| Use Cases| Listicles

Generative AI Tools and Techniques

GANs| VAEs| Transformers| StyleGAN| Pix2Pix| Autoencoders| GPT| BERT| Word2Vec| LSTM| Attention Mechanisms| Diffusion Models| LLMs| SLMs| Encoder Decoder Models| Prompt Engineering| LangChain| LlamaIndex| RAG| Fine-tuning| LangChain AI Agent| Multimodal Models| RNNs| DCGAN| ProGAN| Text-to-Image Models| DDPM| Document Question Answering| Imagen| T5 (Text-to-Text Transfer Transformer)| Seq2seq Models| WaveNet| Attention Is All You Need (Transformer Architecture) | WindSurf| Cursor

Popular GenAI Models

Llama 4| Llama 3.1| GPT 4.5| GPT 4.1| GPT 4o| o3-mini| Sora| DeepSeek R1| DeepSeek V3| Janus Pro| Veo 2| Gemini 2.5 Pro| Gemini 2.0| Gemma 3| Claude Sonnet 3.7| Claude 3.5 Sonnet| Phi 4| Phi 3.5| Mistral Small 3.1| Mistral NeMo| Mistral-7b| Bedrock| Vertex AI| Qwen QwQ 32B| Qwen 2| Qwen 2.5 VL| Qwen Chat| Grok 3

AI Development Frameworks

n8n| LangChain| Agent SDK| A2A by Google| SmolAgents| LangGraph| CrewAI| Agno| LangFlow| AutoGen| LlamaIndex| Swarm| AutoGPT

Data Science Tools and Techniques

Python| R| SQL| Jupyter Notebooks| TensorFlow| Scikit-learn| PyTorch| Tableau| Apache Spark| Matplotlib| Seaborn| Pandas| Hadoop| Docker| Git| Keras| Apache Kafka| AWS| NLP| Random Forest| Computer Vision| Data Visualization| Data Exploration| Big Data| Common Machine Learning Algorithms| Machine Learning| Google Data Science Agent
๐Ÿ‘ Av Logo White

Continue your learning for FREE

Forgot your password?
๐Ÿ‘ Av Logo White

Enter OTP sent to

Edit

Wrong OTP.

Enter the OTP

Resend OTP

Resend OTP in 45s

๐Ÿ‘ Popup Banner
๐Ÿ‘ AI Popup Banner