# NPS survey system database schema

Source: https://personalsoftwarefactory.com/schemas/nps-surveys
Updated: 2026-12-05

> 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) on 2026-12-05
- 11 seed rows from the blueprint
- The blueprint's test build passed 17 of 17 acceptance checks

Files: [nps-surveys.sql](https://personalsoftwarefactory.com/schemas/nps-surveys.sql) · [nps-surveys-seed.sql](https://personalsoftwarefactory.com/schemas/nps-surveys-seed.sql) · [the NPS Surveys blueprint](https://personalsoftwarefactory.com/blueprints/nps-surveys)

## 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.

## Tables

Also described as: net promoter score database design, customer feedback survey database tables, NPS tracking data model.

### settings

The one row of survey settings: question, sending time and limits, throttle, internal domain and the sending switch. Implements: Settings.

| 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 only Check: `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 days Check: `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

An admin or member who signs in and can own accounts and follow-ups. Implements: Person.

| 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, with plan, account owner and survey and unsubscribe state. Implements: Contact.

| Column | Type | Null | Default | Key | Notes |
|---|---|---|---|---|---|
| `id` | TEXT | no |  | Primary key |  |
| `email` | TEXT | no |  | Unique | stored lowercase Check: `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_due` on (last_surveyed_at) where `unsubscribed_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. Implements: SurveySend.

| 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 logged Check: `status IN ('queued', 'sent', 'failed', 'held')` |

- Index `survey_sends_contact` on (contact_id, sent_on)
- Index `survey_sends_sent_on` on (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: score, category, comment and the segment copied at the first click. Implements: Response.

| 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_click` on (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 comment theme with a unique name. Implements: Tag.

| 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's list of tag ids. Implements: join table.

| 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_tag` on (tag_id)

### follow_ups

The follow-up on one detractor response, with its owner, status and due date. Implements: FollowUp.

| 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_owner` on (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 append-only note on a follow-up, with who wrote it and when. Implements: child table of FollowUp.

| 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_up` on (follow_up_id, at)

### events

An append-only record of one change, including each day's send. Implements: Event.

| 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, with headers such as List-Unsubscribe and its delivery status. Implements: Notification.

| 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 off Check: `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

```sql
-- 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.

## Load it

```sh
# SQLite
curl -s https://personalsoftwarefactory.com/schemas/nps-surveys.sql https://personalsoftwarefactory.com/schemas/nps-surveys-seed.sql | sqlite3 nps_surveys.db
# PostgreSQL
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_surveys
```

## 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

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. (This point comes from a gap the blueprint's build test found.)

### 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

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. (This point comes from a gap the blueprint's build test found.)

### Notes are append-only rows

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. (This point comes from a gap the blueprint's build test found.)

### Unsubscribe has a reason, and held sends still count

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. (This point comes from a gap the blueprint's build test found.)

## Example queries

### Send preview: who the next daily send goes to

```sql
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

```sql
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

```sql
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

```sql
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 is section 5 of the [NPS Surveys blueprint](https://personalsoftwarefactory.com/blueprints/nps-surveys), which also has the business rules, the screens, the phased build prompts and the acceptance checks. Paste-ready spec: https://personalsoftwarefactory.com/blueprints/nps-surveys/blueprint.md
