> For the complete documentation index, see [llms.txt](https://docs.concurrence.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.concurrence.com/developer-guide/classic-api/data-access/organization-tables/common-query-patterns.md).

# Common Query Patterns

SQL examples for common analytics queries across organization tables.

{% hint style="info" %}
**Query Examples.** These examples cover common patterns for analyzing your organization's data.
{% endhint %}

{% tabs %}
{% tab title="Basic Queries" %}

```sql
-- Inspect any table structure
SELECT *
FROM conversations
LIMIT 1;

-- Count records by type
SELECT
  COUNT(*) as total,
  COUNT(DISTINCT user_id) as unique_users,
  COUNT(DISTINCT service_id) as unique_services
FROM conversations
WHERE org_id = 'your-org-id';
```

{% endtab %}

{% tab title="Join Queries" %}

```sql
-- Get all messages for a user's conversations
SELECT
  m.*,
  c.service_id,
  c.created_at as conversation_started
FROM messages m
JOIN conversations c ON m.conversation_id = c._id
WHERE c.user_id = 'user-123'
ORDER BY m.created_at DESC;

-- User activity with service details
SELECT
  u.email,
  s.name as service_name,
  COUNT(c._id) as conversation_count,
  MAX(c.created_at) as last_activity
FROM users u
LEFT JOIN conversations c ON u.user_id = c.user_id
LEFT JOIN services s ON c.service_id = s._id
GROUP BY u.email, s.name;
```

{% endtab %}

{% tab title="Analytics Queries" %}

```sql
-- Daily conversation volume
SELECT
  DATE(created_at) as day,
  COUNT(*) as conversations,
  COUNT(DISTINCT user_id) as unique_users
FROM conversations
WHERE created_at >= CURRENT_DATE - INTERVAL '30 days'
GROUP BY DATE(created_at)
ORDER BY day DESC;

-- Service performance metrics
SELECT
  s.name,
  COUNT(c._id) as total_conversations,
  AVG(CASE WHEN c.is_finished THEN 1 ELSE 0 END) as completion_rate,
  COUNT(DISTINCT c.user_id) as unique_users
FROM services s
LEFT JOIN conversations c ON s._id = c.service_id
WHERE s.is_active = true
GROUP BY s.name;
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.concurrence.com/developer-guide/classic-api/data-access/organization-tables/common-query-patterns.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
