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.
Overview
Section titled “Overview”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.
Opening the SQL Query Editor
Section titled “Opening the SQL Query Editor”- Click SQL Query in the sidebar navigation.
- The editor opens with a blank query area and a database selector.
Running a query
Section titled “Running a query”Step-by-step:
- Select the database connection from the dropdown at the top of the editor.
- Type your SQL query in the editor area. For example:
SELECT * FROM production_dataWHERE timestamp > NOW() - INTERVAL '1 hour'ORDER BY timestamp DESCLIMIT 100;
- Click Run or press Ctrl+Enter (⌘+Enter on Mac) to execute.
- Results appear in the table below the editor.
Editor features
Section titled “Editor features”| Feature | Description |
|---|---|
| Syntax highlighting | SQL keywords, strings, numbers, and comments are colour-coded |
| Auto-complete | Table names, column names, and SQL keywords are suggested as you type |
| Multi-statement | Run multiple queries separated by semicolons |
| Query history | Previously executed queries are saved and accessible |
| Error display | SQL errors are shown with line numbers and descriptions |
Working with results
Section titled “Working with results”Viewing results
Section titled “Viewing results”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
Exporting results
Section titled “Exporting results”Ask Kai to export query results:
Export the last query results as CSVSave the query output as JSON
Selecting a database
Section titled “Selecting a database”If you have multiple databases connected, use the database selector dropdown:
- Click the dropdown at the top of the editor.
- Select the database you want to query.
- The editor’s auto-complete updates to reflect the selected database’s schema.
Query examples
Section titled “Query examples”Browse tables
Section titled “Browse tables”-- PostgreSQLSELECT table_name FROM information_schema.tablesWHERE table_schema = 'public';
-- MySQLSHOW TABLES;Time-series query
Section titled “Time-series query”SELECT timestamp, temperature, pressureFROM sensor_readingsWHERE machine_id = 'M001' AND timestamp > NOW() - INTERVAL '24 hours'ORDER BY timestamp;Aggregation
Section titled “Aggregation”SELECT DATE_TRUNC('hour', timestamp) AS hour, AVG(temperature) AS avg_temp, MAX(temperature) AS max_temp, MIN(temperature) AS min_tempFROM sensor_readingsWHERE timestamp > NOW() - INTERVAL '7 days'GROUP BY hourORDER BY hour;Join across tables
Section titled “Join across tables”SELECT m.machine_name, COUNT(a.alarm_id) AS alarm_count, MAX(a.timestamp) AS last_alarmFROM machines mLEFT JOIN alarms a ON m.machine_id = a.machine_idWHERE a.timestamp > NOW() - INTERVAL '24 hours'GROUP BY m.machine_nameORDER BY alarm_count DESC;- Use LIMIT — Always add
LIMITto 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.
Next steps
Section titled “Next steps”- Data Sources → — Connect more databases to query.
- Chat & AI Assistant → — Let Kai generate SQL from natural language.