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
accounts7 columns · 3,200 rows
| account_idnumber · unique id | account_nametext | industrytext | company_sizetext | countrytext | acquisition_channeltext | created_atdate & time |
|---|---|---|---|---|---|---|
| 1 | Iron Capital Inc | Logistics | 11-50 | United States | organic | 2024-01-14 18:52:14 |
| 2 | Vector Data Co | Nonprofit | 51-200 | United Kingdom | social | 2024-01-15 13:21:52 |
| 3 | True Foods Inc | Healthcare | 201-1000 | United States | outbound_sales | 2024-01-19 11:22:02 |
| 4 | Meridian Works | Software | 11-50 | United States | organic | 2024-01-23 23:42:36 |
| 5 | Prism Health Group | Software | 1-10 | Germany | referral | 2024-01-25 11:43:43 |
| 6 | Beacon Supply Ltd | Media | 1000+ | United Kingdom | organic | 2024-01-27 20:19:28 |
| 7 | Birch Health Group | Software | 11-50 | United States | paid_search | 2024-01-28 01:19:58 |
| 8 | Slate Robotics Group | Education | 51-200 | United States | paid_search | 2024-01-28 08:03:16 |
| 9 | Falcon Learning | Manufacturing | 51-200 | United States | outbound_sales | 2024-02-02 05:14:26 |
| 10 | Pine Software Co | Healthcare | 1-10 | United States | social | 2024-02-04 18:06:48 |
- 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
- account_idnumber · unique id
- 7
- account_nametext
- Birch Health Group
- industrytext
- Software
- company_sizetext
- 11-50
- countrytext
- United States
- acquisition_channeltext
- paid_search
- created_atdate & time
- 2024-01-28 01:19:58
- account_idnumber · unique id
- 8
- account_nametext
- Slate Robotics Group
- industrytext
- Education
- company_sizetext
- 51-200
- countrytext
- United States
- acquisition_channeltext
- paid_search
- created_atdate & time
- 2024-01-28 08:03:16
- account_idnumber · unique id
- 9
- account_nametext
- Falcon Learning
- industrytext
- Manufacturing
- company_sizetext
- 51-200
- countrytext
- United States
- acquisition_channeltext
- outbound_sales
- created_atdate & time
- 2024-02-02 05:14:26
- account_idnumber · unique id
- 10
- account_nametext
- Pine Software Co
- industrytext
- Healthcare
- company_sizetext
- 1-10
- countrytext
- United States
- acquisition_channeltext
- social
- created_atdate & time
- 2024-02-04 18:06:48
first 10 of 3,200 rows · your queries use all of them
projects6 columns · 9,962 rows
| project_idnumber · unique id | account_idnumber · links to accounts | created_by_user_idnumber · links to users | project_nametext | created_atdate & time | archived_atdate & time |
|---|---|---|---|---|---|
| 1 | 1 | 1 | Marketing Calendar | 2024-02-23 16:32:02 | NULL |
| 2 | 1 | 1 | Customer Onboarding 2 | 2024-02-23 16:33:07 | NULL |
| 3 | 1 | 1 | Website Redesign 3 | 2024-02-23 16:36:34 | 2024-05-15 08:53:27 |
| 4 | 1 | 2 | Marketing Calendar | 2024-01-27 00:29:22 | NULL |
| 5 | 1 | 2 | Sales Pipeline 2 | 2024-02-03 17:23:30 | NULL |
| 6 | 1 | 3 | Marketing Calendar | 2024-02-13 09:10:19 | NULL |
| 7 | 1 | 3 | Data Migration 2 | 2024-02-14 10:21:49 | NULL |
| 8 | 1 | 3 | Q3 Planning 3 | 2024-03-13 21:16:48 | 2024-11-02 21:27:40 |
| 9 | 3 | 11 | Weekly Metrics | 2024-01-29 21:43:07 | NULL |
| 10 | 3 | 11 | Sales Pipeline 2 | 2024-02-21 15:48:10 | NULL |
- 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
- project_idnumber · unique id
- 7
- account_idnumber · links to accounts
- 1
- created_by_user_idnumber · links to users
- 3
- project_nametext
- Data Migration 2
- created_atdate & time
- 2024-02-14 10:21:49
- archived_atdate & time
- NULL
- project_idnumber · unique id
- 8
- account_idnumber · links to accounts
- 1
- created_by_user_idnumber · links to users
- 3
- project_nametext
- Q3 Planning 3
- created_atdate & time
- 2024-03-13 21:16:48
- archived_atdate & time
- 2024-11-02 21:27:40
- project_idnumber · unique id
- 9
- account_idnumber · links to accounts
- 3
- created_by_user_idnumber · links to users
- 11
- project_nametext
- Weekly Metrics
- created_atdate & time
- 2024-01-29 21:43:07
- archived_atdate & time
- NULL
- project_idnumber · unique id
- 10
- account_idnumber · links to accounts
- 3
- created_by_user_idnumber · links to users
- 11
- project_nametext
- Sales Pipeline 2
- created_atdate & time
- 2024-02-21 15:48:10
- archived_atdate & time
- NULL
first 10 of 9,962 rows · your queries use all of them
invitations7 columns · 6,450 rows
| invitation_idnumber · unique id | account_idnumber · links to accounts | invited_by_user_idnumber · links to users | invitee_emailtext | sent_atdate & time | accepted_atdate & time | accepted_user_idnumber · links to users |
|---|---|---|---|---|---|---|
| 1 | 1 | 1 | deepa.bell@ironcapital1.example | 2024-01-21 11:24:15 | 2024-01-24 03:43:14 | 2 |
| 2 | 1 | 1 | sofia.wilson@mail.example | 2024-02-02 07:45:53 | 2024-02-02 21:34:25 | 3 |
| 3 | 2 | 6 | nadia.flores@vectordata2.example | 2024-01-25 09:18:58 | NULL | NULL |
| 4 | 2 | 6 | rosa.jones@vectordata2.example | 2024-02-03 06:27:24 | 2024-02-03 07:03:46 | 7 |
| 5 | 3 | 10 | raj.li@truefoods3.example | 2024-01-20 15:16:56 | NULL | NULL |
| 6 | 3 | 10 | ayesha.silva@truefoods3.example | 2024-01-24 19:32:04 | 2024-01-26 05:58:03 | 11 |
| 7 | 4 | 15 | ava.morales@mail.example | 2024-02-01 23:08:41 | NULL | NULL |
| 8 | 4 | 15 | rebecca.anderson@mail.example | 2024-02-09 00:04:33 | 2024-02-09 00:58:01 | 16 |
| 9 | 5 | 19 | rana.nguyen+work@prismhealth5.example | 2024-01-28 13:07:28 | 2024-01-28 19:27:51 | 20 |
| 10 | 5 | 19 | abigail.price@prismhealth5.example | 2024-02-01 07:22:28 | NULL | NULL |
- 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
- invitation_idnumber · unique id
- 7
- account_idnumber · links to accounts
- 4
- invited_by_user_idnumber · links to users
- 15
- invitee_emailtext
- ava.morales@mail.example
- sent_atdate & time
- 2024-02-01 23:08:41
- accepted_atdate & time
- NULL
- accepted_user_idnumber · links to users
- NULL
- invitation_idnumber · unique id
- 8
- account_idnumber · links to accounts
- 4
- invited_by_user_idnumber · links to users
- 15
- invitee_emailtext
- rebecca.anderson@mail.example
- sent_atdate & time
- 2024-02-09 00:04:33
- accepted_atdate & time
- 2024-02-09 00:58:01
- accepted_user_idnumber · links to users
- 16
- invitation_idnumber · unique id
- 9
- account_idnumber · links to accounts
- 5
- invited_by_user_idnumber · links to users
- 19
- invitee_emailtext
- rana.nguyen+work@prismhealth5.example
- sent_atdate & time
- 2024-01-28 13:07:28
- accepted_atdate & time
- 2024-01-28 19:27:51
- accepted_user_idnumber · links to users
- 20
- invitation_idnumber · unique id
- 10
- account_idnumber · links to accounts
- 5
- invited_by_user_idnumber · links to users
- 19
- invitee_emailtext
- abigail.price@prismhealth5.example
- sent_atdate & time
- 2024-02-01 07:22:28
- accepted_atdate & time
- NULL
- accepted_user_idnumber · links to users
- NULL
first 10 of 6,450 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.
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.sqlSELECT 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;
Count matches, not the unmatched row. Run this for one workspace. COUNT(project_id) is 0 when that workspace has no projects. The challenge asks for every workspace, not only the empty ones.
step-2.sqlSELECT a.account_id, COUNT(p.project_id) AS projects FROM accounts a LEFT JOIN projects p ON p.account_id = a.account_id WHERE a.account_id = 1 GROUP BY a.account_id;
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.