Skip to main content
PostgreSQL
CHAPTER 14 Intermediate

INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN

Updated: May 16, 2026
7 min read

# CHAPTER 14

INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN

1. Introduction

Normalization forced us to split our data into multiple tables. The users table holds the names, and the orders table holds the purchases. But when a manager asks for a report showing "Customer Names and their Purchase Amounts," we have a problem. The data is in two different places! To solve this, we use the JOIN command. It is the most powerful and heavily used analytical tool in the entire SQL language.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Understand the concept of Table Joins via Foreign Keys.
  • Use INNER JOIN to fetch strictly matching data.
  • Use LEFT JOIN to fetch matching data plus all remaining parent rows.
  • Understand the rarely used RIGHT JOIN and FULL OUTER JOIN.
  • Use Table Aliases to write cleaner, faster queries.

3. The INNER JOIN (The Strict Match)

An INNER JOIN is the most common join. It returns ONLY the rows that have a match in BOTH tables. If John has placed an order, he appears. If Sarah has never placed an order, she is completely excluded from the results.
sql
12345
-- Let's stitch users and orders together!
SELECT users.first_name, orders.total_price 
FROM users
INNER JOIN orders 
    ON users.id = orders.user_id; -- This is the mathematical bridge!

How it works: PostgreSQL looks at the FROM table (users). It then grabs the orders table. The ON clause tells PostgreSQL exactly how to snap the two tables together (by matching the Primary Key to the Foreign Key).

4. Table Aliases (Writing Cleaner Code)

Typing users.id and orders.user_id over and over is exhausting. We use Aliases (AS) in the FROM clause to give tables short nicknames.
sql
1234
SELECT u.first_name, o.total_price 
FROM users AS u
INNER JOIN orders AS o 
    ON u.id = o.user_id;

5. The LEFT JOIN (The Inclusive Match)

What if the Marketing Department says: *"Give us a list of ALL users, and if they have placed an order, show the price. If they haven't placed an order, show a blank space so we know who to email a discount code to."* An INNER JOIN would fail because it excludes users without orders. We must use a LEFT JOIN.

A LEFT JOIN returns ALL rows from the Left table (the FROM table), regardless of whether they have a match in the Right table. If there is no match, the Right table columns are filled with NULL.

sql
1234
SELECT u.first_name, o.total_price 
FROM users u
LEFT JOIN orders o 
    ON u.id = o.user_id;

*(Sarah, who has no orders, will now appear in the list, and her totalprice will show as NULL!)*

6. The RIGHT JOIN

A RIGHT JOIN is the exact opposite of a LEFT JOIN. It returns all rows from the Right table, and matching rows from the Left. *Professional Tip:* Developers almost never use RIGHT JOIN. Why? Because you can just swap the order of the tables and use a LEFT JOIN. It is industry standard to read from Left to Right.

7. The FULL OUTER JOIN

A FULL OUTER JOIN returns EVERYTHING. It returns all matched rows, all unmatched Left rows, and all unmatched Right rows. It is rarely used in daily web development, but occasionally used in deep data auditing to find disconnected orphaned data.

8. Mini Project: The 3-Table Mega Join

In E-Commerce, the orders table only holds the user
id. The orderitems table holds the productid. We need to join 3 tables to see exactly what John bought!
sql
1234567891011121314
SELECT 
    u.first_name, 
    p.product_name, 
    oi.quantity
FROM users u
-- Link Users to Orders
INNER JOIN orders o 
    ON u.id = o.user_id
-- Link Orders to the Pivot Table
INNER JOIN order_items oi 
    ON o.id = oi.order_id
-- Link the Pivot Table to Products!
INNER JOIN products p 
    ON oi.product_id = p.id;

9. Common Mistakes

  • Ambiguous Column Names: If both the users table and the products table have a column named id, and you just type SELECT id, PostgreSQL will crash with an "Ambiguous Column" error. You MUST specify which table it belongs to: SELECT u.id.
  • Forgetting the ON clause: If you write a JOIN but forget the ON condition, PostgreSQL doesn't know how to link them. It will multiply every row by every row (a Cross Join), resulting in millions of garbage results.

10. Best Practices

  • Standardize on LEFT JOIN: When building reports where you need a base list of entities (like "All Users" or "All Products") regardless of their activity, always structure your query to make that base list the FROM table, and LEFT JOIN the activity tables to it.

11. Exercises

  1. 1. Write a query using INNER JOIN to fetch the departmentname and the employeename for all active employees.
  1. 2. What is the fundamental difference in the output between an INNER JOIN and a LEFT JOIN?

12. SQL Challenges

Write a query to find the names of ALL departments in the company, and the names of the employees in them. If a department currently has ZERO employees, the department name must still appear in the final report!
sql
1234
SELECT d.dept_name, e.first_name 
FROM departments d
LEFT JOIN employees e 
    ON d.dept_id = e.department_id;

13. MCQ Quiz with Answers

Question 1

A Marketing manager requests a list of ALL registered users. If a user has made a purchase, their purchase total should be shown. If they have not, their total should be NULL. Which JOIN must be used?

Question 2

What is the purpose of the ON clause in a JOIN statement?

14. Interview Questions

  • Q: Explain the difference between an INNER JOIN and a FULL OUTER JOIN. In what business scenario would you explicitly need a FULL OUTER JOIN?
  • Q: If a query joining three tables is failing with an "Ambiguous Column Reference" error, what does that mean and how do you fix it?

15. FAQs

Q: Does joining tables slow down the database? A: Yes, JOINs require CPU power. However, relational databases are highly optimized for this. If you put an "Index" on your Foreign Key columns, PostgreSQL can join millions of rows in milliseconds.

16. Summary

You have unlocked the core analytical power of the Relational Database. By utilizing INNER JOIN to find exact matches, and LEFT JOIN to map comprehensive reports, you can traverse across the mathematical bridges of your schema to reconstruct normalized data into beautiful, human-readable dashboards.

17. Next Chapter Recommendation

Standard joins cover 90% of your daily workload. But what if you need to join a table to *itself*? Or stack the results of two completely different queries on top of each other? In Chapter 15: Advanced JOIN Queries and Set Operations, we will explore Self Joins, CROSS JOINs, and the UNION operator.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·