NPS survey system database schema
Eleven tables. responses carries the design: one response per survey send through a unique send_id, a score CHECKed from 0 to 10, a category CHECKed against the score, and the plan and account owner copied from the contact at the first click so past segment reports never move. Each detractor response gets exactly one follow-up, with notes as append-only child rows. Tags are a join table of tag ids, so renaming a tag keeps it on its responses.
- 11 tables, 7 indexes
- Verified on SQLite 3.53.4 and PostgreSQL 19.0 (PGlite 0.6.1),
- 11 seed rows from the blueprint
- The blueprint's test build passed 17 of 17 acceptance checks
NPS surveys entity-relationship diagram
Relationships
- contacts.account_owner_id references people.id: each contacts row points to at most one people row, and one people row can have many contacts rows pointing to it.
- survey_sends.contact_id references contacts.id: each survey_sends row points to exactly one contacts row, and one contacts row can have many survey_sends rows pointing to it.
- responses.send_id references survey_sends.id: each responses row points to exactly one survey_sends row, and one survey_sends row can have at most one responses row pointing to it.
- responses.contact_id references contacts.id: each responses row points to exactly one contacts row, and one contacts row can have many responses rows pointing to it.
- responses.account_owner_id references people.id: each responses row points to at most one people row, and one people row can have many responses rows pointing to it.
- response_tags.response_id references responses.id: each response_tags row points to exactly one responses row, and one responses row can have many response_tags rows pointing to it. On delete: cascade.
- response_tags.tag_id references tags.id: each response_tags row points to exactly one tags row, and one tags row can have many response_tags rows pointing to it.
- follow_ups.response_id references responses.id: each follow_ups row points to exactly one responses row, and one responses row can have at most one follow_ups row pointing to it.
- follow_ups.owner_id references people.id: each follow_ups row points to exactly one people row, and one people row can have many follow_ups rows pointing to it.
- follow_up_notes.follow_up_id references follow_ups.id: each follow_up_notes row points to exactly one follow_ups row, and one follow_ups row can have many follow_up_notes rows pointing to it.
- follow_up_notes.by references people.id: each follow_up_notes row points to exactly one people row, and one people row can have many follow_up_notes rows pointing to it.
NPS surveys tables
The schema has 11 tables. Every column below is read from the SQL file itself. Also described as: net promoter score database design, customer feedback survey database tables, NPS tracking data model.
| Table | Implements | What a row is |
|---|---|---|
settings | Settings | The one row of survey settings: question, sending time and limits, throttle, internal domain and the sending switch. |
people | Person | An admin or member who signs in and can own accounts and follow-ups. |
contacts | Contact | A customer who can be surveyed, with plan, account owner and survey and unsubscribe state. |
survey_sends | SurveySend | One survey email to one contact, with the token its score links carry. |
responses | Response | One answer to one survey email: score, category, comment and the segment copied at the first click. |
tags | Tag | A comment theme with a unique name. |
response_tags | join table | A tag applied to a response; stores the response's list of tag ids. |
follow_ups | FollowUp | The follow-up on one detractor response, with its owner, status and due date. |
follow_up_notes | child table of FollowUp | One append-only note on a follow-up, with who wrote it and when. |
events | Event | An append-only record of one change, including each day's send. |
notifications | Notification | One email in the outbox, with headers such as List-Unsubscribe and its delivery status. |
settings
The single settings row. id is fixed at 1 so the table can never hold a second row.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | INTEGER | no | Primary key | Check: id = 1 | |
company_name | TEXT | no | |||
from_name | TEXT | yes | sender name on survey emails | ||
from_email | TEXT | yes | sender address on survey emails | ||
question | TEXT | no | the 0 to 10 question | ||
follow_up_question | TEXT | no | asked on the page that opens after a score is clicked | ||
send_time | TEXT | no | '10:00' | HH:MM in the company time zone, weekdays onlyCheck: length(send_time) = 5 AND substr(send_time, 3, 1) = ':' | |
daily_send_limit | INTEGER | no | 100 | Check: daily_send_limit >= 1 | |
throttle_days | INTEGER | no | 90 | no contact is surveyed twice within this many daysCheck: throttle_days >= 1 | |
company_domain | TEXT | yes | addresses on this domain are internal and never surveyed | ||
sending_enabled | BOOLEAN | no | FALSE | off: survey emails are held in the outbox | |
time_zone | TEXT | no | IANA name; send days, months and due dates use this zone | ||
product_name | TEXT | no | named in the survey subject, e.g. Quick question from Tally |
people
A staff member who signs in: an admin or a member.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
name | TEXT | no | |||
email | TEXT | no | Unique | the import matches account_owner_email against it | |
role | TEXT | no | Check: role IN ('admin', 'member') | ||
active | BOOLEAN | no | TRUE |
contacts
A customer who can be surveyed. Imported by CSV, upserted by email, never deleted.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
email | TEXT | no | Unique | stored lowercaseCheck: email = lower(email) | |
name | TEXT | no | |||
company | TEXT | yes | |||
plan | TEXT | yes | free text, e.g. Pro or Starter | ||
account_owner_id | TEXT | yes | References people.id | NULL when the contact has no owner | |
created_at | TIMESTAMP | no | |||
unsubscribed_at | TIMESTAMP | yes | set once; the contact is never surveyed again | ||
unsubscribe_reason | TEXT | yes | e.g. bounced | ||
last_surveyed_at | TIMESTAMP | yes | NULL if never surveyed; held surveys count |
- Table check:
unsubscribe_reason IS NULL OR unsubscribed_at IS NOT NULL - Index
contacts_dueon(last_surveyed_at)whereunsubscribed_at IS NULL - App rule (R3): a contact is due only if not unsubscribed, not on company_domain, and never surveyed or last surveyed more than throttle_days ago.
- App rule (section 13): three failed sends in a row unsubscribe the contact with the reason bounced.
survey_sends
One survey email to one contact, with the token its score links carry.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
contact_id | TEXT | no | References contacts.id | ||
token | TEXT | no | Unique | random; score links are /s/<token>/<score> | |
sent_on | DATE | no | the company date of the send | ||
status | TEXT | no | 'queued' | sent also when the email was only loggedCheck: status IN ('queued', 'sent', 'failed', 'held') |
- Index
survey_sends_contacton(contact_id, sent_on) - Index
survey_sends_sent_onon(sent_on, status) - App rule (section 7): the daily send runs once per company date, on weekdays at or after send_time, to at most daily_send_limit due contacts.
responses
One answer to one survey email: the score, its category and the comment.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
send_id | TEXT | no | References survey_sends.id; Unique | at most one response per send | |
contact_id | TEXT | no | References contacts.id | ||
plan | TEXT | yes | copied from the contact at the first click, so segment reports never move | ||
account_owner_id | TEXT | yes | References people.id | copied from the contact at the first click | |
score | INTEGER | no | Check: score BETWEEN 0 AND 10 | ||
category | TEXT | no | Check: category IN ('promoter', 'passive', 'detractor') | ||
comment | TEXT | yes | |||
first_clicked_at | TIMESTAMP | no | decides the month and segment of the response | ||
last_changed_at | TIMESTAMP | no |
- Table check:
(score >= 9 AND category = 'promoter') OR (score IN (7, 8) AND category = 'passive') OR (score <= 6 AND category = 'detractor') - Table check:
last_changed_at >= first_clicked_at - Index
responses_first_clickon(first_clicked_at) - App rule (R1, section 7): the score and comment can change only within 24 hours of first_clicked_at.
- App rule (R5): a detractor response creates a follow-up for the account owner.
tags
A tag for comment themes. Renaming a tag keeps it on its responses.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
name | TEXT | no | Unique |
- App rule (section 4): only admins create and rename tags.
response_tags
A tag applied to a response. Stores the response tags list as Tag ids.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
response_id | TEXT | no | Primary key (part); References responses.id, on delete cascade | ||
tag_id | TEXT | no | Primary key (part); References tags.id |
- Index
response_tags_tagon(tag_id)
follow_ups
The follow-up on a detractor response, owned by one person.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
response_id | TEXT | no | References responses.id; Unique | one follow-up per response, reopened rather than recreated | |
owner_id | TEXT | no | References people.id | ||
status | TEXT | no | 'open' | Check: status IN ('open', 'contacted', 'resolved', 'no_reply', 'cancelled') | |
due_on | DATE | no | overdue when open or contacted and before today |
- Index
follow_ups_owneron(owner_id, status) - App rule (R5): due 2 business days after the first click date; a score of 7 or more cancels an open or contacted follow-up, and a score back under 7 reopens it with the same due date.
- App rule (R6): open, contacted, resolved and no_reply can move to contacted, resolved or no_reply, each with a note; only the system sets cancelled, and a cancelled follow-up is never updated.
follow_up_notes
One note on a follow-up. Stores the follow-up notes list; notes are never edited or deleted.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
follow_up_id | TEXT | no | References follow_ups.id | ||
at | TIMESTAMP | no | |||
by | TEXT | no | References people.id | the person who wrote the note | |
text | TEXT | no |
- Index
follow_up_notes_follow_upon(follow_up_id, at)
events
One change, written by every action, including each day of sending. Append-only.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
at | TIMESTAMP | no | |||
actor | TEXT | no | a person id, a customer token, or the job | ||
type | TEXT | no | e.g. daily_send, score_changed, follow_up_resolved | ||
data | TEXT | yes | details of the change as JSON text |
- App rule (R10): events are never edited or deleted.
notifications
One email in the outbox. Every email is written here first, then sent.
| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
id | TEXT | no | Primary key | ||
kind | TEXT | no | which trigger in section 10, e.g. survey, detractor_alert, weekly_digest | ||
to_email | TEXT | no | |||
subject | TEXT | no | |||
body | TEXT | no | |||
headers | TEXT | yes | extra email headers as JSON text, e.g. List-Unsubscribe | ||
status | TEXT | no | 'queued' | held when sending_enabled is offCheck: status IN ('queued', 'sent', 'failed', 'logged', 'held') | |
error | TEXT | yes |
- App rule (section 10): the sending switch holds customer surveys only; staff alerts and the digest always go.
The SQL
The whole schema in one file. It runs top to bottom, unmodified, on SQLite and PostgreSQL. Download nps-surveys.sql or copy it from here.
-- NPS Surveys database schema
-- Source: https://personalsoftwarefactory.com/blueprints/nps-surveys (data model, section 5 of the blueprint)
-- Verified on SQLite 3.53.4 and PostgreSQL 19.0 (PGlite). All timestamps are UTC.
-- License: MIT. Free to copy and adapt.
-- The single settings row. id is fixed at 1 so the table can never hold a second row.
CREATE TABLE settings (
id INTEGER PRIMARY KEY CHECK (id = 1),
company_name TEXT NOT NULL,
from_name TEXT, -- sender name on survey emails
from_email TEXT, -- sender address on survey emails
question TEXT NOT NULL, -- the 0 to 10 question
follow_up_question TEXT NOT NULL, -- asked on the page that opens after a score is clicked
send_time TEXT NOT NULL DEFAULT '10:00' CHECK (length(send_time) = 5 AND substr(send_time, 3, 1) = ':'), -- HH:MM in the company time zone, weekdays only
daily_send_limit INTEGER NOT NULL DEFAULT 100 CHECK (daily_send_limit >= 1),
throttle_days INTEGER NOT NULL DEFAULT 90 CHECK (throttle_days >= 1), -- no contact is surveyed twice within this many days
company_domain TEXT, -- addresses on this domain are internal and never surveyed
sending_enabled BOOLEAN NOT NULL DEFAULT FALSE, -- off: survey emails are held in the outbox
time_zone TEXT NOT NULL, -- IANA name; send days, months and due dates use this zone
product_name TEXT NOT NULL -- named in the survey subject, e.g. Quick question from Tally
);
-- A staff member who signs in: an admin or a member.
CREATE TABLE people (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL UNIQUE, -- the import matches account_owner_email against it
role TEXT NOT NULL CHECK (role IN ('admin', 'member')),
active BOOLEAN NOT NULL DEFAULT TRUE
);
-- A customer who can be surveyed. Imported by CSV, upserted by email, never deleted.
-- App rule (R3): a contact is due only if not unsubscribed, not on company_domain, and never surveyed or last surveyed more than throttle_days ago.
-- App rule (section 13): three failed sends in a row unsubscribe the contact with the reason bounced.
CREATE TABLE contacts (
id TEXT PRIMARY KEY,
email TEXT NOT NULL UNIQUE CHECK (email = lower(email)), -- stored lowercase
name TEXT NOT NULL,
company TEXT,
plan TEXT, -- free text, e.g. Pro or Starter
account_owner_id TEXT REFERENCES people(id), -- NULL when the contact has no owner
created_at TIMESTAMP NOT NULL,
unsubscribed_at TIMESTAMP, -- set once; the contact is never surveyed again
unsubscribe_reason TEXT, -- e.g. bounced
last_surveyed_at TIMESTAMP, -- NULL if never surveyed; held surveys count
CHECK (unsubscribe_reason IS NULL OR unsubscribed_at IS NOT NULL)
);
CREATE INDEX contacts_due ON contacts (last_surveyed_at) WHERE unsubscribed_at IS NULL;
-- One survey email to one contact, with the token its score links carry.
-- App rule (section 7): the daily send runs once per company date, on weekdays at or after send_time, to at most daily_send_limit due contacts.
CREATE TABLE survey_sends (
id TEXT PRIMARY KEY,
contact_id TEXT NOT NULL REFERENCES contacts(id),
token TEXT NOT NULL UNIQUE, -- random; score links are /s/<token>/<score>
sent_on DATE NOT NULL, -- the company date of the send
status TEXT NOT NULL DEFAULT 'queued' CHECK (status IN ('queued', 'sent', 'failed', 'held')) -- sent also when the email was only logged
);
CREATE INDEX survey_sends_contact ON survey_sends (contact_id, sent_on);
CREATE INDEX survey_sends_sent_on ON survey_sends (sent_on, status);
-- One answer to one survey email: the score, its category and the comment.
-- App rule (R1, section 7): the score and comment can change only within 24 hours of first_clicked_at.
-- App rule (R5): a detractor response creates a follow-up for the account owner.
CREATE TABLE responses (
id TEXT PRIMARY KEY,
send_id TEXT NOT NULL UNIQUE REFERENCES survey_sends(id), -- at most one response per send
contact_id TEXT NOT NULL REFERENCES contacts(id),
plan TEXT, -- copied from the contact at the first click, so segment reports never move
account_owner_id TEXT REFERENCES people(id), -- copied from the contact at the first click
score INTEGER NOT NULL CHECK (score BETWEEN 0 AND 10),
category TEXT NOT NULL CHECK (category IN ('promoter', 'passive', 'detractor')),
comment TEXT,
first_clicked_at TIMESTAMP NOT NULL, -- decides the month and segment of the response
last_changed_at TIMESTAMP NOT NULL,
CHECK ((score >= 9 AND category = 'promoter') OR (score IN (7, 8) AND category = 'passive') OR (score <= 6 AND category = 'detractor')),
CHECK (last_changed_at >= first_clicked_at)
);
CREATE INDEX responses_first_click ON responses (first_clicked_at);
-- A tag for comment themes. Renaming a tag keeps it on its responses.
-- App rule (section 4): only admins create and rename tags.
CREATE TABLE tags (
id TEXT PRIMARY KEY,
name TEXT NOT NULL UNIQUE
);
-- A tag applied to a response. Stores the response tags list as Tag ids.
CREATE TABLE response_tags (
response_id TEXT NOT NULL REFERENCES responses(id) ON DELETE CASCADE,
tag_id TEXT NOT NULL REFERENCES tags(id),
PRIMARY KEY (response_id, tag_id)
);
CREATE INDEX response_tags_tag ON response_tags (tag_id);
-- The follow-up on a detractor response, owned by one person.
-- App rule (R5): due 2 business days after the first click date; a score of 7 or more cancels an open or contacted follow-up, and a score back under 7 reopens it with the same due date.
-- App rule (R6): open, contacted, resolved and no_reply can move to contacted, resolved or no_reply, each with a note; only the system sets cancelled, and a cancelled follow-up is never updated.
CREATE TABLE follow_ups (
id TEXT PRIMARY KEY,
response_id TEXT NOT NULL UNIQUE REFERENCES responses(id), -- one follow-up per response, reopened rather than recreated
owner_id TEXT NOT NULL REFERENCES people(id),
status TEXT NOT NULL DEFAULT 'open' CHECK (status IN ('open', 'contacted', 'resolved', 'no_reply', 'cancelled')),
due_on DATE NOT NULL -- overdue when open or contacted and before today
);
CREATE INDEX follow_ups_owner ON follow_ups (owner_id, status);
-- One note on a follow-up. Stores the follow-up notes list; notes are never edited or deleted.
CREATE TABLE follow_up_notes (
id TEXT PRIMARY KEY,
follow_up_id TEXT NOT NULL REFERENCES follow_ups(id),
at TIMESTAMP NOT NULL,
by TEXT NOT NULL REFERENCES people(id), -- the person who wrote the note
text TEXT NOT NULL
);
CREATE INDEX follow_up_notes_follow_up ON follow_up_notes (follow_up_id, at);
-- One change, written by every action, including each day of sending. Append-only.
-- App rule (R10): events are never edited or deleted.
CREATE TABLE events (
id TEXT PRIMARY KEY,
at TIMESTAMP NOT NULL,
actor TEXT NOT NULL, -- a person id, a customer token, or the job
type TEXT NOT NULL, -- e.g. daily_send, score_changed, follow_up_resolved
data TEXT -- details of the change as JSON text
);
-- One email in the outbox. Every email is written here first, then sent.
-- App rule (section 10): the sending switch holds customer surveys only; staff alerts and the digest always go.
CREATE TABLE notifications (
id TEXT PRIMARY KEY,
kind TEXT NOT NULL, -- which trigger in section 10, e.g. survey, detractor_alert, weekly_digest
to_email TEXT NOT NULL,
subject TEXT NOT NULL,
body TEXT NOT NULL,
headers TEXT, -- extra email headers as JSON text, e.g. List-Unsubscribe
status TEXT NOT NULL DEFAULT 'queued' CHECK (status IN ('queued', 'sent', 'failed', 'logged', 'held')), -- held when sending_enabled is off
error TEXT
);Seed data
Loads the parts of section 14 that name real rows: settings (question and follow-up question from section 3, product Tally, domain tallyco.example, New York time, sending off), 3 people, the 4 named contacts and the 3 tags. Left out, because the spec describes them only as totals and loading them would mean inventing people and scores: the other 296 contacts, the 130 September and October sends, the 52 responses behind the worked NPS figures, their follow-ups and tag counts. Stand-ins: every contact's created_at and Cara Diaz's unsubscribe time, which the spec does not give. Dev Shah's account owner is not given, so it is NULL; from_name and from_email are not given either. Ana and Ben's last survey dates are the spec's 95 and 60 days back at the 10:00 send time, although both fall on a Sunday and sends run on weekdays.
Download nps-surveys-seed.sql (11 rows). It loads with foreign keys enforced on both engines.
Load it
SQLite, into a new file:
curl -s https://personalsoftwarefactory.com/schemas/nps-surveys.sql https://personalsoftwarefactory.com/schemas/nps-surveys-seed.sql | sqlite3 nps_surveys.dbPostgreSQL, into a new database:
createdb nps_surveys && curl -s https://personalsoftwarefactory.com/schemas/nps-surveys.sql https://personalsoftwarefactory.com/schemas/nps-surveys-seed.sql | psql -v ON_ERROR_STOP=1 nps_surveysSQLite only enforces foreign keys when the connection asks for it: run PRAGMA foreign_keys = ON; when your app opens the database.
Design decisions
The category is stored, and checked against the score
Section 6 fixes the categories: 9 and 10 promoters, 7 and 8 passives, 0 to 6 detractors, and R2 says they are calculated the same way everywhere. category is stored so NPS is a plain count, and a CHECK ties it to the score, so a changed score cannot leave a stale category behind. NPS itself is computed per query, never stored.
One response per send, one follow-up per response
From the build test · field report
R1 allows at most one response per send, so responses.send_id is UNIQUE. R5 cancels a follow-up when the score rises to 7 or more and reopens it if the score falls back, so follow_ups.response_id is UNIQUE and the row is updated rather than recreated. The build test found nothing said whether a reopened follow-up gets a new due date or a second alert; it keeps its due date and sends no second email, which a single row per response makes natural.
Plan and owner are copied onto the response
Section 5 copies plan and account_owner_id from the contact at the first click, and section 13 says a contact who changes plan keeps the old plan on past responses, so segment reports never move. The two columns duplicate contact data on purpose; reading them through the contact would rewrite last month's NPS by plan whenever someone upgrades.
Tags are a join table of tag ids
From the build test · field report
The build test found Response.tags had no stated form. It is a list of Tag ids, so renaming a tag keeps it on its responses; here that list is the response_tags join table with a composite primary key, which also stops the same tag being applied twice. Theme counts (R7) are a GROUP BY on it.
Notes are append-only rows
From the build test · field report
FollowUp.notes is a list of { at, by, text }, and R6 says notes are never edited or deleted. Each note is a follow_up_notes row with those three columns, by being a foreign key to people, so the history of a follow-up is a plain ordered query. The build test found R6 did not say whether every change needs a note; it does, so the app writes a note row with every status change. Both engines accept by and text as column names, so the spec names are kept.
Unsubscribe has a reason, and held sends still count
From the build test · field report
The build test found bounced contacts were unsubscribed with a reason but Contact had no reason field; unsubscribe_reason now exists, and a CHECK allows it only alongside unsubscribed_at. It also found held surveys were unclear: they count as surveyed and are never released, so survey_sends and notifications both have a held status and last_surveyed_at is set for them.
Example queries
The queries behind the tool's main screens. Each returned the same rows on both engines against the seed data.
Send preview: who the next daily send goes to
SELECT c.name, c.email, c.plan, c.last_surveyed_at
FROM contacts c
JOIN settings s ON s.id = 1
WHERE c.unsubscribed_at IS NULL
AND c.email NOT LIKE ('%@' || s.company_domain)
AND (c.last_surveyed_at IS NULL OR c.last_surveyed_at < '2026-07-17 04:00:00')
ORDER BY CASE WHEN c.last_surveyed_at IS NULL THEN 0 ELSE 1 END, c.last_surveyed_at, c.email
LIMIT 100;On the seed data, with a 90-day throttle on Oct 15, 2026: 1 row, Ana Lopez, last surveyed 2026-07-12. Ben Cole was surveyed 60 days ago, Cara Diaz is unsubscribed and Dev Shah is on the internal domain.
Why each contact is or is not due
SELECT c.name, COALESCE(o.name, 'No owner') AS owner,
CASE
WHEN c.unsubscribed_at IS NOT NULL THEN 'unsubscribed'
WHEN c.email LIKE ('%@' || s.company_domain) THEN 'internal'
WHEN c.last_surveyed_at IS NULL OR c.last_surveyed_at < '2026-07-17 04:00:00' THEN 'due'
ELSE 'throttled'
END AS state
FROM contacts c
JOIN settings s ON s.id = 1
LEFT JOIN people o ON o.id = c.account_owner_id
ORDER BY c.name;On the seed data: 4 rows; Ana Lopez due, Ben Cole throttled, Cara Diaz unsubscribed, Dev Shah internal.
NPS for a month
SELECT COUNT(*) AS responses,
COALESCE(SUM(CASE WHEN r.category = 'promoter' THEN 1 ELSE 0 END), 0) AS promoters,
COALESCE(SUM(CASE WHEN r.category = 'passive' THEN 1 ELSE 0 END), 0) AS passives,
COALESCE(SUM(CASE WHEN r.category = 'detractor' THEN 1 ELSE 0 END), 0) AS detractors,
CASE WHEN COUNT(*) = 0 THEN 'No responses' ELSE 'NPS' END AS label,
CASE WHEN COUNT(*) = 0 THEN NULL ELSE ROUND((SUM(CASE WHEN r.category = 'promoter' THEN 1 ELSE 0 END) - SUM(CASE WHEN r.category = 'detractor' THEN 1 ELSE 0 END)) * 100.0 / COUNT(*)) END AS nps
FROM responses r
WHERE r.first_clicked_at >= '2026-10-01 04:00:00' AND r.first_clicked_at < '2026-11-01 04:00:00';On the seed data: 1 row with 0 responses and the label No responses, because the seed does not load the survey history (see below). With section 14's October responses it would be 12 responses and an NPS of 25.
Tags with all-time counts
SELECT t.name, COUNT(rt.response_id) AS responses
FROM tags t
LEFT JOIN response_tags rt ON rt.tag_id = t.id
GROUP BY t.id, t.name
ORDER BY responses DESC, t.name;On the seed data: 3 rows, Bank feeds, Export and Pricing, each with 0 responses because the tagged comments are not loaded.
What was not tested
- MySQL, MariaDB, SQL Server and Oracle. The file avoids engine-specific syntax, but it was not run on them.
- Other versions of SQLite and PostgreSQL than the two named above.
- PostgreSQL as a server. The PostgreSQL run used PGlite, which is PostgreSQL compiled to run in-process.
- Production load. The check loads the seed rows and runs the example queries; it says nothing about performance with real volumes.
- Migrations. This is a schema for a new database, not a path from an existing one.
The blueprint this schema comes from
Tables are the easy half. This schema is section 5 of the NPS Surveys blueprint, a free spec that also has the business rules, the screens, 4 phased build prompts and 17 acceptance checks for building the whole tool with an AI app builder. We built it from those prompts and the result passed 17 of 17 acceptance checks in a browser.
- Read the NPS Surveys blueprint: rules, screens, build prompts and acceptance checks
- Read the build-test field report: what the test build got wrong and what was not tested
- All database schemas
- How we test blueprints
Follow new blueprints
Every new blueprint and build test appears in the feed and in the public GitHub repository. No account, no email list.