Database: SQL referencebeta
The read-only SQL that POST /data/query accepts: the two tables, the supported keywords and functions, the limits, and examples.
POST /v1/api/data/query runs one read-only SELECT over a workspace's Database. This page lists what the statement can contain.
The Database is in beta and Ody turns it on for one workspace at a time. Until it is on for yours, the endpoint returns 409 failed_precondition.
#How a query runs
Your statement is not sent to the database as written. Ody reads it, checks every table and column name against your workspace's columns, and builds its own query in which every value is a bound parameter. Three things follow from that:
- Only what is listed on this page works. Anything else fails with
400 invalid_argumentand a message that says what to change and where in the statement. - A query can only read. There is no statement that writes, and the query also runs in a read-only transaction.
- A query can only see your workspace. No other table exists for it.
The scope is contacts:read.
#Tables
Call GET /data/schema for the exact list in your workspace, with the type of every column and how to write its name.
#contacts
One row per contact. Every contact is here, including contacts with no values.
| Column | Type | |
|---|---|---|
id | id | The contact's id |
name | text | First and last name together |
first_name, last_name, company | text | |
phone | text | The contact's first phone number, E.164 |
email | text | The contact's first e-mail address |
tags | list | Test one with 'vip' = ANY(tags) |
created_at | timestamp | When the contact was created |
job_title, headline, website, linkedin_url, company_domain, company_linkedin_url, secondary_phone | text | Only where AI Contact is on |
address_street, address_city, address_state, address_postal_code, address_country | text | Only where AI Contact is on. Stored as given: a state can be TX or Texas |
phone_line_type, phone_carrier, caller_id_name, ai_notes | text | Only where AI Contact is on. phone_line_type is mobile, landline, voip, toll_free or unknown |
| one per Database column | by column | Named by the column's key |
How each Database column appears:
| Column type | SQL type | Notes |
|---|---|---|
text, select | text | |
number | number | |
date | date | |
boolean | boolean | Empty is NULL |
multi_select | list | Test one choice with 'Voice' = ANY(interests). A list cannot be grouped. |
| signal | boolean | Never NULL: false until the contact said it |
| signal, a second column | number | <key>_count: the number of conversations it came up in |
A column whose key is the name of one of the contact's own fields is written data_<key>: a Database column named "Company" is data_company. The card field names in the table above are set aside on every workspace, with or without AI Contact, so a Database column named "Job title" is always data_job_title and a saved query keeps its meaning. A key that is an SQL keyword or starts with a digit is written in double quotes: "order", "2nd_phone". GET /data/schema gives the right spelling in each column's sql field.
Ody adds columns from conversations, so contacts can have a column today that it did not have last week. A query that names its columns keeps working. SELECT * returns whatever columns exist, and is refused on a table with more than 50 columns.
#observations
One row per thing Ody heard a contact say: the history behind the cells Ody filled. Values that a person or the API wrote are not here.
| Column | Type | |
|---|---|---|
id | id | |
contact_id | id | Joins to contacts.id |
column_key | text | The key of the column the value is for |
column_name | text | The name of that column |
value | text | What was heard, as text. Several choices are joined with commas. |
observed_at | timestamp | When the conversation happened |
channel | text | call, ai_call, voicemail, text or greeting |
direction | text | inbound or outbound, when known |
confidence | number | How sure Ody was, from 0 to 1 |
applied | boolean | Whether this observation changed what the cell shows |
The contact's words are not in this table. The quote is shown in the app.
#What a statement can contain
| Clauses | SELECT, SELECT DISTINCT, FROM, JOIN … ON, WHERE, GROUP BY, HAVING, ORDER BY … ASC / DESC [NULLS FIRST / LAST], LIMIT, OFFSET, AS |
| Tables | contacts, observations, or the two joined: contacts JOIN observations ON contacts.id = observations.contact_id |
| Comparisons | =, <> (or !=), <, <=, >, >=, BETWEEN … AND …, IN (…), NOT IN (…), LIKE, ILIKE, IS NULL, IS NOT NULL, IS TRUE, IS NOT TRUE, IS FALSE |
| Logic | AND, OR, NOT, parentheses |
| Lists | 'value' = ANY(column) |
| Arithmetic | + and - on numbers |
| Totals | count(*), count(x), count(DISTINCT x), sum, avg, min, max |
| Functions | lower, upper, length, round, coalesce, date_trunc, now(), current_date |
| Dates | Compare a date or timestamp with a string: renewal_date < '2027-01-01'. Add or subtract a period: now() - INTERVAL '30 days', current_date + INTERVAL '2 weeks'. date_trunc takes hour, day, week, month, quarter or year. |
| Grouping | By a column, by an expression, by an output name, or by position: GROUP BY 1 |
Keywords and names are not case-sensitive. One trailing semicolon is allowed.
#Not supported
- Any statement other than
SELECT - Subqueries,
WITH,UNION,EXISTS CASE- Window functions and
FILTER LEFT,RIGHT,FULLandCROSSjoins, and joining a table to itself- Multiplication and division
- Any function not listed above
- Comments
For anything heavier, export the rows with GET /data/rows and query them in your own database.
#Behaviour to know
- Times are UTC.
date_trunc('day', observed_at)cuts at midnight UTC, andnow()andcurrent_dateare UTC. - Empty values sort last, in both directions. Write
NULLS FIRSTto put them first. - A list column cannot be grouped. To count contacts per choice of a
multi_selectcolumn, useGET /data/columns/:column/summary. - Output columns without a name are called
column_1,column_2, … by position. Give them names withAS. - Text comparison is exact.
plan = 'pro'does not matchPro. Uselower(plan) = 'pro'orILIKE.
#Limits
| Length of the statement | 4,000 characters |
| Rows returned by one call | 1,000. truncated: true in the response means more rows matched. |
Items in SELECT | 50 |
| Database columns used in one statement | 40 |
Values in one IN (…) | 200 |
| Running time | 5 seconds. A statement that takes longer is stopped and returns 400; add a filter or a smaller LIMIT. |
To read more than 1,000 rows, page with LIMIT and OFFSET and a stable ORDER BY:
SELECT id, name, plan FROM contacts ORDER BY id LIMIT 1000 OFFSET 1000Calls count toward the rate limit like any other request.
#Examples
Contacts per plan, largest first:
SELECT plan, count(*) AS contacts
FROM contacts
GROUP BY plan
ORDER BY contacts DESCPro contacts with a budget of at least 500 whose renewal is in the next 30 days:
SELECT name, phone, budget, renewal_date
FROM contacts
WHERE plan = 'Pro'
AND budget >= 500
AND renewal_date BETWEEN current_date AND current_date + INTERVAL '30 days'
ORDER BY renewal_dateContacts who asked about pricing more than once (asked_about_pricing is a signal column):
SELECT name, phone, asked_about_pricing_count
FROM contacts
WHERE asked_about_pricing AND asked_about_pricing_count > 1
ORDER BY asked_about_pricing_count DESCNew contacts per month:
SELECT date_trunc('month', created_at) AS month, count(*) AS contacts
FROM contacts
GROUP BY 1
ORDER BY 1Contacts tagged vip who are interested in Voice (interests is a multi_select column):
SELECT name, company
FROM contacts
WHERE 'vip' = ANY(tags) AND 'Voice' = ANY(interests)What Ody learned this month, by column and channel:
SELECT column_name, channel, count(*) AS heard
FROM observations
WHERE observed_at >= date_trunc('month', now())
GROUP BY column_name, channel
ORDER BY heard DESCEverything heard from one company's contacts in the last week:
SELECT contacts.name, observations.column_name, observations.value, observations.observed_at
FROM contacts
JOIN observations ON contacts.id = observations.contact_id
WHERE contacts.company ILIKE '%brightline%'
AND observations.observed_at >= now() - INTERVAL '7 days'
ORDER BY observations.observed_at DESC#Run a query
curl https://api.ody.co/v1/api/data/query \
-H "Authorization: Bearer $ODY_API_KEY" \
-H "Content-Type: application/json" \
-d '{ "sql": "SELECT plan, count(*) AS contacts FROM contacts GROUP BY plan ORDER BY contacts DESC" }'const { columns, rows, truncated } = await ody.data.query<{ plan: string; contacts: number }>(
"SELECT plan, count(*) AS contacts FROM contacts GROUP BY plan ORDER BY contacts DESC",
);result = client.data.query(
"SELECT plan, count(*) AS contacts FROM contacts GROUP BY plan ORDER BY contacts DESC"
)
for row in result["rows"]:
print(row["plan"], row["contacts"]){
"columns": [
{ "name": "plan", "type": "text" },
{ "name": "contacts", "type": "number" }
],
"rows": [
{ "plan": "Pro", "contacts": 701 },
{ "plan": "Free", "contacts": 455 },
{ "plan": null, "contacts": 628 }
],
"rowCount": 3,
"truncated": false
}The TypeScript and Python calls need v0.10.0 of the SDKs, which is not on npm or PyPI yet. With v0.9.0, use the cURL call.
#From an AI assistant
The MCP server has the same query as the tool query_data, and describe_data for the column names. An error comes back as a sentence the assistant can act on, for example:
There is no column "plam". Columns: id, name, first_name, last_name, company, phone, email, tags, created_at, plan, seats, renewal_dateSee MCP tools.