query_examples.md•9.08 kB
# EdgeLake MCP Query Examples
## Basic Queries
### Simple SELECT with LIMIT
```json
{
"name": "query",
"arguments": {
"database": "iot_data",
"table": "sensor_readings",
"limit": 10
}
}
```
Generated SQL:
```sql
SELECT * FROM sensor_readings LIMIT 10
```
---
### SELECT with WHERE clause
```json
{
"name": "query",
"arguments": {
"database": "iot_data",
"table": "sensor_readings",
"where": "temperature > 25 AND humidity < 60",
"limit": 20
}
}
```
Generated SQL:
```sql
SELECT * FROM sensor_readings WHERE temperature > 25 AND humidity < 60 LIMIT 20
```
---
### SELECT specific columns
```json
{
"name": "query",
"arguments": {
"database": "iot_data",
"table": "sensor_readings",
"select": ["device_id", "temperature", "timestamp"],
"where": "device_id = 'sensor_001'",
"limit": 50
}
}
```
Generated SQL:
```sql
SELECT device_id, temperature, timestamp FROM sensor_readings WHERE device_id = 'sensor_001' LIMIT 50
```
---
## Aggregation Queries
### COUNT with GROUP BY
```json
{
"name": "query",
"arguments": {
"database": "iot_data",
"table": "sensor_readings",
"select": ["device_id", "COUNT(*) as reading_count"],
"group_by": ["device_id"],
"order_by": [
{"column": "reading_count", "direction": "DESC"}
],
"limit": 10
}
}
```
Generated SQL:
```sql
SELECT device_id, COUNT(*) as reading_count FROM sensor_readings GROUP BY device_id ORDER BY reading_count DESC LIMIT 10
```
---
### AVG, MIN, MAX aggregations
```json
{
"name": "query",
"arguments": {
"database": "iot_data",
"table": "sensor_readings",
"select": [
"device_id",
"AVG(temperature) as avg_temp",
"MIN(temperature) as min_temp",
"MAX(temperature) as max_temp",
"COUNT(*) as count"
],
"where": "timestamp >= '2025-01-01'",
"group_by": ["device_id"],
"order_by": [
{"column": "avg_temp", "direction": "DESC"}
],
"limit": 20
}
}
```
Generated SQL:
```sql
SELECT device_id, AVG(temperature) as avg_temp, MIN(temperature) as min_temp, MAX(temperature) as max_temp, COUNT(*) as count FROM sensor_readings WHERE timestamp >= '2025-01-01' GROUP BY device_id ORDER BY avg_temp DESC LIMIT 20
```
---
## Advanced Queries
### Multiple ORDER BY columns
```json
{
"name": "query",
"arguments": {
"database": "sales",
"table": "orders",
"select": ["customer_id", "order_date", "total_amount"],
"where": "status = 'completed'",
"order_by": [
{"column": "order_date", "direction": "DESC"},
{"column": "total_amount", "direction": "DESC"}
],
"limit": 100
}
}
```
Generated SQL:
```sql
SELECT customer_id, order_date, total_amount FROM orders WHERE status = 'completed' ORDER BY order_date DESC, total_amount DESC LIMIT 100
```
---
### Complex WHERE with OR conditions
```json
{
"name": "query",
"arguments": {
"database": "ecommerce",
"table": "products",
"where": "(category = 'Electronics' OR category = 'Computers') AND price > 100 AND in_stock = true",
"order_by": [
{"column": "price", "direction": "ASC"}
],
"limit": 50
}
}
```
Generated SQL:
```sql
SELECT * FROM products WHERE (category = 'Electronics' OR category = 'Computers') AND price > 100 AND in_stock = true ORDER BY price ASC LIMIT 50
```
---
### JOIN with include_tables
```json
{
"name": "query",
"arguments": {
"database": "sales",
"table": "orders",
"select": ["orders.order_id", "customers.name", "orders.total_amount"],
"include_tables": ["customers"],
"where": "orders.customer_id = customers.customer_id AND orders.status = 'completed'",
"order_by": [
{"column": "orders.total_amount", "direction": "DESC"}
],
"limit": 25
}
}
```
Generated SQL:
```sql
SELECT orders.order_id, customers.name, orders.total_amount FROM orders, customers WHERE orders.customer_id = customers.customer_id AND orders.status = 'completed' ORDER BY orders.total_amount DESC LIMIT 25
```
---
### Cross-database JOIN
```json
{
"name": "query",
"arguments": {
"database": "sales",
"table": "orders",
"include_tables": ["inventory.products", "customers"],
"where": "orders.product_id = inventory.products.product_id AND orders.customer_id = customers.customer_id",
"limit": 50
}
}
```
Generated SQL:
```sql
SELECT * FROM orders, inventory.products, customers WHERE orders.product_id = inventory.products.product_id AND orders.customer_id = customers.customer_id LIMIT 50
```
---
## Extended Fields (EdgeLake Metadata)
### Add node metadata to results
```json
{
"name": "query",
"arguments": {
"database": "iot_data",
"table": "events",
"extend_fields": ["+ip", "+hostname"],
"select": ["timestamp", "event_type", "severity"],
"where": "severity = 'critical'",
"limit": 20
}
}
```
Generated SQL:
```sql
SELECT +ip, +hostname, timestamp, event_type, severity FROM events WHERE severity = 'critical' LIMIT 20
```
---
### Include table name in distributed query
```json
{
"name": "query",
"arguments": {
"database": "monitoring",
"table": "metrics",
"extend_fields": ["@table_name", "+overlay_ip"],
"select": ["timestamp", "metric_name", "value"],
"order_by": [
{"column": "timestamp", "direction": "DESC"}
],
"limit": 100
}
}
```
Generated SQL:
```sql
SELECT @table_name, +overlay_ip, timestamp, metric_name, value FROM metrics ORDER BY timestamp DESC LIMIT 100
```
---
## Time-series Queries
### Recent data with time filtering
```json
{
"name": "query",
"arguments": {
"database": "iot_data",
"table": "sensor_readings",
"select": ["timestamp", "device_id", "temperature", "humidity"],
"where": "timestamp > NOW() - INTERVAL '1 hour'",
"order_by": [
{"column": "timestamp", "direction": "DESC"}
],
"limit": 100
}
}
```
Generated SQL:
```sql
SELECT timestamp, device_id, temperature, humidity FROM sensor_readings WHERE timestamp > NOW() - INTERVAL '1 hour' ORDER BY timestamp DESC LIMIT 100
```
---
### Hourly aggregation
```json
{
"name": "query",
"arguments": {
"database": "iot_data",
"table": "sensor_readings",
"select": [
"DATE_TRUNC('hour', timestamp) as hour",
"device_id",
"AVG(temperature) as avg_temp",
"COUNT(*) as readings"
],
"where": "timestamp >= '2025-01-01'",
"group_by": ["DATE_TRUNC('hour', timestamp)", "device_id"],
"order_by": [
{"column": "hour", "direction": "DESC"}
],
"limit": 168
}
}
```
Generated SQL:
```sql
SELECT DATE_TRUNC('hour', timestamp) as hour, device_id, AVG(temperature) as avg_temp, COUNT(*) as readings FROM sensor_readings WHERE timestamp >= '2025-01-01' GROUP BY DATE_TRUNC('hour', timestamp), device_id ORDER BY hour DESC LIMIT 168
```
---
## Output Formats
### JSON format (default)
```json
{
"name": "query",
"arguments": {
"database": "iot_data",
"table": "sensor_readings",
"limit": 5,
"format": "json"
}
}
```
Returns:
```json
{
"Query": [
{
"device_id": "sensor_001",
"temperature": 25.5,
"humidity": 60.2,
"timestamp": "2025-01-15T10:30:00"
},
...
]
}
```
---
### Table format
```json
{
"name": "query",
"arguments": {
"database": "iot_data",
"table": "sensor_readings",
"limit": 5,
"format": "table"
}
}
```
Returns:
```
device_id | temperature | humidity | timestamp
-------------|-------------|----------|-------------------
sensor_001 | 25.5 | 60.2 | 2025-01-15T10:30:00
sensor_002 | 26.1 | 58.5 | 2025-01-15T10:30:00
...
```
---
## Natural Language to MCP Examples
### User asks: "Show me the latest 10 temperature readings"
```json
{
"name": "query",
"arguments": {
"database": "iot_data",
"table": "sensor_readings",
"select": ["timestamp", "device_id", "temperature"],
"order_by": [
{"column": "timestamp", "direction": "DESC"}
],
"limit": 10
}
}
```
---
### User asks: "What's the average temperature by device?"
```json
{
"name": "query",
"arguments": {
"database": "iot_data",
"table": "sensor_readings",
"select": ["device_id", "AVG(temperature) as avg_temp"],
"group_by": ["device_id"],
"order_by": [
{"column": "avg_temp", "direction": "DESC"}
]
}
}
```
---
### User asks: "Find all critical alerts from today"
```json
{
"name": "query",
"arguments": {
"database": "monitoring",
"table": "alerts",
"where": "severity = 'critical' AND timestamp >= CURRENT_DATE",
"order_by": [
{"column": "timestamp", "direction": "DESC"}
],
"limit": 100
}
}
```
---
### User asks: "Which products are low in stock?"
```json
{
"name": "query",
"arguments": {
"database": "inventory",
"table": "products",
"select": ["product_id", "product_name", "quantity", "reorder_level"],
"where": "quantity < reorder_level",
"order_by": [
{"column": "quantity", "direction": "ASC"}
],
"limit": 50
}
}
```