SQL Minifier

Compresses Structured Query Language (SQL) code.

Input: TextResult: Text

Text input

Examples

Compact a three-row INSERT statement

Input

INSERT INTO planets (name, moons)
VALUES ('Earth', 1),
       ('Mars', 2),
       ('Venus', 0);

Expected answer

INSERT INTO planets(name, moons) VALUES ('Earth', 1), ('Mars', 2), ('Venus', 0);
Reduce whitespace around an EXISTS subquery

Input

SELECT name
FROM customers
WHERE EXISTS (SELECT 1 FROM orders WHERE orders.customer_id = customers.id);

Expected answer

SELECT name FROM customers WHERE EXISTS(SELECT 1 FROM orders WHERE orders.customer_id = customers.id);
Keep a spaced city name inside a string literal

Input

SELECT station_id, city
FROM stations
WHERE city = 'New York'
ORDER BY station_id;

Expected answer

SELECT station_id, city FROM stations WHERE city = 'New York' ORDER BY station_id;
Compact an update with an explicit safety condition

Input

UPDATE inventory
SET stock = stock - 1
WHERE sku = 'BOOK-ALICE' AND stock > 0;

Expected answer

UPDATE inventory SET stock = stock - 1 WHERE sku = 'BOOK-ALICE' AND stock > 0;
Keep UNION ALL when combining two event streams

Input

SELECT occurred_at, 'signup' AS kind FROM signups
UNION ALL
SELECT occurred_at, 'purchase' AS kind FROM purchases;

Expected answer

SELECT occurred_at, 'signup' AS kind FROM signups UNION ALL SELECT occurred_at, 'purchase' AS kind FROM purchases;

Related tools

Browse code tidy tools