TutorialsAIPostgreSQLMySQL

How to Run Ad Hoc Database Queries Without Writing SQL

Most database querying tools are built around one assumption: you know in advance what you want to ask. You pre-build a report, set up a dashboard, configure...

Marcus Chen· Solutions EngineerApril 9, 20269 min read

Most database querying tools are built around one assumption: you know in advance what you want to ask. You pre-build a report, set up a dashboard, configure a view and then query against it later. That works fine for recurring reports. It falls apart the moment someone asks a question you didn't prepare for.

That's what ad hoc queries are for. A CEO asks "What's our revenue from customers who signed up more than 90 days ago but haven't upgraded?" A support manager wants to see "all users who logged in this week but submitted a ticket in the last 30 days." These are one-off, unplanned questions and answering them has traditionally required either writing SQL yourself or waiting for a data engineer to do it.

This guide explains how ad hoc database queries work, why SQL is a bottleneck for most teams, and how modern AI tools let non-technical people run them instantly in plain English.

What Is an Ad Hoc Query?

An ad hoc query is a database query created on the spot to answer a specific, immediate question as opposed to a scheduled report or pre-built dashboard.

The Latin phrase "ad hoc" means "for this specific purpose." In database terms, it means you're writing a query that wasn't planned in advance. You have a question right now, and you need an answer right now.

Here's a typical SQL ad hoc query:

SELECT
  u.email,
  u.created_at,
  COUNT(o.id) AS order_count,
  SUM(o.total_amount) AS lifetime_value
FROM users u
LEFT JOIN orders o ON o.user_id = u.id
WHERE u.created_at < NOW() - INTERVAL '90 days'
  AND u.plan_id = 'free'
GROUP BY u.id, u.email, u.created_at
HAVING COUNT(o.id) > 0
ORDER BY lifetime_value DESC;

This query finds free-tier users who've been around for more than 90 days and have placed at least one order. It might take an experienced developer 5–10 minutes to write and verify. For someone without SQL knowledge, it's simply impossible without help.

Why SQL Is a Bottleneck for Ad Hoc Questions

The SQL query above is not particularly complex. But it requires knowing:

  • Which tables hold which data (users, orders)
  • How those tables relate (user_id foreign key)
  • Which aggregate functions to use (COUNT, SUM)
  • How to write date arithmetic (INTERVAL '90 days')
  • The difference between WHERE and HAVING
  • Most business users analysts, operations managers, founders, sales leads don't have this knowledge. So their options are:

  • Ask a developer or data analyst who has a queue of other work and will get to it in a day or two
  • Try to use a BI tool which only shows pre-built reports and doesn't let them ask new questions
  • Export data to a spreadsheet slow, error-prone, and produces stale data the moment you open the file
  • None of these are good. Ad hoc questions lose half their value when the answer takes 48 hours.

    The Traditional Workarounds (and Their Limitations)

    Before AI-powered tools, teams tried several approaches to reduce SQL dependency:

    Drag-and-drop query builders like Metabase's question builder let you filter, group, and sort without typing SQL. They work well for simple questions but struggle with multi-table joins or conditional logic. The moment your question needs more than one table, you're back to SQL.

    Pre-aggregated views and data models some teams create pre-joined views that analysts can query more easily. This helps, but someone still has to maintain those views, and they never quite cover every question that comes up.

    Natural language plugins for existing BI tools most major BI tools now have an AI chat feature. The problem is they're querying against pre-built semantic layers, not your raw database. If a column or relationship isn't in the model, the question can't be answered.

    Asking ChatGPT to write SQL this works surprisingly often, but requires you to know your schema, copy it into the prompt, get the SQL back, then find a way to run it against your actual database. It's four manual steps where there should be zero.

    How AI-Powered Natural Language Queries Actually Work

    The better approach connects the AI model directly to your database and handles the full loop: understand the question, write SQL, run it, return results.

    Here's what happens under the hood when you type a question into AI for Database:

  • Schema inspection the system reads your database schema (tables, columns, types, relationships) to understand what data is available
  • Query generation a language model translates your plain-English question into a SQL query appropriate for your database engine (PostgreSQL, MySQL, MongoDB, BigQuery, etc.)
  • Execution the query runs against your live database through a secure, read-only connection
  • Result formatting the results come back as a table or chart depending on what makes sense for the data
  • The whole loop takes a few seconds. No waiting, no SQL, no intermediaries.

    A question like "Show me all customers who signed up last month but never completed onboarding" gets translated to something like:

    SELECT
      u.id,
      u.email,
      u.created_at,
      u.onboarding_completed_at
    FROM users u
    WHERE u.created_at >= DATE_TRUNC('month', NOW() - INTERVAL '1 month')
      AND u.created_at < DATE_TRUNC('month', NOW())
      AND u.onboarding_completed_at IS NULL
    ORDER BY u.created_at DESC;

    The user never sees this query. They just see a list of users and can take action on it.

    Practical Examples of Ad Hoc Queries Non-Technical Teams Run

    Here are the kinds of questions business users actually ask when they have direct database access:

    Sales and RevOps:

  • "Which accounts haven't had any activity in the last 45 days?"
  • "Show me deals that were marked closed-won this quarter but the invoice was never generated"
  • "Which sales reps have more than 20 open opportunities over 60 days old?"
  • Customer Success:

  • "List all enterprise customers whose usage dropped more than 30% this month compared to last"
  • "Show me customers on the starter plan who hit the usage limit more than 3 times"
  • "Which customers submitted more than 5 support tickets in the past two weeks?"
  • Product:

  • "What percentage of users who completed onboarding used the dashboard feature in their first week?"
  • "Show me all users who used feature X but haven't returned in 30 days"
  • Finance:

  • "What's total revenue from customers in Germany this quarter?"
  • "Show me all invoices over $5,000 that are more than 30 days past due"
  • Each of these would require a custom SQL query. With a natural language interface, they take seconds to answer.

    Setting Up Ad Hoc Query Access for Your Team

    The practical challenge isn't just technical it's organizational. You need to give business users query access without giving them the ability to accidentally delete data or expose credentials.

    A few things to get right:

    Read-only database user create a dedicated database user with SELECT-only permissions. This is basic hygiene regardless of what tool you use.

     PostgreSQL example
    CREATE USER analyst_readonly WITH PASSWORD 'strong-password-here';
    GRANT CONNECT ON DATABASE yourdb TO analyst_readonly;
    GRANT USAGE ON SCHEMA public TO analyst_readonly;
    GRANT SELECT ON ALL TABLES IN SCHEMA public TO analyst_readonly;
    ALTER DEFAULT PRIVILEGES IN SCHEMA public
      GRANT SELECT ON TABLES TO analyst_readonly;

    Clear schema documentation AI tools work best when your table and column names are descriptive. user_created_at is better than uc. If possible, add comments to your database columns so the AI model has more context.

    Access controls decide who can query which data. If your database contains PII, you might want certain tables accessible only to specific team members.

    With AI for Database, you connect your database once and define which users can access it. From there, each person can ask questions in natural language the system handles query generation and execution, and never gives users access to raw credentials or schema write permissions.

    Turning Ad Hoc Answers Into Saved Dashboards

    One pattern that works well: run an ad hoc query, get the answer, then decide if it's worth saving as a recurring dashboard.

    If you find yourself asking "how many free users converted this week?" every Monday morning, that's a good candidate for a saved, auto-refreshing dashboard. If it's a one-time question about a specific event, you don't need to save it at all.

    AI for Database lets you pin any query result as a dashboard card that refreshes on a schedule you define daily, hourly, weekly, whatever you need. The original plain-English question becomes the query definition. You never have to touch SQL.

    Getting Started

    If your team is spending time waiting on SQL queries, the fix isn't more training it's better tooling. Natural language database access doesn't replace your data team, it frees them from the constant stream of "can you pull a quick report on..." requests.

    Ad hoc questions are part of how good businesses operate. They happen when something unexpected occurs, when a new campaign launches, when a customer asks a question that requires data to answer. The ability to get those answers in seconds without waiting, without SQL is a practical advantage.

    Try it for free at aifordatabase.com. Connect your database in a few minutes and start asking questions in plain English.

    Ready to try AI for Database?

    Query your database in plain English. No SQL required. Start free today.