MySQL & MariaDB SQL and Databases

SQL JOIN Types: INNER, LEFT, RIGHT and FULL JOIN Examples

Learn what INNER, LEFT, RIGHT and FULL JOIN return using the same example tables, see why MySQL has no FULL OUTER JOIN, how to fake it with UNION, and why rows disappear in a JOIN chain.

Serhat ALPASLAN22 min read

Contents

SQL JOIN usage means matching rows from multiple tables in a relational database on a common column and combining them into a single result set. This lesson is part of the SQL and Databases series and is written for PostgreSQL 18, MySQL 8.4 LTS, and SQL Server 2022; syntax differences are noted where relevant. When you join the same two tables with INNER JOIN, you’ll see 3 rows; with LEFT JOIN, you’ll get 5 rows; where those 2 missing rows went will become clear by the end of the lesson.

What you’ll learn in this lesson

  • Explain which rows INNER, LEFT, RIGHT, FULL OUTER, CROSS, and SELF JOIN return
  • Write queries that join two and three tables with JOIN
  • Find unmatched records with LEFT JOIN + IS NULL
  • Get the same result with UNION when FULL OUTER JOIN is not available in MySQL
  • Recognize common JOIN mistakes (row multiplication, breaking a LEFT JOIN with WHERE, inserting an INNER into a JOIN chain)

You can follow the lesson with basic knowledge of SELECT, WHERE, and ORDER BY; to practice, having PostgreSQL, MySQL, or SQL Server installed is enough.

What is SQL JOIN, and what does it do?

Short answer JOIN is a SQL clause that matches rows from two or more tables on a key column and combines them into a single result table. With JOIN, you can get a combined report in a single query even when customer information is in one table and orders are in another.

In relational databases, data is split across tables to avoid writing the same information twice: the customer’s name lives in the musteriler table, and the order amount lives in the siparisler table. When you want to see these two pieces side by side, the clause that joins the tables is JOIN, and the clause where you write the matching rule is ON. What determines which rows JOIN returns is the JOIN type you choose and the ON condition.

By definition: JOIN is a SQL clause that produces a new result set by matching rows from two tables according to an equality condition.

Example tables: musteriler, siparisler, and kategoriler

All examples use the same three tables: musteriler, siparisler, and kategoriler. The two setup scripts below are copy-and-run for PostgreSQL 18, MySQL 8.4, and SQL Server 2022; the expected output of each query is given below it, and no external dependency is required.

-- Setup script for PostgreSQL 18 and MySQL 8.4
CREATE TABLE musteriler (
  musteri_id INT PRIMARY KEY,
  ad_soyad   VARCHAR(60) NOT NULL,
  sehir      VARCHAR(40)
);

CREATE TABLE kategoriler (
  kategori_id  INT PRIMARY KEY,
  kategori_ad  VARCHAR(40) NOT NULL
);

CREATE TABLE siparisler (
  siparis_id  INT PRIMARY KEY,
  musteri_id  INT,
  kategori_id INT,
  urun        VARCHAR(50),
  tutar       NUMERIC(10,2),
  FOREIGN KEY (musteri_id)  REFERENCES musteriler(musteri_id),
  FOREIGN KEY (kategori_id) REFERENCES kategoriler(kategori_id)
);

INSERT INTO musteriler (musteri_id, ad_soyad, sehir) VALUES
  (1, 'Ayşe Yılmaz',  'İstanbul'),
  (2, 'Mehmet Demir', 'Ankara'),
  (3, 'Zeynep Kaya',  'İzmir'),
  (4, 'Burak Şahin',  'Bursa'),
  (5, 'Elif Çetin',   'Antalya');

INSERT INTO kategoriler (kategori_id, kategori_ad) VALUES
  (1, 'Çevre Birimi'),
  (2, 'Ekran'),
  (3, 'Ses');

INSERT INTO siparisler (siparis_id, musteri_id, kategori_id, urun, tutar) VALUES
  (101, 1, 1, 'Klavye',   450.00),
  (102, 2, 2, 'Monitör', 3200.00),
  (103, 3, 3, 'Kulaklık', 750.00);

Here’s the point to notice in the data: there are five customers but only three orders. Burak and Elif have no orders at all; these two rows will be the easiest way to show the difference between JOIN types.

MySQL 8.4 accepts the NUMERIC type in the same form as DECIMAL, so the script works as is. In SQL Server 2022, however, text columns need NVARCHAR and N'...' literals to store Turkish characters; the script below is the SQL Server counterpart of the main script and creates the same three tables, the same foreign keys, and the same data.

-- SQL Server 2022: NVARCHAR and N'...' literals for Turkish characters
CREATE TABLE musteriler (
  musteri_id INT PRIMARY KEY,
  ad_soyad   NVARCHAR(60) NOT NULL,
  sehir      NVARCHAR(40)
);

CREATE TABLE kategoriler (
  kategori_id  INT PRIMARY KEY,
  kategori_ad  NVARCHAR(40) NOT NULL
);

CREATE TABLE siparisler (
  siparis_id  INT PRIMARY KEY,
  musteri_id  INT,
  kategori_id INT,
  urun        NVARCHAR(50),
  tutar       DECIMAL(10,2),
  FOREIGN KEY (musteri_id)  REFERENCES musteriler(musteri_id),
  FOREIGN KEY (kategori_id) REFERENCES kategoriler(kategori_id)
);

INSERT INTO musteriler (musteri_id, ad_soyad, sehir) VALUES
  (1, N'Ayşe Yılmaz',  N'İstanbul'),
  (2, N'Mehmet Demir', N'Ankara'),
  (3, N'Zeynep Kaya',  N'İzmir'),
  (4, N'Burak Şahin',  N'Bursa'),
  (5, N'Elif Çetin',   N'Antalya');

INSERT INTO kategoriler (kategori_id, kategori_ad) VALUES
  (1, N'Çevre Birimi'),
  (2, N'Ekran'),
  (3, N'Ses');

INSERT INTO siparisler (siparis_id, musteri_id, kategori_id, urun, tutar) VALUES
  (101, 1, 1, N'Klavye',   450.00),
  (102, 2, 2, N'Monitör', 3200.00),
  (103, 3, 3, N'Kulaklık', 750.00);

SQL Server’s default collation depends on the Windows locale used during installation: on servers installed with the English (US) locale, the default is SQL_Latin1_General_CP1_CI_AS, and VARCHAR columns cannot store non-ASCII Turkish characters. On servers installed with the Turkish locale, the default is usually Turkish_CI_AS. To work regardless of the collation used during installation, the safest approach is to make text columns NVARCHAR and write literals in the N'...' form; in the exercises, you’ll make a comparison such as WHERE m.sehir = N'İstanbul'.

What are the SQL JOIN types?

Short answer SQL has six main JOIN types: INNER, LEFT, RIGHT, FULL OUTER, CROSS, and SELF JOIN. The first four select rows based on a match condition, CROSS JOIN produces every combination, and SELF JOIN joins a table to itself.

The row counts in the table below assume a left table musteriler (5 rows) and a right table siparisler (3 rows). Based on this assumption, the RIGHT JOIN row in the table returns 3 rows; in the RIGHT JOIN example below, the tables are swapped, so the result is 5 rows.

JOIN type Rows returned Row count in the sample data
INNER JOIN Only rows that match on both sides 3
LEFT JOIN All rows from the left table + matching right rows (NULL if none) 5
RIGHT JOIN All rows from the right table + matching left rows (NULL if none) 3
FULL OUTER JOIN All rows from both tables, with NULL where there is no match 5
CROSS JOIN Every combination of the two tables 15
SELF JOIN Rows in a table matched with itself Varies by table

INNER JOIN

The shortest answer to “What is INNER JOIN?” is this: it is a join type that returns only the rows that satisfy the ON condition; rows without a match never enter the result set. That’s why customers without orders don’t appear in the result below.

SELECT m.ad_soyad, s.urun, s.tutar
FROM musteriler AS m
INNER JOIN siparisler AS s ON s.musteri_id = m.musteri_id
ORDER BY m.musteri_id;
ad_soyad      | urun     | tutar
--------------+----------+---------
Ayşe Yılmaz   | Klavye   |  450.00
Mehmet Demir  | Monitör  | 3200.00
Zeynep Kaya   | Kulaklık |  750.00
(3 satır)

AS m and AS s are table aliases. They are used to avoid repeating long table names in every column and to distinguish columns with the same name; you can also omit the AS keyword.

LEFT JOIN

LEFT JOIN preserves all rows from the table on the left; if it can’t find a match on the right, it fills those columns with NULL. When we change the same query to a LEFT JOIN, Burak and Elif also appear in the list.

SELECT m.ad_soyad, m.sehir, s.urun
FROM musteriler AS m
LEFT JOIN siparisler AS s ON s.musteri_id = m.musteri_id
ORDER BY m.musteri_id;
ad_soyad      | sehir    | urun
--------------+----------+---------
Ayşe Yılmaz   | İstanbul | Klavye
Mehmet Demir  | Ankara   | Monitör
Zeynep Kaya   | İzmir    | Kulaklık
Burak Şahin   | Bursa    | NULL
Elif Çetin    | Antalya  | NULL
(5 satır)

The 3-vs-5-row difference we mentioned at the beginning comes exactly from here: INNER JOIN eliminates the two customers without a match, while LEFT JOIN keeps them with NULL order information.

RIGHT JOIN

RIGHT JOIN preserves all rows from the table on the right and produces NULL when there’s no match on the left. It’s the mirror image of LEFT JOIN: swapping the tables and writing LEFT JOIN often gives the same result, so teams prefer LEFT JOIN for readability.

SELECT m.ad_soyad, s.urun
FROM siparisler AS s
RIGHT JOIN musteriler AS m ON m.musteri_id = s.musteri_id
ORDER BY m.musteri_id;
ad_soyad      | urun
--------------+---------
Ayşe Yılmaz   | Klavye
Mehmet Demir  | Monitör
Zeynep Kaya   | Kulaklık
Burak Şahin   | NULL
Elif Çetin    | NULL
(5 satır)

Here, since the right table is musteriler, 5 rows come back. If you wrote musteriler RIGHT JOIN siparisler, the right side would be orders, so only 3 rows would come back; in other words, the row count of a RIGHT JOIN depends on which table is on the right.

FULL OUTER JOIN (with UNION in MySQL)

FULL OUTER JOIN returns all rows from both tables; rows that don’t match on either side are completed with NULL. PostgreSQL 18 and SQL Server 2022 support this type directly, but MySQL 8.4 does not have FULL OUTER JOIN, and if you write it you’ll get a syntax error.

-- PostgreSQL 18 and SQL Server 2022
SELECT m.ad_soyad, s.urun
FROM musteriler AS m
FULL OUTER JOIN siparisler AS s ON s.musteri_id = m.musteri_id
ORDER BY m.musteri_id;
ad_soyad      | urun
--------------+---------
Ayşe Yılmaz   | Klavye
Mehmet Demir  | Monitör
Zeynep Kaya   | Kulaklık
Burak Şahin   | NULL
Elif Çetin    | NULL
(5 satır)

In MySQL, we get the same result as the UNION of a LEFT JOIN and a RIGHT JOIN. Because UNION removes duplicate rows, matching rows don’t come back twice.

-- MySQL 8.4: FULL OUTER JOIN emulation
SELECT m.ad_soyad, s.urun
FROM musteriler AS m
LEFT JOIN siparisler AS s ON s.musteri_id = m.musteri_id
UNION
SELECT m.ad_soyad, s.urun
FROM musteriler AS m
RIGHT JOIN siparisler AS s ON s.musteri_id = m.musteri_id
ORDER BY ad_soyad;
ad_soyad      | urun
--------------+---------
Ayşe Yılmaz   | Klavye
Burak Şahin   | NULL
Elif Çetin    | NULL
Mehmet Demir  | Monitör
Zeynep Kaya   | Kulaklık
(5 satır)

The result is again 5 rows; the two customers without orders come back with NULL urun. The hidden risk here is this: UNION removes not only unmatched rows but also rows that are genuinely identical. If the same customer bought the same product twice, those two orders collapse into one row and you lose data.

A safer pattern is to add WHERE m.musteri_id IS NULL inside the second SELECT and use UNION ALL; this way, only rows that don’t match on the right side are added, and legitimate duplicates are preserved.

-- MySQL 8.4: FULL OUTER JOIN emulation without losing duplicates
SELECT m.ad_soyad, s.urun
FROM musteriler AS m
LEFT JOIN siparisler AS s ON s.musteri_id = m.musteri_id
UNION ALL
SELECT m.ad_soyad, s.urun
FROM musteriler AS m
RIGHT JOIN siparisler AS s ON s.musteri_id = m.musteri_id
WHERE m.musteri_id IS NULL;

CROSS JOIN and SELF JOIN

CROSS JOIN takes no match condition and produces the Cartesian product of the two tables: 5 customers times 3 categories makes 15 rows. SELF JOIN, on the other hand, joins a table to itself; the employee–manager relationship is the classic example.

SELECT m.ad_soyad, k.kategori_ad
FROM musteriler AS m
CROSS JOIN kategoriler AS k
ORDER BY m.ad_soyad, k.kategori_ad;
ad_soyad      | kategori_ad
--------------+-------------
Ayşe Yılmaz   | Çevre Birimi
Ayşe Yılmaz   | Ekran
Ayşe Yılmaz   | Ses
... (toplam 15 satır)
CREATE TABLE calisanlar (
  calisan_id  INT PRIMARY KEY,
  ad          VARCHAR(40),
  yonetici_id INT
);

INSERT INTO calisanlar (calisan_id, ad, yonetici_id) VALUES
  (1, 'Ayşe Yılmaz',  NULL),
  (2, 'Mehmet Demir', 1),
  (3, 'Zeynep Kaya',  1);

SELECT c.ad AS calisan, y.ad AS yonetici
FROM calisanlar AS c
LEFT JOIN calisanlar AS y ON y.calisan_id = c.yonetici_id
ORDER BY c.calisan_id;
calisan       | yonetici
--------------+--------------
Ayşe Yılmaz   | NULL
Mehmet Demir  | Ayşe Yılmaz
Zeynep Kaya   | Ayşe Yılmaz

If you’re going to run this block on SQL Server, make the ad column NVARCHAR(40) and write the values as N'...'; otherwise, the default collation may corrupt Turkish characters.

The difference between LEFT JOIN and INNER JOIN

Short answer The difference between LEFT JOIN and INNER JOIN comes down to the fate of unmatched rows: INNER JOIN returns only rows that match on both sides, while LEFT JOIN keeps every row from the left table and fills the right-side columns with NULL when there’s no match.

Topic INNER JOIN LEFT JOIN
Rows returned in the sample data 3 5
Customer with no orders Not in the result Present in the result, order columns are NULL
NULL value Not produced because no unmatched row is returned Appears in unmatched rows
Typical use Mandatory relationships, reporting JOINs Optional relationships, “find what’s missing” queries

Rule of thumb: if you’re saying “I also want to see customers with no orders,” use LEFT JOIN; if you’re saying “I only want customers who placed orders,” use INNER JOIN.

Using SQL JOIN: joining two and three tables

Short answer You join two tables with the FROM + JOIN … ON … pattern, and you join a third table by adding a second JOIN … ON … line to that same pattern. Each JOIN needs its own match condition; mixing JOIN types in the chain directly changes which rows are kept.

  1. Write the first table with FROM and give it an alias.
  2. Add the second table with a JOIN type and match it with ON.
  3. Add a second JOIN line for the third table and write its own ON condition.
  4. Select the columns using their aliases, and sort with ORDER BY if needed.
-- Three tables: customer, order and category
SELECT m.ad_soyad, s.urun, k.kategori_ad
FROM musteriler AS m
INNER JOIN siparisler AS s  ON s.musteri_id  = m.musteri_id
INNER JOIN kategoriler AS k ON k.kategori_id = s.kategori_id
ORDER BY m.musteri_id;
ad_soyad      | urun     | kategori_ad
--------------+----------+-------------
Ayşe Yılmaz   | Klavye   | Çevre Birimi
Mehmet Demir  | Monitör  | Ekran
Zeynep Kaya   | Kulaklık | Ses
(3 satır)

To keep customers with no orders, the subsequent JOINs in the chain must also be LEFT; if an INNER JOIN slips in between, the NULL rows protected by the LEFT JOIN get filtered out. Because Burak and Elif have a NULL s.kategori_id value, the INNER JOIN kategoriler ON k.kategori_id = s.kategori_id condition drops them from the result.

-- Correct: all JOINs in the chain are LEFT
SELECT m.ad_soyad, s.urun, k.kategori_ad
FROM musteriler AS m
LEFT JOIN siparisler AS s  ON s.musteri_id  = m.musteri_id
LEFT JOIN kategoriler AS k ON k.kategori_id = s.kategori_id
ORDER BY m.musteri_id;
ad_soyad      | urun     | kategori_ad
--------------+----------+-------------
Ayşe Yılmaz   | Klavye   | Çevre Birimi
Mehmet Demir  | Monitör  | Ekran
Zeynep Kaya   | Kulaklık | Ses
Burak Şahin   | NULL     | NULL
Elif Çetin    | NULL     | NULL
(5 satır)

As a rule: if you want to preserve a table, make every JOIN after that table a LEFT JOIN. A single INNER JOIN mixed in silently deletes the rows you were protecting.

Finding unmatched records (LEFT JOIN + IS NULL)

Short answer If you do a LEFT JOIN and search for an IS NULL condition on the right table’s key column, only the unmatched rows come back. This pattern is the standard answer to questions like “customers who haven’t placed an order.”

SELECT m.ad_soyad, m.sehir
FROM musteriler AS m
LEFT JOIN siparisler AS s ON s.musteri_id = m.musteri_id
WHERE s.siparis_id IS NULL
ORDER BY m.ad_soyad;
ad_soyad     | sehir
-------------+---------
Burak Şahin  | Bursa
Elif Çetin   | Antalya
(2 satır)

Writing the condition as s.tutar IS NULL is risky; if the amount column later changes to allow NULLs, the result will be wrong. Always check a primary key column that is NOT NULL (s.siparis_id). Also, don’t forget that writing s.siparis_id = NULL returns no rows at all; comparisons with NULL are done only with IS NULL / IS NOT NULL.

The difference between JOIN and UNION

Short answer JOIN combines tables side by side—that is, by adding columns; UNION combines result sets with the same structure one below the other—that is, by adding rows. With JOIN, the row count depends on the matches; with UNION, the row counts are added together.

Feature JOIN UNION
Direction of combination Side by side (adds columns) Stacked vertically (adds rows)
Condition Match condition with ON No condition; column count and types must be compatible
Duplicate rows Can repeat as many times as there are matches UNION removes duplicates, UNION ALL keeps them
SELECT ad_soyad AS metin FROM musteriler
UNION
SELECT urun FROM siparisler
ORDER BY metin;

This query produces a single-column list from two different tables; customer names and product names are sorted one below the other. MySQL and PostgreSQL expect column types to be compatible for UNION.

Common Mistakes

1. Filtering the right table in WHERE after a LEFT JOIN

Symptom: Even though you write a LEFT JOIN, customers with no orders don’t show up in the result and the row count comes out the same as with INNER JOIN. Cause: A condition like WHERE s.tutar > 0 also eliminates NULL rows, effectively turning the LEFT JOIN into an INNER JOIN. Fix: Move the filter on the right table from WHERE to the ON condition.

-- Wrong: breaks the LEFT JOIN
SELECT m.ad_soyad, s.urun
FROM musteriler AS m
LEFT JOIN siparisler AS s ON s.musteri_id = m.musteri_id
WHERE s.tutar > 0;

-- Correct: filter inside ON
SELECT m.ad_soyad, s.urun
FROM musteriler AS m
LEFT JOIN siparisler AS s
  ON s.musteri_id = m.musteri_id
 AND s.tutar > 0;

2. Forgetting the ON condition

Symptom: You expect 5 customers and 3 orders but get 15 rows. Cause: It depends on the version: MySQL 8.4 accepts FROM musteriler m JOIN siparisler s and, since there is no match condition, produces a Cartesian product; in PostgreSQL 18 and SQL Server 2022, an INNER/LEFT JOIN without ON is a syntax error. On the other hand, the comma-separated FROM musteriler m, siparisler s syntax is valid in all three versions and returns a 15-row Cartesian product if the WHERE condition is forgotten. Fix: Write the ON condition after every JOIN; if you intentionally want all combinations, use CROSS JOIN and note it in a comment.

-- MySQL 8.4: no ON, no error, returns 15 rows
SELECT m.ad_soyad, s.urun
FROM musteriler AS m
JOIN siparisler AS s;

-- Valid in all three versions: comma-separated FROM + forgotten WHERE
SELECT m.ad_soyad, s.urun
FROM musteriler AS m, siparisler AS s;

3. Row multiplication in a one-to-many relationship

Symptom: You expect a customer list, but the same customer appears in multiple rows and COUNT(*) gives the wrong customer count. Cause: When a customer has more than one order, the JOIN produces one row per order. Fix: For a unique count, use COUNT(DISTINCT m.musteri_id), or if you need one row per customer, aggregate with a subquery.

You can see the multiplication in your own tables by adding the following row: the customer with ID 1 now has two orders.

INSERT INTO siparisler (siparis_id, musteri_id, kategori_id, urun, tutar)
VALUES (104, 1, 2, 'Monitör', 2900.00);

SELECT m.ad_soyad, s.urun
FROM musteriler AS m
INNER JOIN siparisler AS s ON s.musteri_id = m.musteri_id
ORDER BY m.musteri_id, s.siparis_id;
ad_soyad      | urun
--------------+---------
Ayşe Yılmaz   | Klavye
Ayşe Yılmaz   | Monitör
Mehmet Demir  | Monitör
Zeynep Kaya   | Kulaklık
(4 satır)
SELECT COUNT(*)                     AS satir_sayisi,
       COUNT(DISTINCT m.musteri_id) AS musteri_sayisi
FROM musteriler AS m
INNER JOIN siparisler AS s ON s.musteri_id = m.musteri_id;
satir_sayisi | musteri_sayisi
-------------+---------------
           4 |              3

Because the same customer appears in two rows, the row count is 4, while the actual customer count is 3. So that the numbers in the rest of the lesson don’t break, delete the row you added afterward:

DELETE FROM siparisler WHERE siparis_id = 104;

4. Trying to write FULL OUTER JOIN in MySQL

Symptom: MySQL 8.4 throws a syntax error and the query doesn’t run at all. Cause: MySQL doesn’t support FULL OUTER JOIN. Fix: Combine LEFT JOIN and RIGHT JOIN with UNION; the WHERE m.musteri_id IS NULL + UNION ALL pattern in the FULL OUTER JOIN section above also prevents duplicates.

5. Leaving the same column name ambiguous

Symptom: You get a “column reference is ambiguous” or “Column ‘musteri_id’ in field list is ambiguous” error. Cause: Both tables have a musteri_id column, and the query can’t tell which one you mean. Fix: Always qualify columns with the table alias: m.musteri_id, s.musteri_id.

6. Putting an INNER JOIN into a LEFT JOIN chain

Symptom: Even though you make the first JOIN a LEFT JOIN, customers with no orders still disappear in the three-table query. Cause: The INNER JOIN kategoriler ON k.kategori_id = s.kategori_id line added to the chain eliminates rows where s.kategori_id is NULL; thus the NULL rows protected by the LEFT JOIN are dropped. Fix: Make all JOINs after the table you want to preserve LEFT JOINs and verify the result against the expected row count.

Performance: A short introduction to the relationship between JOINs and indexes

Short answer A JOIN’s speed depends largely on the indexes on the join columns; if the foreign key column used in the ON condition is indexed, the database finds rows directly instead of scanning them.

PostgreSQL and SQL Server don’t automatically create an index on the referencing column when you define a foreign key; MySQL’s InnoDB engine, however, creates an index for the foreign key. That’s why you need to manually index columns like siparisler.musteri_id, especially in PostgreSQL.

CREATE INDEX idx_siparisler_musteri ON siparisler (musteri_id);

To see whether the query actually uses an index, you can look at EXPLAIN ANALYZE in PostgreSQL, EXPLAIN in MySQL, and the execution plan output in SQL Server. For official details, check out the Joins Between Tables page in the PostgreSQL documentation, the MySQL 8.4 Reference Manual: JOIN Clause, and Microsoft Learn: Joins.

If you run JOIN queries from the application layer, you also need to map the types of the returned columns correctly; for that mapping on the PHP side, you can check out the PHP Tutorials – Data Types article.

Exercises and solutions

The following SQL query examples use the three tables from the beginning of the lesson. Try writing them yourself first, then compare with the solution.

Exercise 1: For each customer, return the number of orders; customers with no orders should show as 0.

SELECT m.ad_soyad, COUNT(s.siparis_id) AS siparis_sayisi
FROM musteriler AS m
LEFT JOIN siparisler AS s ON s.musteri_id = m.musteri_id
GROUP BY m.musteri_id, m.ad_soyad
ORDER BY siparis_sayisi DESC, m.ad_soyad;
ad_soyad      | siparis_sayisi
--------------+---------------
Ayşe Yılmaz   | 1
Mehmet Demir  | 1
Zeynep Kaya   | 1
Burak Şahin   | 0
Elif Çetin    | 0

COUNT(s.siparis_id) doesn’t count NULL rows, so it returns 0; if you wrote COUNT(*), you’d also see 1 for customers with no match.

Exercise 2: List only customers in Istanbul, and keep them in the list even if they have no orders.

SELECT m.ad_soyad, s.urun
FROM musteriler AS m
LEFT JOIN siparisler AS s ON s.musteri_id = m.musteri_id
WHERE m.sehir = 'İstanbul'
ORDER BY m.ad_soyad;

Filtering the left table with WHERE doesn’t break the LEFT JOIN, because the condition applies to the side you want to preserve. What breaks it is filtering a column from the right table in WHERE. In SQL Server, you need to write this query as WHERE m.sehir = N'İstanbul'.

Exercise 3: Return all orders with their category name; orders with no category assigned should also appear.

SELECT s.siparis_id, s.urun, k.kategori_ad
FROM siparisler AS s
LEFT JOIN kategoriler AS k ON k.kategori_id = s.kategori_id
ORDER BY s.siparis_id;

In the sample data, every order has a category, so the result has 3 rows; if you add a row where siparisler.kategori_id is NULL, that order stays in the list with a NULL category name.

Frequently Asked Questions

What are the JOIN types in SQL?

There are six main types: INNER, LEFT, RIGHT, FULL OUTER, CROSS, and SELF JOIN. The first four select rows based on a match condition, CROSS JOIN produces a Cartesian product, and SELF JOIN joins a table to itself.

What is the difference between LEFT JOIN and INNER JOIN in SQL?

INNER JOIN returns only rows that match in both tables, while LEFT JOIN preserves the entire left table and fills unmatched right-side columns with NULL. In the sample data, this difference shows up as 3 rows versus 5 rows.

What is LEFT JOIN in SQL?

LEFT JOIN is a join type that includes every row from the left table in the result set and adds only matching rows from the right table. If there is no match, the right table’s columns become NULL; that’s why it’s the first choice for queries that find missing records.

How is UNION used in SQL?

UNION combines two SELECT results with the same number of columns and compatible types vertically and removes duplicate rows. If you want to keep duplicates, use UNION ALL.

What is FULL OUTER JOIN, and does MySQL have it?

FULL OUTER JOIN returns all rows from both tables and fills unmatched sides with NULL. PostgreSQL 18 and SQL Server 2022 support it directly, while MySQL 8.4 does not have this join type; you get the same result by taking the UNION of a LEFT JOIN and a RIGHT JOIN.

How do you retrieve data from two tables in SQL?

To retrieve data from two tables in SQL, use the pattern FROM tablo1 AS a JOIN tablo2 AS b ON a.anahtar = b.anahtar and select columns using their aliases. Which JOIN type you choose depends on whether you want unmatched rows in the result; in practice, SQL JOIN usage starts with this decision.

Summary and Next Lesson

  • JOIN matches tables on a key column and produces a single result set; the match rule is written in ON.
  • INNER JOIN returns only matches, LEFT JOIN returns the entire left table, and FULL OUTER JOIN returns both sides entirely; in the sample data, that means 3 and 5 rows.
  • MySQL 8.4 doesn’t have FULL OUTER JOIN; it’s emulated with LEFT JOIN + RIGHT JOIN UNION (preferably WHERE … IS NULL + UNION ALL).
  • The LEFT JOIN + IS NULL pattern is the standard way to find unmatched records.
  • Putting a filter on the right table in WHERE or introducing an INNER JOIN into the join chain silently breaks the LEFT JOIN; move the filter into ON, and keep the chain LEFT all the way through.

Once SQL JOIN usage clicks, in the next lesson of the series we’ll summarize JOIN results with GROUP BY and aggregate functions and move on to reporting queries. Until then, writing the exercises above once more with your own tables will make the difference between JOIN types stick. You can reach the site’s other lessons from the serhatalpaslan.com home page.

Lesson complete · 1 / 1

You've finished this lesson

The next lesson in the series is in the works. Subscribe to the channel so you don't miss it when it's published.

Share it if it helped

Write a comment