Syntax: Advanced SQL

Updated · 4 min read

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.

DatabaseSyntax
PostgreSQLWITH x AS (SELECT 1 AS n) SELECT n FROM x;
MySQLWITH x AS (SELECT 1 AS n) SELECT n FROM x; -- 8.0+
SQL ServerWITH x AS (SELECT 1 AS n) SELECT n FROM x;
OracleWITH x AS (SELECT 1 AS n FROM dual) SELECT n FROM x;
SQLiteWITH x AS (SELECT 1 AS n) SELECT n FROM x;

Recursive CTE

Standard. Lesson: Recursive CTEs.

DatabaseSyntax
PostgreSQLWITH RECURSIVE n(x) AS (SELECT 1 UNION ALL SELECT x + 1 FROM n WHERE x < 3) SELECT x FROM n;
MySQLWITH RECURSIVE n(x) AS (SELECT 1 UNION ALL SELECT x + 1 FROM n WHERE x < 3) SELECT x FROM n;
SQL ServerWITH n(x) AS (SELECT 1 UNION ALL SELECT x + 1 FROM n WHERE x < 3) SELECT x FROM n;
OracleWITH n(x) AS (SELECT 1 FROM dual UNION ALL SELECT x + 1 FROM n WHERE x < 3) SELECT x FROM n;
SQLiteWITH 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.

DatabaseSyntax
PostgreSQLRANK() OVER (ORDER BY fee DESC)
MySQLRANK() OVER (ORDER BY fee DESC) -- 8.0+
SQL ServerRANK() OVER (ORDER BY fee DESC)
OracleRANK() OVER (ORDER BY fee DESC)
SQLiteRANK() OVER (ORDER BY fee DESC) -- 3.25+

Running Total

Standard. Lesson: Running Totals, LAG and LEAD.

DatabaseSyntax
PostgreSQLsum(x) OVER (ORDER BY d)
MySQLSUM(x) OVER (ORDER BY d)
SQL ServerSUM(x) OVER (ORDER BY d)
OracleSUM(x) OVER (ORDER BY d)
SQLitesum(x) OVER (ORDER BY d)

Conditional Count

Standard. Lesson: Pivoting Data.

DatabaseSyntax
PostgreSQLcount(*) FILTER (WHERE method = 'card')
MySQLSUM(method = 'card')
SQL ServerSUM(CASE WHEN method = 'card' THEN 1 ELSE 0 END)
OracleCOUNT(CASE WHEN method = 'card' THEN 1 END)
SQLitecount(*) FILTER (WHERE method = 'card') -- 3.30+

Subtotals

Standard. Lesson: ROLLUP, CUBE and GROUPING SETS.

DatabaseSyntax
PostgreSQLGROUP BY ROLLUP (a, b)
MySQLGROUP BY a, b WITH ROLLUP
SQL ServerGROUP BY ROLLUP (a, b)
OracleGROUP BY ROLLUP (a, b)
SQLiteNot supported: use UNION ALL of the groupings

Read a JSON Field as Text

Dialect. Lesson: JSON in SQL.

DatabaseSyntax
PostgreSQLdata ->> 'name'
MySQLdata ->> '$.name'
SQL ServerJSON_VALUE(data, '$.name')
OracleJSON_VALUE(data, '$.name')
SQLitedata ->> '$.name' -- 3.38+

Dialect. Lesson: Full-Text Search.

DatabaseSyntax
PostgreSQLto_tsvector('english', body) @@ to_tsquery('english', 'join')
MySQLMATCH (body) AGAINST ('join') -- needs a FULLTEXT index
SQL ServerCONTAINS(body, 'join') -- needs a full-text index
OracleCONTAINS(body, 'join') > 0 -- needs an Oracle Text index
SQLitebody MATCH 'join' -- on an FTS5 virtual table

A Function Language

Dialect. Lesson: User-Defined Functions.

DatabaseSyntax
PostgreSQLCREATE FUNCTION f() RETURNS int LANGUAGE plpgsql AS $$ BEGIN RETURN 1; END $$;
MySQLCREATE FUNCTION f() RETURNS INT DETERMINISTIC RETURN 1;
SQL ServerCREATE FUNCTION dbo.f() RETURNS INT AS BEGIN RETURN 1; END;
OracleCREATE FUNCTION f RETURN NUMBER IS BEGIN RETURN 1; END;
SQLiteNot supported in SQL: register functions from the host language

Try the PostgreSQL Forms

These run on the Fee Tracker in Try it:

SQL▶ 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;
Output
       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?