Back to Data Science Notes
Topic #40

SQL Joins

By the end of this lesson, you will be able to combine data from multiple tables using SQL joins and choose the correct join type for your analysis.

What it is

A SQL join combines rows from two or more tables based on a related column between them. Think of it as merging spreadsheets where one column acts as the common key. The most common types are INNER JOIN, which returns only matching records; LEFT JOIN, which keeps all records from the left table; RIGHT JOIN, which keeps all records from the right table; FULL OUTER JOIN, which keeps all records from both tables; CROSS JOIN, which produces a Cartesian product; and SELF JOIN, which joins a table to itself.

Why it matters

  • Data Integration: Relational databases normalize data into separate tables to reduce redundancy. Joins allow you to reconstruct complete views for analysis.
  • Efficiency: Performing joins in the database engine is often faster than pulling raw data into Python or R and merging it there.
  • Accuracy: Understanding join types prevents accidental duplication of rows (fan-out) or loss of critical data (missing matches).
  • Reporting: Most business intelligence dashboards rely on joined datasets to display metrics alongside descriptive attributes.

Syntax or steps

The basic syntax involves specifying the tables and the condition that links them. For example, to link an orders table with a customers table, you match the customer ID present in both.

SELECT columns
FROM table1
JOIN_TYPE table2 ON table1.key = table2.key;

Example

Consider two small tables: employees and departments. We want to see employee names alongside their department names.

-- Table: employees
-- id | name    | dept_id
-- 1  | Alice   | 101
-- 2  | Bob     | 102
-- 3  | Charlie | NULL

-- Table: departments
-- id | dept_name
-- 101| Sales
-- 102| Engineering
-- 103| HR

SELECT e.name, d.dept_name
FROM employees e
INNER JOIN departments d ON e.dept_id = d.id;

Explanation: The INNER JOIN looks for matches where e.dept_id equals d.id. Alice matches Sales (101), and Bob matches Engineering (102). Charlie has a NULL department ID, so he does not appear in the result because there is no matching row in departments.

Common mistakes

  • Forgetting the Join Condition: Writing FROM A, B without a WHERE clause creates a CROSS JOIN, resulting in massive, unintended data duplication.
  • Misunderstanding NULLs: In an INNER JOIN, rows with NULL keys are excluded. Use LEFT JOIN if you need to keep those rows.
  • Ambiguous Column Names: If both tables have a column named id, referencing just id causes an error. Always use aliases like e.id or d.id.
  • Performance Issues: Joining on unindexed columns can slow down queries significantly. Ensure join keys are indexed.

When to use it

Choosing the right join depends on whether you prioritize completeness or strict matching.

Join TypeReturnsUse Case
INNER JOINOnly matching rowsYou only care about valid associations (e.g., orders with customers).
LEFT JOINAll left rows + matchesYou want all primary entities even if they lack secondary data (e.g., all users, even those without profiles).
FULL JOINAll rows from bothReconciling two independent lists to find discrepancies.

Practice

Guided Exercise: Modify the previous query to use a LEFT JOIN. What happens to Charlie?

Hint: Charlie should now appear in the results, but his dept_name will be NULL.

Challenge: Write a query to find all departments that currently have zero employees assigned. Use a LEFT JOIN from departments to employees and filter for NULL employee IDs.

Quick check

Question: Which join type would you use if you wanted to list every customer, including those who have never placed an order?

Answer: A LEFT JOIN starting from the customers table to the orders table.

Summary

SQL joins are fundamental tools for combining normalized data into meaningful analytical views. Mastering the difference between inner and outer joins ensures you retrieve exactly the data scope required for your specific question.

Want to go beyond the notes?

Join Coding Now Tech Institute's Data Science course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

SQL Joins – FAQs

Quick answers about learning SQL Joins in Data Science.

This free note from Coding Now Tech Institute explains SQL Joins in Data Science — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Data Science topic on Coding Now Tech Institute, including SQL Joins, is 100% free with no signup required.
With focused practice, most students grasp SQL Joins in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now