Skip to main content
Pro plan required. The SQL Editor lets you write raw SQL queries when you need precise control over your database. It’s the power tool for situations where the visual browser isn’t enough.

When to Use the SQL Editor

Most of the time, you can manage your database through the AI chat or the Data Browser. The SQL Editor is for when you need something more specific:
Use CaseExample
Complex queriesJoins, subqueries, aggregations the AI can’t generate through chat
Bulk operationsINSERT, UPDATE, or DELETE across many rows at once
Custom analyticsAd-hoc reports and data exploration
DebuggingInvestigate data issues with targeted queries
Testing queriesTry out SQL before incorporating it into your app logic

How to Use

1

Open the Database tab

Click the Database tab in your workspace.
2

Navigate to the SQL Editor

Switch to the SQL Editor view within the Database tab.
3

Write your SQL query

Type your query in the editor. You get syntax highlighting and standard editing features to help you write correct SQL.
4

Execute and view results

Run your query and see the results displayed below the editor. SELECT queries return a results table. Other statements show affected row counts or error messages.

Example Queries

Here are some practical queries to get you started.

Count users by signup month

SELECT
  date_trunc('month', created_at) AS month,
  COUNT(*)
FROM users
GROUP BY month
ORDER BY month;

Find posts without comments

SELECT p.*
FROM posts p
LEFT JOIN comments c ON p.id = c.post_id
WHERE c.id IS NULL;

Get the most active users

SELECT
  u.name,
  COUNT(p.id) AS post_count
FROM users u
JOIN posts p ON u.id = p.author_id
GROUP BY u.id, u.name
ORDER BY post_count DESC
LIMIT 10;

Check for duplicate emails

SELECT email, COUNT(*)
FROM users
GROUP BY email
HAVING COUNT(*) > 1;

View RLS policies on a table

SELECT *
FROM pg_policies
WHERE tablename = 'posts';

Insert sample data

INSERT INTO posts (title, body, author_id)
VALUES
  ('First Post', 'Hello world!', '550e8400-e29b-41d4-a716-446655440000'),
  ('Second Post', 'Another entry.', '550e8400-e29b-41d4-a716-446655440000');
SQL operations execute directly against your live database. Be careful with UPDATE and DELETE queries — there’s no undo.

Tips for Safe SQL

Writing queries against a live database requires some caution. Here are practices that will save you from mistakes:
Use SELECT queries first to verify your WHERE clause before running UPDATE or DELETE.
For example, before running this:
-- DON'T run this first
DELETE FROM posts WHERE status = 'draft';
Run this to see what would be deleted:
-- Run this first to verify
SELECT * FROM posts WHERE status = 'draft';
Once you’ve confirmed the results look right, run the DELETE.

Other Safety Practices

  • Start with SELECT — Always preview what your query will affect
  • Use LIMIT — Add LIMIT 10 to exploratory queries so you don’t pull back thousands of rows
  • Be specific with WHERE — Broad WHERE clauses (or missing ones) can affect more rows than you expect
  • Use transactions — Wrap multi-step operations in BEGIN and COMMIT so you can ROLLBACK if something goes wrong

Using Transactions for Multi-Step Operations

When you need to make several related changes at once, wrap them in a transaction:
BEGIN;

-- Add a new column
ALTER TABLE posts ADD COLUMN slug text;

-- Populate it from existing data
UPDATE posts SET slug = lower(replace(title, ' ', '-'));

-- Add a unique constraint
ALTER TABLE posts ADD CONSTRAINT unique_slug UNIQUE (slug);

COMMIT;
If any step fails, you can run ROLLBACK instead of COMMIT and none of the changes will be applied. This keeps your database consistent.
The SQL Editor is a great companion to the AI chat. If the AI generates a query you want to tweak, copy it into the SQL Editor to modify and test it yourself.

SQL Editor vs. AI Chat

Both can execute SQL, but they serve different purposes:
SQL EditorAI Chat
You write the SQL yourselfAI generates SQL from your description
Full control over every detailFaster for common operations
Best for debugging and explorationBest for creating tables, migrations, CRUD
Results displayed in a tableResults described in conversation
Use the AI chat when you know what you want but don’t want to write the SQL. Use the SQL Editor when you need precise control over how the query works.

Database Overview

Learn about all the database management tools available in Nativeline.

Migrations

Track versioned schema changes to your database.