Skip to content

Instantly share code, notes, and snippets.

@decagondev
Created May 12, 2026 17:31
Show Gist options
  • Select an option

  • Save decagondev/038dfd6babea9c8ed880de0732b6b792 to your computer and use it in GitHub Desktop.

Select an option

Save decagondev/038dfd6babea9c8ed880de0732b6b792 to your computer and use it in GitHub Desktop.

You are building an internal analytics dashboard for a SaaS company. Analysts need to generate reports from the users and orders tables without writing raw SQL (to reduce errors and SQL injection risks).

Table Schemas:

users (
    id              SERIAL PRIMARY KEY,
    created_at      TIMESTAMPTZ NOT NULL,
    email           VARCHAR(255),
    status          VARCHAR(50),      -- 'active', 'inactive', 'suspended'
    country         VARCHAR(2),       -- ISO country codes e.g. 'US', 'IN', 'GB'
    plan            VARCHAR(50),      -- 'free', 'pro', 'enterprise'
    revenue         NUMERIC(12,2),    -- lifetime revenue
    last_login      TIMESTAMPTZ
);

orders (
    id              SERIAL PRIMARY KEY,
    user_id         INTEGER REFERENCES users(id),
    order_date      TIMESTAMPTZ NOT NULL,
    amount          NUMERIC(10,2),
    status          VARCHAR(50),      -- 'paid', 'refunded', 'failed'
    product         VARCHAR(100)
);

Task:
Create a fluent, type-safe, secure Python query builder class called AnalyticsQueryBuilder that generates parameterized PostgreSQL queries.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment