> ## Documentation Index
> Fetch the complete documentation index at: https://docs.drdroid.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Writing SQL Queries

Query relational databases directly from your runbooks to fetch and analyze data.

## Supported Databases

* PostgreSQL
* MySQL
* SQL Server
* Oracle
* SQLite
* ClickHouse
* Any JDBC-compatible database

## Query Format

```sql theme={null}
-- Basic SELECT
SELECT column1, column2 
FROM table_name
WHERE condition
LIMIT 100;

-- JOIN example
SELECT 
    o.order_id,
    c.customer_name,
    o.order_date
FROM orders o
JOIN customers c ON o.customer_id = c.id
WHERE o.status = 'shipped';
```

## Examples

### Database Health Check

```sql theme={null}
-- Check for long-running queries
SELECT 
    pid,
    now() - query_start AS duration,
    query
FROM pg_stat_activity
WHERE state != 'idle'
  AND now() - query_start > interval '5 minutes';
```

### Performance Analysis

```sql theme={null}
-- Find slow queries
SELECT 
    query,
    mean_exec_time,
    calls
FROM pg_stat_statements
ORDER BY mean_exec_time DESC
LIMIT 10;
```

## Best Practices

* Always use parameterized queries to prevent SQL injection
* Limit result sets with WHERE clauses
* Use appropriate indexes
* Include error handling for database connections
* Close connections when done


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.