DB Olympics

Queries · q105 · Chief Information Security Officer · week 1, night 2

Privileged role landscape by role name, company-wide

“Beyond the default 'Owner' role (which filled the entire first page of the prior report), what other roles across merchants carry significant permissions, how widely are they assigned, and how dormant are the users holding them?”

Manager · asked the questionHenrik AaltoChief Information Security Officer. Joined from a cloud provider's security team. Several enterprise deals depend on passing security reviews.
Analyst · planned the reportNoah FischerSystems thinker: maps how one process feeds the next before measuring anything.
Engineer · wrote the SQLOmarPragmatic: the shortest correct query, aggregates first and joins dimensions afterwards, FILTER clauses for conditional counts.

Fictional people at a fictional company, each played by a language-model agent. Who they are and what they are told

  1. Henrik Aalto writes to Noah Fischer

    The manager, at the start of week 1

    Subject: Where I'd like your help first — account security snapshot

    Hi Noah,

    Good to have you working with me this week. I'm still building my mental map of the business, so let's start broad and sharpen later. Here's what's on my mind, roughly in priority order:

    1. How exposed are we on sign-in security today? I want to understand how merchant staff and shoppers are authenticating — are we seeing risky patterns like repeated failed logins, credential-stuffing-type behavior, or logins from unusual places? I need a feel for the baseline before I can say whether we have a problem.
    2. How strong is our multi-factor authentication posture? I'd like to know roughly how many of our merchants' staff (and ideally shoppers, if meaningful) actually have MFA set up and active, versus how many are relying on password-only access. This matters because it's one of the first things enterprise security reviewers ask about, and I need to know whether we can answer confidently or whether it's a gap we need to close.
    3. Who holds powerful access, and is it still appropriate? I'm interested in how permissions and roles are granted and whether there's good hygiene around it — e.g., are there dormant privileged accounts, or grants that nobody seems to be reviewing. This feeds directly into access-control questions in security audits.
    4. How healthy are our API keys and service accounts? These are often the weakest link — long-lived tokens that nobody rotates. I'd like a sense of how many are active, how old they are, and whether any look abandoned or over-privileged.
    5. Can we actually reconstruct "who did what, when" if asked? I want to understand how complete and reliable our audit trail is in practice — are sensitive actions consistently logged, and would we be able to produce a credible record if a merchant or regulator asked us to investigate an incident?

    What I need this for: I'm walking into enterprise deals where security questionnaires will ask us point-blank about authentication strength, access governance, credential hygiene, and auditability. I need an honest, evidence-based picture — good or bad — so I can brief the CEO and sales on what we can credibly claim today versus what needs fixing before the next review cycle.

    Given the slot budget, let's pick whichever 3 of these will give us the most defensible starting picture this week, and we can go deeper on the rest next week.

    Thanks, Henrik

  2. Noah Fischer plans the night

    The analyst · week 1, night 2, request 3 of 3

    Reading critically: (1) login volumes look like a calm baseline — failure rate holds 5-8% and avg attempts-per-IP stays ~1.5 all 26 weeks, which doesn't look like credential stuffing. But the week of 2025-11-24 jumps ~41% in total attempts (and max-attempts-per-single-IP jumps to 24, the second-highest in the series) while failure rate stays normal — plausibly just Black Friday/Cyber Monday shopping traffic, but I want to confirm it's broad-based across tenants rather than concentrated on one tenant/IP before ruling out an attack. (2) MFA report 2 is a bombshell: shopper MFA is 0.0% for every one of the 50 worst-covered tenants, and staff MFA tops out at 50% even among the better of that bottom 50 — but this report only shows the worst half of tenants by staff rate, so I don't yet have the company-wide headline number Henrik actually needs to quote, nor do I know if MFA adoption is improving over time or flat. (3) Report 3's 50-row page got entirely consumed by the single "Owner" system role (26 permissions, tied for the most) across ~50 tenants, out of 434 qualifying tenant-role rows — I haven't seen any non-Owner privileged roles yet, so I can't say anything about custom role hygiene. I did spot two dormant privileged accounts worth keeping (Aurum & Ash: Owner account active in status but last login 2024-12-26, over a year stale, on a churned tenant; Halden Boots: Owner last login 2025-10-03). Tonight's three requests: a monthly MFA-adoption trend giving the defensible overall number and trajectory, a tenant breakdown of the anomalous Nov 24 week to test the Black-Friday-vs-attack hypothesis, and a company-wide rollup by role name (collapsing the 434 tenant-role rows) to see privileged access beyond "Owner" and its dormancy rate.

    Privileged role landscape by role name, company-wide
    Question
    Beyond the default 'Owner' role (which filled the entire first page of the prior report), what other roles across merchants carry significant permissions, how widely are they assigned, and how dormant are the users holding them?
    One row per
    distinct role name (roles.name), aggregated across all tenants that have a role with that name
    Columns
    • role name
    • tenant count (distinct tenants with a role of this name)
    • share of those instances flagged is_system (%)
    • average permission count granted (across tenant instances of this role)
    • max permission count seen for this role name
    • total users currently assigned (summed across tenants, via user_roles, excluding deleted users)
    • total users dormant (status = active and last_login_at is null or before 2025-10-05, summed across tenants)
    • dormant rate among assigned users (%)
  3. Omar writes the SQL

    The engineer · 1 dry run on the empty schema, then the submission

    Dry run 1 · STATUS: COMPILES (dry run on the empty schema, no data read)
    WITH role_instance AS (
      SELECT r.id AS role_id, r.tenant_id, r.name, r.is_system,
             COUNT(DISTINCT rp.permission_id) AS perm_count
      FROM roles r
      JOIN role_permissions rp ON rp.role_id = r.id
      GROUP BY r.id, r.tenant_id, r.name, r.is_system
    ),
    assigned AS (
      SELECT ur.role_id,
             COUNT(DISTINCT u.id) AS assigned_users,
             COUNT(DISTINCT u.id) FILTER (WHERE u.status = 'active' AND (u.last_login_at IS NULL OR u.last_login_at < DATE '2025-10-05')) AS dormant_users
      FROM user_roles ur
      JOIN users u ON u.id = ur.user_id AND u.tenant_id = ur.tenant_id
      WHERE u.deleted_at IS NULL
      GROUP BY ur.role_id
    ),
    qualifying AS (
      SELECT ri.tenant_id, ri.name, ri.is_system, ri.perm_count,
             a.assigned_users, a.dormant_users
      FROM role_instance ri
      JOIN assigned a ON a.role_id = ri.role_id
      WHERE ri.perm_count >= 1 AND a.assigned_users >= 1
    )
    SELECT
      name AS role_name,
      COUNT(DISTINCT tenant_id) AS tenant_count,
      ROUND(100.0 * SUM(CASE WHEN is_system THEN 1 ELSE 0 END) / COUNT(*), 1) AS pct_is_system,
      ROUND(AVG(perm_count), 1) AS avg_permission_count,
      MAX(perm_count) AS max_permission_count,
      SUM(assigned_users) AS total_users_assigned,
      SUM(dormant_users) AS total_users_dormant,
      ROUND(100.0 * SUM(dormant_users) / NULLIF(SUM(assigned_users), 0), 1) AS dormant_rate_pct
    FROM qualifying
    WHERE name <> 'Owner'
    GROUP BY name
    ORDER BY total_users_assigned DESC, role_name
    LIMIT 50

    “Query compiles and matches the requested grain and columns. Submitting now.”

    Submitted · the SQL of the last dry run, unchanged · Submitted to tonight's batch. Your work on this request is done.

    “Submitted. Summary of what the query does: - Role instances: roles joined to role_permissions, grouped per (role_id, tenant_id, name, is_system) to get a permission count per role instance. - Assignment & dormancy: user_roles joined to users (excluding soft-deleted users), aggregated per role_id into assigned-user counts and dormant-user counts (active status, and last login null or before 2025-10-05). - Qualifying set: role instances with ≥1 permission and ≥1 assigned non-deleted user — matching the prior report's population — with 'Owner' excluded per the analyst's framing. - Final grouping: by role_name across all tenants, computing tenant count, % flagged is_system, average/max permission counts, total assigned, total dormant, and dormant rate, ordered by total assigned descending (tie-break on role name), capped at 50 rows.”

  4. The report comes back overnight

    As Noah Fischer saw it, from the small dataset the agents worked on

    role_name     | tenant_count | pct_is_system | avg_permission_count | max_permission_count | total_users_assigned | total_users_dormant | dormant_rate_pct
    --------------+--------------+---------------+----------------------+----------------------+----------------------+---------------------+-----------------
    Support Agent |          120 |         100.0 |                  6.0 |                    6 |                  234 |                  10 |              4.3
    Administrator |          120 |         100.0 |                 25.0 |                   25 |                  122 |                   8 |              6.6
    Warehouse     |           33 |         100.0 |                  4.0 |                    4 |                   37 |                   0 |              0.0
    Marketing     |           18 |         100.0 |                  5.0 |                    5 |                   19 |                   0 |              0.0
    Read-only     |            8 |           0.0 |                  4.0 |                    4 |                   10 |                   0 |              0.0
    Merchandiser  |            8 |         100.0 |                  4.0 |                    4 |                    8 |                   0 |              0.0
    Finance       |            4 |         100.0 |                  5.0 |                    5 |                    4 |                   0 |              0.0
    Sales Rep     |            3 |         100.0 |                  5.0 |                    5 |                    3 |                   0 |              0.0
    (8 rows)
  5. What came of it

    The memo and the queries that followed; links found by title, so inferred

    Later queries that build on this report:

    It builds on:

  6. It becomes benchmark query q105

    Changed only where the benchmark needs the same result on every system

    • spellingfetch first
    • numericsdocs/NUMERICS.md: rounding removed; results keep full precision; AVG over an integer computed as a decimal: AVG(CAST(x AS NUMERIC(18,6)))
    • numericsdocs/NUMERICS.md: rounding removed; results keep full precision
    • made explicita selected t.col gets AS col, its standard name made explicit (10×)
    • determinismthe standard leaves the place of NULLs in a sort to the implementation: NULLS LAST ascending, NULLS FIRST descending, as in the reference results (2×)
    • placeholderfetch first
    The benchmark text, against the submitted SQL
    WITH role_instance AS (  SELECT r.id AS role_id, r.tenant_id, r.name, r.is_system,  SELECT r.id AS role_id, r.tenant_id AS tenant_id, r.name AS name, r.is_system AS is_system,         COUNT(DISTINCT rp.permission_id) AS perm_count  FROM roles r  JOIN role_permissions rp ON rp.role_id = r.id  GROUP BY r.id, r.tenant_id, r.name, r.is_system),assigned AS (  SELECT ur.role_id,  SELECT ur.role_id AS role_id,         COUNT(DISTINCT u.id) AS assigned_users,         COUNT(DISTINCT u.id) FILTER (WHERE u.status = 'active' AND (u.last_login_at IS NULL OR u.last_login_at < DATE '2025-10-05')) AS dormant_users  FROM user_roles ur  JOIN users u ON u.id = ur.user_id AND u.tenant_id = ur.tenant_id  WHERE u.deleted_at IS NULL  GROUP BY ur.role_id),qualifying AS (  SELECT ri.tenant_id, ri.name, ri.is_system, ri.perm_count,         a.assigned_users, a.dormant_users  SELECT ri.tenant_id AS tenant_id, ri.name AS name, ri.is_system AS is_system, ri.perm_count AS perm_count,         a.assigned_users AS assigned_users, a.dormant_users AS dormant_users  FROM role_instance ri  JOIN assigned a ON a.role_id = ri.role_id  WHERE ri.perm_count >= 1 AND a.assigned_users >= 1)SELECT  name AS role_name,  COUNT(DISTINCT tenant_id) AS tenant_count,  ROUND(100.0 * SUM(CASE WHEN is_system THEN 1 ELSE 0 END) / COUNT(*), 1) AS pct_is_system,  ROUND(AVG(perm_count), 1) AS avg_permission_count,  100.0 * SUM(CASE WHEN is_system THEN 1 ELSE 0 END) / COUNT(*) AS pct_is_system,  AVG(CAST(perm_count AS NUMERIC(18,6))) AS avg_permission_count,  MAX(perm_count) AS max_permission_count,  SUM(assigned_users) AS total_users_assigned,  SUM(dormant_users) AS total_users_dormant,  ROUND(100.0 * SUM(dormant_users) / NULLIF(SUM(assigned_users), 0), 1) AS dormant_rate_pct  100.0 * SUM(dormant_users) / NULLIF(SUM(assigned_users), 0) AS dormant_rate_pctFROM qualifyingWHERE name <> 'Owner'GROUP BY nameORDER BY total_users_assigned DESC, role_nameLIMIT 50ORDER BY total_users_assigned DESC NULLS FIRST, role_name NULLS LAST{{fetch_first(50)}}
    WITH role_instance AS (
      SELECT r.id AS role_id, r.tenant_id AS tenant_id, r.name AS name, r.is_system AS is_system,
             COUNT(DISTINCT rp.permission_id) AS perm_count
      FROM roles r
      JOIN role_permissions rp ON rp.role_id = r.id
      GROUP BY r.id, r.tenant_id, r.name, r.is_system
    ),
    assigned AS (
      SELECT ur.role_id AS role_id,
             COUNT(DISTINCT u.id) AS assigned_users,
             COUNT(DISTINCT u.id) FILTER (WHERE u.status = 'active' AND (u.last_login_at IS NULL OR u.last_login_at < DATE '2025-10-05')) AS dormant_users
      FROM user_roles ur
      JOIN users u ON u.id = ur.user_id AND u.tenant_id = ur.tenant_id
      WHERE u.deleted_at IS NULL
      GROUP BY ur.role_id
    ),
    qualifying AS (
      SELECT ri.tenant_id AS tenant_id, ri.name AS name, ri.is_system AS is_system, ri.perm_count AS perm_count,
             a.assigned_users AS assigned_users, a.dormant_users AS dormant_users
      FROM role_instance ri
      JOIN assigned a ON a.role_id = ri.role_id
      WHERE ri.perm_count >= 1 AND a.assigned_users >= 1
    )
    SELECT
      name AS role_name,
      COUNT(DISTINCT tenant_id) AS tenant_count,
      100.0 * SUM(CASE WHEN is_system THEN 1 ELSE 0 END) / COUNT(*) AS pct_is_system,
      AVG(CAST(perm_count AS NUMERIC(18,6))) AS avg_permission_count,
      MAX(perm_count) AS max_permission_count,
      SUM(assigned_users) AS total_users_assigned,
      SUM(dormant_users) AS total_users_dormant,
      100.0 * SUM(dormant_users) / NULLIF(SUM(assigned_users), 0) AS dormant_rate_pct
    FROM qualifying
    WHERE name <> 'Owner'
    GROUP BY name
    ORDER BY total_users_assigned DESC NULLS FIRST, role_name NULLS LAST
    FETCH FIRST 50 ROWS ONLY
    10 ms100 msHyper11.0 msDuckDB39.9 msGizmoSQL54.6 msclickhouse_brew82.8 msFirebolt66.3 msDataFusion82.3 msClickHouse39.9 ms
  7. Everything, as recorded

    The agents' own records, as the role play wrote them; only the personal data of the person who ran it is redacted

    Omar's whole session, 7 turns
    • The request, as Omar received it
      Report request from Noah Fischer (analyst for the Chief Information Security Officer):
      
      Title: Privileged role landscape by role name, company-wide
      Question: Beyond the default 'Owner' role (which filled the entire first page of the prior report), what other roles across merchants carry significant permissions, how widely are they assigned, and how dormant are the users holding them?
      Grain: one row per distinct role name (roles.name), aggregated across all tenants that have a role with that name
      Columns:
      - role name
      - tenant count (distinct tenants with a role of this name)
      - share of those instances flagged is_system (%)
      - average permission count granted (across tenant instances of this role)
      - max permission count seen for this role name
      - total users currently assigned (summed across tenants, via user_roles, excluding deleted users)
      - total users dormant (status = active and last_login_at is null or before 2025-10-05, summed across tenants)
      - dormant rate among assigned users (%)
      Details: Source: roles joined to role_permissions (for permission counts per role instance), user_roles joined to users (excluding users.deleted_at is not null) for assignment and dormancy. Restrict to role instances with >=1 permission and >=1 assigned non-deleted user (same population as the prior 'Privileged roles per merchant' report, i.e. drawn from that same 434-row qualifying set, but now grouped by role_name instead of by tenant+role so the whole landscape fits on one page). 'Dormant' = users.status = 'active' and (last_login_at is null or last_login_at < 2025-10-05). Dormant rate = total dormant / total assigned, rounded to 1 decimal percent. Order by total users assigned descending, then role name as tie-break; cap at 50 rows and note how many distinct role names exist in total.
      
      Check your query with the warehouse's dry_run tool, then hand it in with submit.
    • Omar calls dry_run
      WITH role_instance AS (
        SELECT r.id AS role_id, r.tenant_id, r.name, r.is_system,
               COUNT(DISTINCT rp.permission_id) AS perm_count
        FROM roles r
        JOIN role_permissions rp ON rp.role_id = r.id
        GROUP BY r.id, r.tenant_id, r.name, r.is_system
      ),
      assigned AS (
        SELECT ur.role_id,
               COUNT(DISTINCT u.id) AS assigned_users,
               COUNT(DISTINCT u.id) FILTER (WHERE u.status = 'active' AND (u.last_login_at IS NULL OR u.last_login_at < DATE '2025-10-05')) AS dormant_users
        FROM user_roles ur
        JOIN users u ON u.id = ur.user_id AND u.tenant_id = ur.tenant_id
        WHERE u.deleted_at IS NULL
        GROUP BY ur.role_id
      ),
      qualifying AS (
        SELECT ri.tenant_id, ri.name, ri.is_system, ri.perm_count,
               a.assigned_users, a.dormant_users
        FROM role_instance ri
        JOIN assigned a ON a.role_id = ri.role_id
        WHERE ri.perm_count >= 1 AND a.assigned_users >= 1
      )
      SELECT
        name AS role_name,
        COUNT(DISTINCT tenant_id) AS tenant_count,
        ROUND(100.0 * SUM(CASE WHEN is_system THEN 1 ELSE 0 END) / COUNT(*), 1) AS pct_is_system,
        ROUND(AVG(perm_count), 1) AS avg_permission_count,
        MAX(perm_count) AS max_permission_count,
        SUM(assigned_users) AS total_users_assigned,
        SUM(dormant_users) AS total_users_dormant,
        ROUND(100.0 * SUM(dormant_users) / NULLIF(SUM(assigned_users), 0), 1) AS dormant_rate_pct
      FROM qualifying
      WHERE name <> 'Owner'
      GROUP BY name
      ORDER BY total_users_assigned DESC, role_name
      LIMIT 50
    • The warehouse answers
      STATUS: COMPILES (dry run on the empty schema, no data read)
      COLUMNS: role_name, tenant_count, pct_is_system, avg_permission_count, max_permission_count, total_users_assigned, total_users_dormant, dormant_rate_pct
    • OmarQuery compiles and matches the requested grain and columns. Submitting now.
    • Omar calls submit
      WITH role_instance AS (
        SELECT r.id AS role_id, r.tenant_id, r.name, r.is_system,
               COUNT(DISTINCT rp.permission_id) AS perm_count
        FROM roles r
        JOIN role_permissions rp ON rp.role_id = r.id
        GROUP BY r.id, r.tenant_id, r.name, r.is_system
      ),
      assigned AS (
        SELECT ur.role_id,
               COUNT(DISTINCT u.id) AS assigned_users,
               COUNT(DISTINCT u.id) FILTER (WHERE u.status = 'active' AND (u.last_login_at IS NULL OR u.last_login_at < DATE '2025-10-05')) AS dormant_users
        FROM user_roles ur
        JOIN users u ON u.id = ur.user_id AND u.tenant_id = ur.tenant_id
        WHERE u.deleted_at IS NULL
        GROUP BY ur.role_id
      ),
      qualifying AS (
        SELECT ri.tenant_id, ri.name, ri.is_system, ri.perm_count,
               a.assigned_users, a.dormant_users
        FROM role_instance ri
        JOIN assigned a ON a.role_id = ri.role_id
        WHERE ri.perm_count >= 1 AND a.assigned_users >= 1
      )
      SELECT
        name AS role_name,
        COUNT(DISTINCT tenant_id) AS tenant_count,
        ROUND(100.0 * SUM(CASE WHEN is_system THEN 1 ELSE 0 END) / COUNT(*), 1) AS pct_is_system,
        ROUND(AVG(perm_count), 1) AS avg_permission_count,
        MAX(perm_count) AS max_permission_count,
        SUM(assigned_users) AS total_users_assigned,
        SUM(dormant_users) AS total_users_dormant,
        ROUND(100.0 * SUM(dormant_users) / NULLIF(SUM(assigned_users), 0), 1) AS dormant_rate_pct
      FROM qualifying
      WHERE name <> 'Owner'
      GROUP BY name
      ORDER BY total_users_assigned DESC, role_name
      LIMIT 50
    • The warehouse answers
      Submitted to tonight's batch. Your work on this request is done.
    • OmarSubmitted. Summary of what the query does: - **Role instances**: `roles` joined to `role_permissions`, grouped per `(role_id, tenant_id, name, is_system)` to get a permission count per role instance. - **Assignment & dormancy**: `user_roles` joined to `users` (excluding soft-deleted users), aggregated per `role_id` into assigned-user counts and dormant-user counts (active status, and last login null or before 2025-10-05). - **Qualifying set**: role instances with ≥1 permission and ≥1 assigned non-deleted user — matching the prior report's population — with `'Owner'` excluded per the analyst's framing. - **Final grouping**: by `role_name` across all tenants, computing tenant count, % flagged `is_system`, average/max permission counts, total assigned, total dormant, and dormant rate, ordered by total assigned descending (tie-break on role name), capped at 50 rows.