runtime

compare syntax one concept · three SQL dialects

Compare Syntax

See how the same SQL concept is written in SQLite, PostgreSQL and SQL Server.

Limit output

Return a small, predictable set of rows: here, the five most recent invoices.

what differs

SQLite and PostgreSQL put LIMIT after ORDER BY. SQL Server puts TOP after SELECT. Ordering by a unique tie-breaker (order_id) makes the chosen rows predictable.

at a glance

dialecthow it is written
SQLite… ORDER BY … LIMIT 5
PostgreSQLLIMIT 5 or FETCH FIRST 5 ROWS ONLY
SQL ServerSELECT TOP (5) … ORDER BY …

in Runtime

The SQLite version runs for real in the editor under the examples. PostgreSQL and SQL Server are shown for reference; they do not run here.

Example dataset: Retail

SQLiteruns here
SELECT order_id, invoice_no, ordered_at
FROM orders
ORDER BY ordered_at DESC, order_id DESC
LIMIT 5;
PostgreSQLreference
SELECT order_id, invoice_no, ordered_at
FROM orders
ORDER BY ordered_at DESC, order_id DESC
LIMIT 5;
SQL Serverreference
SELECT TOP (5) order_id, invoice_no, ordered_at
FROM orders
ORDER BY ordered_at DESC, order_id DESC;

run the sqlite version

sqlite.sql
SELECT order_id, invoice_no, ordered_at
FROM orders
ORDER BY ordered_at DESC, order_id DESC
LIMIT 5;
Compare SQL Syntax · Runtime