runtime
22 / 88

lesson 22 Joins & sets · intermediate

Self-joins & referral relationships

Join a table to itself to explore relationships.

the idea

A self-join joins a table to itself under two aliases. users.referred_by_user_id points at another row of users, so joining users u to users r ON r.user_id = u.referred_by_user_id puts each user beside their referrer. LEFT JOIN keeps users who were not referred.

Dataset: Product / SaaS Analytics

users9 columns · 11,348 rows
user_idnumber · unique idaccount_idnumber · links to accountsfull_nametextemailtextroletextsignup_sourcetextreferred_by_user_idnumber · links to userscreated_atdate & timedeactivated_attext
11Omar Jacksonomar.jackson@ironcapital1.exampleownerworkspace_creatorNULL2024-01-14 18:52:14NULL
21Imani Kangimani.kang@ironcapital1.exampleadmininvitationNULL2024-01-24 03:43:14NULL
31Chloe Mitchellchloe.mitchell@ironcapital1.exampleadmininvitationNULL2024-02-02 21:34:25NULL
41Jaya Carterjaya.carter@ironcapital1.examplememberdomain_joinNULL2024-03-31 03:39:32NULL
51Zoe Turnerzoe.turner@ironcapital1.examplememberdomain_joinNULL2024-02-12 02:53:31NULL
62Esther Smithesther.smith@vectordata2.exampleownerworkspace_creatorNULL2024-01-15 13:21:52NULL
72Wei Walkerwei.walker7@vectordata2.examplememberinvitationNULL2024-02-03 07:03:46NULL
82Lucia Chenlucia.chen@vectordata2.examplememberdomain_joinNULL2024-05-18 15:26:01NULL
92Hana Edwardshana.edwards@vectordata2.examplememberdomain_joinNULL2024-02-01 19:03:56NULL
103Caleb Nakamuracaleb.nakamura@truefoods3.exampleownerworkspace_creatorNULL2024-01-19 11:22:02NULL
user_idnumber · unique id
1
account_idnumber · links to accounts
1
full_nametext
Omar Jackson
emailtext
omar.jackson@ironcapital1.example
roletext
owner
signup_sourcetext
workspace_creator
referred_by_user_idnumber · links to users
NULL
created_atdate & time
2024-01-14 18:52:14
deactivated_attext
NULL
user_idnumber · unique id
2
account_idnumber · links to accounts
1
full_nametext
Imani Kang
emailtext
imani.kang@ironcapital1.example
roletext
admin
signup_sourcetext
invitation
referred_by_user_idnumber · links to users
NULL
created_atdate & time
2024-01-24 03:43:14
deactivated_attext
NULL
user_idnumber · unique id
3
account_idnumber · links to accounts
1
full_nametext
Chloe Mitchell
emailtext
chloe.mitchell@ironcapital1.example
roletext
admin
signup_sourcetext
invitation
referred_by_user_idnumber · links to users
NULL
created_atdate & time
2024-02-02 21:34:25
deactivated_attext
NULL
user_idnumber · unique id
4
account_idnumber · links to accounts
1
full_nametext
Jaya Carter
emailtext
jaya.carter@ironcapital1.example
roletext
member
signup_sourcetext
domain_join
referred_by_user_idnumber · links to users
NULL
created_atdate & time
2024-03-31 03:39:32
deactivated_attext
NULL
user_idnumber · unique id
5
account_idnumber · links to accounts
1
full_nametext
Zoe Turner
emailtext
zoe.turner@ironcapital1.example
roletext
member
signup_sourcetext
domain_join
referred_by_user_idnumber · links to users
NULL
created_atdate & time
2024-02-12 02:53:31
deactivated_attext
NULL
user_idnumber · unique id
6
account_idnumber · links to accounts
2
full_nametext
Esther Smith
emailtext
esther.smith@vectordata2.example
roletext
owner
signup_sourcetext
workspace_creator
referred_by_user_idnumber · links to users
NULL
created_atdate & time
2024-01-15 13:21:52
deactivated_attext
NULL
user_idnumber · unique id
7
account_idnumber · links to accounts
2
full_nametext
Wei Walker
emailtext
wei.walker7@vectordata2.example
roletext
member
signup_sourcetext
invitation
referred_by_user_idnumber · links to users
NULL
created_atdate & time
2024-02-03 07:03:46
deactivated_attext
NULL
user_idnumber · unique id
8
account_idnumber · links to accounts
2
full_nametext
Lucia Chen
emailtext
lucia.chen@vectordata2.example
roletext
member
signup_sourcetext
domain_join
referred_by_user_idnumber · links to users
NULL
created_atdate & time
2024-05-18 15:26:01
deactivated_attext
NULL
user_idnumber · unique id
9
account_idnumber · links to accounts
2
full_nametext
Hana Edwards
emailtext
hana.edwards@vectordata2.example
roletext
member
signup_sourcetext
domain_join
referred_by_user_idnumber · links to users
NULL
created_atdate & time
2024-02-01 19:03:56
deactivated_attext
NULL
user_idnumber · unique id
10
account_idnumber · links to accounts
3
full_nametext
Caleb Nakamura
emailtext
caleb.nakamura@truefoods3.example
roletext
owner
signup_sourcetext
workspace_creator
referred_by_user_idnumber · links to users
NULL
created_atdate & time
2024-01-19 11:22:02
deactivated_attext
NULL

first 10 of 11,348 rows · your queries use all of them

worked example

The step marked “the pattern” is what the challenges build on. Running examples doesn’t count toward progress.

  1. See who referred whom. Run this. referred_by_user_id points at another row in the same users table.

    step-1.sql
    SELECT user_id, account_id, referred_by_user_id
    FROM users
    WHERE referred_by_user_id IS NOT NULL
    LIMIT 10;
  2. Self-join to show referrer names. Run this. Alias u for the user and r for the referrer row matched on user_id.the pattern

    example.sql
    SELECT u.user_id, u.full_name, r.full_name AS referred_by
    FROM users u
    JOIN users r ON r.user_id = u.referred_by_user_id
    LIMIT 10;

common mistake

Without distinct aliases the column names are ambiguous, and an inner self-join drops everyone without a referrer.

challenge 1

Find every workspace creator (signup_source = 'workspace_creator').

Return user_id, referred_by_user_id and referrer_email.

Creators who were not referred keep NULL.

challenge-1.sql
 
Output
To master this lesson, solve both challenges without hints and pass the concept check. 3 of 3 to go.
Self-joins & referral relationships — Joins & sets · Runtime