Skip to content

SQL Query Editor

The SQL Query Editor lets you run direct SQL queries against any connected database. Designed for power users who want full control over their queries.


While Kai can generate and run SQL queries from natural language, the SQL Query Editor gives you a direct interface for writing and executing SQL. Use it for:

  • Complex queries that are easier to write in SQL than describe in words
  • Debugging and verifying data
  • Exporting query results

Note: For safety, DDL statements (CREATE, ALTER, DROP) are not permitted. The SQL Query Editor is read-only — it supports SELECT queries and non-destructive operations only.


  1. Click SQL Query in the sidebar navigation.
  2. The editor opens with a blank query area and a database selector.

Step-by-step:

  1. Select the database connection from the dropdown at the top of the editor.
  2. Type your SQL query in the editor area. For example:
    SELECT * FROM production_data
    WHERE timestamp > NOW() - INTERVAL '1 hour'
    ORDER BY timestamp DESC
    LIMIT 100;
  3. Click Run or press Ctrl+Enter (⌘+Enter on Mac) to execute.
  4. Results appear in the table below the editor.

FeatureDescription
Syntax highlightingSQL keywords, strings, numbers, and comments are colour-coded
Auto-completeTable names, column names, and SQL keywords are suggested as you type
Multi-statementRun multiple queries separated by semicolons
Query historyPreviously executed queries are saved and accessible
Error displaySQL errors are shown with line numbers and descriptions

Query results are displayed in a scrollable data table with:

  • Column headers — Click to sort ascending/descending
  • Row count — Total rows returned
  • Execution time — How long the query took
  • Pagination — Navigate through large result sets

Ask Kai to export query results:

  • Export the last query results as CSV
  • Save the query output as JSON

If you have multiple databases connected, use the database selector dropdown:

  1. Click the dropdown at the top of the editor.
  2. Select the database you want to query.
  3. The editor’s auto-complete updates to reflect the selected database’s schema.

-- PostgreSQL
SELECT table_name FROM information_schema.tables
WHERE table_schema = 'public';
-- MySQL
SHOW TABLES;
SELECT timestamp, temperature, pressure
FROM sensor_readings
WHERE machine_id = 'M001'
AND timestamp > NOW() - INTERVAL '24 hours'
ORDER BY timestamp;
SELECT
DATE_TRUNC('hour', timestamp) AS hour,
AVG(temperature) AS avg_temp,
MAX(temperature) AS max_temp,
MIN(temperature) AS min_temp
FROM sensor_readings
WHERE timestamp > NOW() - INTERVAL '7 days'
GROUP BY hour
ORDER BY hour;
SELECT
m.machine_name,
COUNT(a.alarm_id) AS alarm_count,
MAX(a.timestamp) AS last_alarm
FROM machines m
LEFT JOIN alarms a ON m.machine_id = a.machine_id
WHERE a.timestamp > NOW() - INTERVAL '24 hours'
GROUP BY m.machine_name
ORDER BY alarm_count DESC;

  • Use LIMIT — Always add LIMIT to exploratory queries to avoid fetching millions of rows.
  • Check the selected database — Make sure the correct database is selected before running a query.
  • Use Kai for complex analytics — For OEE, anomaly detection, and forecasting, Kai can handle the SQL and analysis for you.