runtime
70 / 88

lesson 70 Performance & query plans · advanced

Review AI-generated SQL with evidence

Check generated SQL against real results and requirements.

the idea

Treat generated SQL like any other code review: check the grain, the join type, the denominator and NULL handling against a small known case before trusting the output. Typical defects are inner joins that drop zero rows, COUNT(*) after a LEFT JOIN, and integer division.

Dataset: Product / SaaS Analytics

The tables the challenges use, with their first 6 rows. Under each column name: the kind of value it holds, and which table it links to.

accounts7 columns · 3,200 rows
account_idnumber · unique idaccount_nametextindustrytextcompany_sizetextcountrytextacquisition_channeltextcreated_atdate & time
1Iron Capital IncLogistics11-50United Statesorganic2024-01-14 18:52:14
2Vector Data CoNonprofit51-200United Kingdomsocial2024-01-15 13:21:52
3True Foods IncHealthcare201-1000United Statesoutbound_sales2024-01-19 11:22:02
4Meridian WorksSoftware11-50United Statesorganic2024-01-23 23:42:36
5Prism Health GroupSoftware1-10Germanyreferral2024-01-25 11:43:43
6Beacon Supply LtdMedia1000+United Kingdomorganic2024-01-27 20:19:28
account_idnumber · unique id
1
account_nametext
Iron Capital Inc
industrytext
Logistics
company_sizetext
11-50
countrytext
United States
acquisition_channeltext
organic
created_atdate & time
2024-01-14 18:52:14
account_idnumber · unique id
2
account_nametext
Vector Data Co
industrytext
Nonprofit
company_sizetext
51-200
countrytext
United Kingdom
acquisition_channeltext
social
created_atdate & time
2024-01-15 13:21:52
account_idnumber · unique id
3
account_nametext
True Foods Inc
industrytext
Healthcare
company_sizetext
201-1000
countrytext
United States
acquisition_channeltext
outbound_sales
created_atdate & time
2024-01-19 11:22:02
account_idnumber · unique id
4
account_nametext
Meridian Works
industrytext
Software
company_sizetext
11-50
countrytext
United States
acquisition_channeltext
organic
created_atdate & time
2024-01-23 23:42:36
account_idnumber · unique id
5
account_nametext
Prism Health Group
industrytext
Software
company_sizetext
1-10
countrytext
Germany
acquisition_channeltext
referral
created_atdate & time
2024-01-25 11:43:43
account_idnumber · unique id
6
account_nametext
Beacon Supply Ltd
industrytext
Media
company_sizetext
1000+
countrytext
United Kingdom
acquisition_channeltext
organic
created_atdate & time
2024-01-27 20:19:28

first 6 of 3,200 rows · your queries use all of them

projects6 columns · 9,962 rows
project_idnumber · unique idaccount_idnumber · links to accountscreated_by_user_idnumber · links to usersproject_nametextcreated_atdate & timearchived_atdate & time
111Marketing Calendar2024-02-23 16:32:02NULL
211Customer Onboarding 22024-02-23 16:33:07NULL
311Website Redesign 32024-02-23 16:36:342024-05-15 08:53:27
412Marketing Calendar2024-01-27 00:29:22NULL
512Sales Pipeline 22024-02-03 17:23:30NULL
613Marketing Calendar2024-02-13 09:10:19NULL
project_idnumber · unique id
1
account_idnumber · links to accounts
1
created_by_user_idnumber · links to users
1
project_nametext
Marketing Calendar
created_atdate & time
2024-02-23 16:32:02
archived_atdate & time
NULL
project_idnumber · unique id
2
account_idnumber · links to accounts
1
created_by_user_idnumber · links to users
1
project_nametext
Customer Onboarding 2
created_atdate & time
2024-02-23 16:33:07
archived_atdate & time
NULL
project_idnumber · unique id
3
account_idnumber · links to accounts
1
created_by_user_idnumber · links to users
1
project_nametext
Website Redesign 3
created_atdate & time
2024-02-23 16:36:34
archived_atdate & time
2024-05-15 08:53:27
project_idnumber · unique id
4
account_idnumber · links to accounts
1
created_by_user_idnumber · links to users
2
project_nametext
Marketing Calendar
created_atdate & time
2024-01-27 00:29:22
archived_atdate & time
NULL
project_idnumber · unique id
5
account_idnumber · links to accounts
1
created_by_user_idnumber · links to users
2
project_nametext
Sales Pipeline 2
created_atdate & time
2024-02-03 17:23:30
archived_atdate & time
NULL
project_idnumber · unique id
6
account_idnumber · links to accounts
1
created_by_user_idnumber · links to users
3
project_nametext
Marketing Calendar
created_atdate & time
2024-02-13 09:10:19
archived_atdate & time
NULL

first 6 of 9,962 rows · your queries use all of them

invitations7 columns · 6,450 rows
invitation_idnumber · unique idaccount_idnumber · links to accountsinvited_by_user_idnumber · links to usersinvitee_emailtextsent_atdate & timeaccepted_atdate & timeaccepted_user_idnumber · links to users
111deepa.bell@ironcapital1.example2024-01-21 11:24:152024-01-24 03:43:142
211sofia.wilson@mail.example2024-02-02 07:45:532024-02-02 21:34:253
326nadia.flores@vectordata2.example2024-01-25 09:18:58NULLNULL
426rosa.jones@vectordata2.example2024-02-03 06:27:242024-02-03 07:03:467
5310raj.li@truefoods3.example2024-01-20 15:16:56NULLNULL
6310ayesha.silva@truefoods3.example2024-01-24 19:32:042024-01-26 05:58:0311
invitation_idnumber · unique id
1
account_idnumber · links to accounts
1
invited_by_user_idnumber · links to users
1
invitee_emailtext
deepa.bell@ironcapital1.example
sent_atdate & time
2024-01-21 11:24:15
accepted_atdate & time
2024-01-24 03:43:14
accepted_user_idnumber · links to users
2
invitation_idnumber · unique id
2
account_idnumber · links to accounts
1
invited_by_user_idnumber · links to users
1
invitee_emailtext
sofia.wilson@mail.example
sent_atdate & time
2024-02-02 07:45:53
accepted_atdate & time
2024-02-02 21:34:25
accepted_user_idnumber · links to users
3
invitation_idnumber · unique id
3
account_idnumber · links to accounts
2
invited_by_user_idnumber · links to users
6
invitee_emailtext
nadia.flores@vectordata2.example
sent_atdate & time
2024-01-25 09:18:58
accepted_atdate & time
NULL
accepted_user_idnumber · links to users
NULL
invitation_idnumber · unique id
4
account_idnumber · links to accounts
2
invited_by_user_idnumber · links to users
6
invitee_emailtext
rosa.jones@vectordata2.example
sent_atdate & time
2024-02-03 06:27:24
accepted_atdate & time
2024-02-03 07:03:46
accepted_user_idnumber · links to users
7
invitation_idnumber · unique id
5
account_idnumber · links to accounts
3
invited_by_user_idnumber · links to users
10
invitee_emailtext
raj.li@truefoods3.example
sent_atdate & time
2024-01-20 15:16:56
accepted_atdate & time
NULL
accepted_user_idnumber · links to users
NULL
invitation_idnumber · unique id
6
account_idnumber · links to accounts
3
invited_by_user_idnumber · links to users
10
invitee_emailtext
ayesha.silva@truefoods3.example
sent_atdate & time
2024-01-24 19:32:04
accepted_atdate & time
2024-01-26 05:58:03
accepted_user_idnumber · links to users
11

first 6 of 6,450 rows · your queries use all of them

worked example

Run these steps in order to see the idea before you write your own query. The step marked “the pattern” is what the challenges ask for. Running the example does not count toward progress.

  1. Spot the bad LEFT JOIN count. Run the assistant’s COUNT(*) version. Workspaces with zero projects can still show 1 because the unmatched join row counts as one row in the group.

    step-1.sql
    SELECT a.account_id, COUNT(*) AS projects
    FROM accounts a
    LEFT JOIN projects p ON p.account_id = a.account_id
    GROUP BY a.account_id
    HAVING COUNT(p.project_id) = 0
    LIMIT 5;
  2. List workspaces with no projects. HAVING COUNT(p.project_id) = 0 finds accounts that truly have no projects—the corrected metric is COUNT(p.project_id), not COUNT(*).

    step-2.sql
    SELECT a.account_id, COUNT(p.project_id) AS projects
    FROM accounts a
    LEFT JOIN projects p ON p.account_id = a.account_id
    GROUP BY a.account_id
    HAVING COUNT(p.project_id) = 0
    LIMIT 5;
  3. Put it together. The lesson's pattern in one query.the pattern

    example.sql
    -- Proposed by an assistant: count projects per workspace
    
    SELECT a.account_id, COUNT(*) AS projects
    FROM accounts a
    LEFT JOIN projects p ON p.account_id = a.account_id
    GROUP BY a.account_id;

common mistake

A query that runs and returns plausible numbers can still be wrong; compare it with a hand-checked example.

challenge 1

An assistant proposed counting projects per workspace with COUNT(*) after a LEFT JOIN, which reports 1 for workspaces with none.

Return the corrected account_id and projects for every workspace (0 when none).

what to return

Columns
account_idprojects in this order
Rows
any order
challenge-1.sql
 
To master this lesson, solve both challenges without hints and pass the concept check. 3 of 3 to go.
Review AI-generated SQL with evidence — Performance & query plans · Runtime