// 免费参考资料

SQL cheat sheets

One page per engine with the SQL you type every day: selects, joins, aggregation, DML, DDL, indexes, functions and NULL handling. Each snippet is written for its own engine rather than copied between them, and each sheet prints as a two-column reference card.

Same task, five dialects

Six things every sheet covers, side by side. Each cell is copied from that engine's own sheet, so it is the same engine-checked SQL you get on the full page.

任务 SQL Server PostgreSQL MySQL SQLite Oracle
First 10 rows, ordered
SELECT TOP (10) * FROM dbo.customers ORDER BY last_name;
SELECT * FROM customers ORDER BY last_name LIMIT 10;
SELECT * FROM customers ORDER BY last_name LIMIT 10;
SELECT * FROM customers ORDER BY last_name LIMIT 10;
SELECT * FROM customers ORDER BY last_name FETCH FIRST 10 ROWS ONLY;
Page 3, 10 rows per page
SELECT * FROM dbo.orders ORDER BY placed_at DESC OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY;
SELECT * FROM orders ORDER BY placed_at DESC LIMIT 10 OFFSET 20;
SELECT * FROM orders ORDER BY placed_at DESC LIMIT 10 OFFSET 20;
SELECT * FROM orders ORDER BY placed_at DESC LIMIT 10 OFFSET 20;
(12c+)
SELECT * FROM orders ORDER BY placed_at DESC OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY;
Upsert with MERGE
MERGE INTO dbo.targets t USING dbo.sources s ON t.id = s.id WHEN MATCHED THEN UPDATE SET t.name = s.name WHEN NOT MATCHED THEN INSERT (id, name) VALUES (s.id, s.name);
with ON CONFLICT
INSERT INTO stock (sku, count) VALUES ('A1', 5) ON CONFLICT (sku) DO UPDATE SET count = stock.count + EXCLUDED.count;
with ON DUPLICATE KEY UPDATE (row alias: 8.0.19+)
INSERT INTO stock (sku, count) VALUES ('A1', 5) AS new ON DUPLICATE KEY UPDATE count = stock.count + new.count;
with ON CONFLICT
INSERT INTO stock (sku, count) VALUES ('A1', 5) ON CONFLICT (sku) DO UPDATE SET count = count + excluded.count;
with MERGE
MERGE INTO targets t USING sources s ON (t.id = s.id) WHEN MATCHED THEN UPDATE SET t.name = s.name WHEN NOT MATCHED THEN INSERT (id, name) VALUES (s.id, s.name);
Substitute a default for NULL
SELECT ISNULL(nickname, first_name) FROM dbo.customers;
SELECT coalesce(nickname, first_name, 'unknown') FROM customers;
SELECT IFNULL(nickname, first_name) FROM customers;
SELECT ifnull(nickname, first_name) FROM customers;
SELECT NVL(nickname, first_name) FROM customers;
Current date and time
SELECT SYSDATETIME(), GETDATE();
SELECT now();
SELECT NOW(), CURDATE();
SELECT datetime('now'), date('now');
SELECT SYSDATE, SYSTIMESTAMP FROM dual;
Create a table with a generated key
CREATE TABLE dbo.orders (id BIGINT IDENTITY(1,1) PRIMARY KEY, customer_id BIGINT NOT NULL REFERENCES dbo.customers (id), total DECIMAL(10,2) NOT NULL DEFAULT 0, placed_at DATETIME2 NOT NULL DEFAULT SYSDATETIME());
CREATE TABLE orders (id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, customer_id BIGINT NOT NULL REFERENCES customers (id), total NUMERIC(10,2) NOT NULL DEFAULT 0, placed_at TIMESTAMPTZ NOT NULL DEFAULT now());
CREATE TABLE orders (id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, customer_id BIGINT UNSIGNED NOT NULL, total DECIMAL(10,2) NOT NULL DEFAULT 0, placed_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4;
CREATE TABLE orders (id INTEGER PRIMARY KEY, customer_id INTEGER NOT NULL REFERENCES customers (id), total REAL NOT NULL DEFAULT 0, placed_at TEXT NOT NULL DEFAULT (datetime('now')));
CREATE TABLE orders (id NUMBER GENERATED ALWAYS AS IDENTITY PRIMARY KEY, customer_id NUMBER NOT NULL REFERENCES customers (id), total NUMBER(10,2) DEFAULT 0 NOT NULL, placed_at DATE DEFAULT SYSDATE NOT NULL);

SQL cheat sheet FAQ

What is in each SQL cheat sheet?

Grouped, copy-pasteable snippets: SELECT basics, joins, filtering, aggregation, INSERT/UPDATE/DELETE, CREATE/ALTER/DROP, indexes, string functions, date functions, NULL handling and paging. Each sheet also lists the engine's quirks: the places where its dialect differs from the others and catches people moving between them.

Which database engines are covered?

SQL Server (T-SQL), PostgreSQL, MySQL (with notes where MariaDB differs), SQLite and Oracle. Each sheet is written against that engine's syntax: TOP versus LIMIT versus FETCH FIRST, backticks versus brackets versus double quotes, and so on.

Can I print a cheat sheet?

Yes. Each sheet has its own print stylesheet: the site navigation, FAQ and calls to action drop out, and the snippet tables print in two compact columns. Print or save as PDF from your browser.

Is this free?

Yes, with no sign-up. The SQLly desktop app connects to all five engines if you want to run the snippets against your own database.

// the sheet is the start

Run these against your own database

SQLly is a native SQL workbench for macOS, Windows and Linux. It connects to all five engines on these sheets, and its IntelliSense completes from your real schema.