Last Chance to Join Data Science Interview MasterClass (this week) 🚀 | Just 2 spots remaining...

FREE

SELF JOIN

Course Author Daniel Lee
Instructor

Dan Lee

Learn SQL for free with interactive exercises designed to help aspiring Data Engineers, Data Analysts, and Data Scientists. Learn the basics and advanced SQL concepts by solving real-world problems from MANGO (Meta, Amazon, Netflix, Google, OpenAI)

Lesson Objectives

By the end of this lesson, you will:

  • Learn how to join a table to itself using SELF JOIN
  • Understand how to compare records within the same table
  • Write SQL queries to analyze relationships between Google Ads campaigns

🎬 Scenario: Comparing Campaign Performance

Your manager at Google Ads, Jordan, wants to analyze how different campaigns from the same company compare to each other.

Can you list all campaign pairs within each company so we can compare their performance?

Your task is to use a SELF JOIN to find all campaign comparisons for the same advertiser.

campaign_iduser_idcampaign_namead_typestart_dateend_datebudgetimpressionsclicksconversions
11Growth Hackers SpecialSearch2024-01-10T00:00:002024-02-10T00:00:00500050000012000800
22BigTech AwarenessDisplay2024-01-05T00:00:002024-02-20T00:00:00200002000000500003000
33Ecom Holiday SalesShopping2023-11-15T00:00:002024-01-15T00:00:0010000750000220001500
44Fashion Hub SummerVideo2024-02-01T00:00:002024-03-01T00:00:0015000900000180001200
55HealthyLife Brand PushSearch2024-01-20T00:00:002024-03-10T00:00:00800060000015000900

1. Understanding SELF JOIN

A SELF JOIN joins a table to itself by treating it as if it were two separate tables.

Why use SELF JOIN?
Compare records within the same table
Find relationships between different rows in the same dataset
Analyze hierarchical or grouped data

Syntax

1SELECT a.column1, b.column2  
2FROM table_name a  
3JOIN table_name b  
4ON a.common_key = b.common_key;
  • Assign aliases (a, b) to the table so it can be referenced twice.
  • Use ON to define the relationship between the two rows.

2. Comparing Campaigns Within the Same Company

Jordan wants to see all campaign comparisons within each company.

Example Query: Listing Campaign Comparisons for the Same Company

1SELECT a.company, a.campaign_name AS campaign_1, b.campaign_name AS campaign_2  
2FROM google_ads_campaigns a  
3JOIN google_ads_campaigns b  
4ON a.company = b.company  
5AND a.campaign_id < b.campaign_id;
companycampaign_1campaign_2
BigTech CorpBigTech AwarenessProduct Launch 2024
FashionHubFashion Hub SummerNew Collection Ads
EcomStoreEcom Holiday SalesBlack Friday Special

What’s Happening?

  • SELF JOIN finds pairs of campaigns within the same company.
  • The condition a.campaign_id < b.campaign_id ensures each pair appears only once (avoids duplicates).

✍️ SQL Exercise

Exercise: Comparing Campaigns Within the Same Company

Jordan wants to compare all campaign pairs for each company.

Filter condition:

  • Retrieve company name and two campaign names.
  • Each company should appear with paired campaign comparisons.
  • Sort by company in ascending order.
companycampaign_1campaign_2
BigTech CorpBigTech AwarenessProduct Launch 2024
FashionHubFashion Hub SummerNew Collection Ads
EcomStoreEcom Holiday SalesBlack Friday Special

Write an SQL query to return the requested data.

Upgrade to Pro
👉 Found this lesson helpful?