Syntax: Advanced SQL
This page lists common advanced sql tasks in five databases, so you can copy the form your database understands. Each task shows whether the SQL standard defines it (standard) or each database has its own way (dialect). For what each feature does and when to use it, follow the lesson link under each task. The SQL Syntax by Database page explains how to find out which database and version you're using.
How these were checked: every PostgreSQL and SQLite form on this page was run (PostgreSQL 18 and SQLite 3.54). MySQL, SQL Server and Oracle forms follow each database's current documentation (MySQL 8.4, SQL Server 2022 and later, Oracle AI Database 26ai); version notes such as "8.0.19+" mark features that older releases lack.
Common Table Expression
Standard. Lesson: Common Table Expressions.
| Database | Syntax |
|---|---|
| PostgreSQL | WITH x AS (SELECT 1 AS n) SELECT n FROM x; |
| MySQL | WITH x AS (SELECT 1 AS n) SELECT n FROM x; -- 8.0+ |
| SQL Server | WITH x AS (SELECT 1 AS n) SELECT n FROM x; |
| Oracle | WITH x AS (SELECT 1 AS n FROM dual) SELECT n FROM x; |
| SQLite | WITH x AS (SELECT 1 AS n) SELECT n FROM x; |
Recursive CTE
Standard. Lesson: Recursive CTEs.
| Database | Syntax |
|---|---|
| PostgreSQL | WITH RECURSIVE n(x) AS (SELECT 1 UNION ALL SELECT x + 1 FROM n WHERE x < 3) SELECT x FROM n; |
| MySQL | WITH RECURSIVE n(x) AS (SELECT 1 UNION ALL SELECT x + 1 FROM n WHERE x < 3) SELECT x FROM n; |
| SQL Server | WITH n(x) AS (SELECT 1 UNION ALL SELECT x + 1 FROM n WHERE x < 3) SELECT x FROM n; |
| Oracle | WITH n(x) AS (SELECT 1 FROM dual UNION ALL SELECT x + 1 FROM n WHERE x < 3) SELECT x FROM n; |
| SQLite | WITH RECURSIVE n(x) AS (SELECT 1 UNION ALL SELECT x + 1 FROM n WHERE x < 3) SELECT x FROM n; |
Rank Rows
Standard. Lesson: Ranking Functions.
| Database | Syntax |
|---|---|
| PostgreSQL | RANK() OVER (ORDER BY fee DESC) |
| MySQL | RANK() OVER (ORDER BY fee DESC) -- 8.0+ |
| SQL Server | RANK() OVER (ORDER BY fee DESC) |
| Oracle | RANK() OVER (ORDER BY fee DESC) |
| SQLite | RANK() OVER (ORDER BY fee DESC) -- 3.25+ |
Running Total
Standard. Lesson: Running Totals, LAG and LEAD.
| Database | Syntax |
|---|---|
| PostgreSQL | sum(x) OVER (ORDER BY d) |
| MySQL | SUM(x) OVER (ORDER BY d) |
| SQL Server | SUM(x) OVER (ORDER BY d) |
| Oracle | SUM(x) OVER (ORDER BY d) |
| SQLite | sum(x) OVER (ORDER BY d) |
Conditional Count
Standard. Lesson: Pivoting Data.
| Database | Syntax |
|---|---|
| PostgreSQL | count(*) FILTER (WHERE method = 'card') |
| MySQL | SUM(method = 'card') |
| SQL Server | SUM(CASE WHEN method = 'card' THEN 1 ELSE 0 END) |
| Oracle | COUNT(CASE WHEN method = 'card' THEN 1 END) |
| SQLite | count(*) FILTER (WHERE method = 'card') -- 3.30+ |
Subtotals
Standard. Lesson: ROLLUP, CUBE and GROUPING SETS.
| Database | Syntax |
|---|---|
| PostgreSQL | GROUP BY ROLLUP (a, b) |
| MySQL | GROUP BY a, b WITH ROLLUP |
| SQL Server | GROUP BY ROLLUP (a, b) |
| Oracle | GROUP BY ROLLUP (a, b) |
| SQLite | Not supported: use UNION ALL of the groupings |
Read a JSON Field as Text
Dialect. Lesson: JSON in SQL.
| Database | Syntax |
|---|---|
| PostgreSQL | data ->> 'name' |
| MySQL | data ->> '$.name' |
| SQL Server | JSON_VALUE(data, '$.name') |
| Oracle | JSON_VALUE(data, '$.name') |
| SQLite | data ->> '$.name' -- 3.38+ |
Full-Text Search
Dialect. Lesson: Full-Text Search.
| Database | Syntax |
|---|---|
| PostgreSQL | to_tsvector('english', body) @@ to_tsquery('english', 'join') |
| MySQL | MATCH (body) AGAINST ('join') -- needs a FULLTEXT index |
| SQL Server | CONTAINS(body, 'join') -- needs a full-text index |
| Oracle | CONTAINS(body, 'join') > 0 -- needs an Oracle Text index |
| SQLite | body MATCH 'join' -- on an FTS5 virtual table |
A Function Language
Dialect. Lesson: User-Defined Functions.
| Database | Syntax |
|---|---|
| PostgreSQL | CREATE FUNCTION f() RETURNS int LANGUAGE plpgsql AS $$ BEGIN RETURN 1; END $$; |
| MySQL | CREATE FUNCTION f() RETURNS INT DETERMINISTIC RETURN 1; |
| SQL Server | CREATE FUNCTION dbo.f() RETURNS INT AS BEGIN RETURN 1; END; |
| Oracle | CREATE FUNCTION f RETURN NUMBER IS BEGIN RETURN 1; END; |
| SQLite | Not supported in SQL: register functions from the host language |
Try the PostgreSQL Forms
These run on the Fee Tracker in Try it:
SELECT title, fee_usd,
RANK() OVER (ORDER BY fee_usd DESC) AS fee_rank
FROM courses
ORDER BY fee_rank, title;
SELECT count(*) FILTER (WHERE method = 'card') AS card_payments,
count(*) FILTER (WHERE method = 'check') AS check_payments
FROM payments; title | fee_usd | fee_rank
-------------------+---------+----------
Java Collections | 550.00 | 1
Java OOP | 500.00 | 2
Java Basics | 450.00 | 3
JavaScript Basics | 450.00 | 3
SQL Basics | 400.00 | 5
Git Basics | 250.00 | 6
(6 rows)
card_payments | check_payments
---------------+----------------
4 | 2
(1 row)Was this tutorial helpful?