sqlcmd.net validated sql reference

Advanced commands

Guides grouped by the expected difficulty and amount of SQL context they assume.

28 commands in this rank Step through by experience level
advanced windowing MySQL MariaDB SQL Server PostgreSQL SQLite

Access Previous And Next Rows With LAG And LEAD

Reference values from preceding or following rows within an ordered partition without writing a self-join.

advanced aggregation MySQL MariaDB SQL Server PostgreSQL SQLite

Aggregate Rows Conditionally With CASE Inside An Aggregate

Use `CASE WHEN` inside `SUM` or `COUNT` to produce multiple metrics from a single pass over grouped data.

advanced json MySQL MariaDB SQL Server PostgreSQL SQLite

Aggregate Rows Into A JSON Array

Collect values from multiple rows into a single JSON array, ordered and grouped by other columns.

advanced json MySQL MariaDB SQL Server PostgreSQL SQLite

Build A JSON Object From Column Values

Construct a JSON object literal from individual column values within a query, returning structured data without a separate serialization step.

advanced windowing MySQL MariaDB SQL Server PostgreSQL SQLite

Calculate A Running Total With SUM OVER

Accumulate a column's values row by row using `SUM` as a window function.

advanced windowing MySQL MariaDB SQL Server PostgreSQL

Calculate Percent Of Total With A Window Function

Use `SUM(...) OVER ()` to compare each row's value to the total without collapsing the result set.

advanced composition MySQL MariaDB SQL Server PostgreSQL SQLite

Chain Multiple CTEs in a Single WITH Clause

Define several named subqueries in one `WITH` block, each building on the previous, before the final `SELECT`.

advanced joining SQL Server PostgreSQL SQLite

Combine All Rows From Both Tables With FULL OUTER JOIN

Return all rows from both tables, filling NULLs on either side when there is no matching row.

advanced aggregation MySQL MariaDB SQL Server PostgreSQL SQLite

Concatenate Values Within A Group

Aggregate multiple string values from grouped rows into a single delimited string.

advanced windowing MySQL MariaDB SQL Server PostgreSQL SQLite

Control the Window Frame With ROWS BETWEEN

Use `ROWS BETWEEN` in a window function to aggregate only the rows immediately surrounding each row.

advanced deleting MySQL MariaDB SQL Server PostgreSQL SQLite

Delete Rows Based On A Condition In Another Table

Remove rows from one table when a matching row exists in another table using a subquery.

advanced string-processing MySQL MariaDB PostgreSQL

Extract A Substring Matching A Regular Expression

Return the portion of a string that matches a regular expression pattern, useful for parsing structured text without fixed delimiters.

advanced joining MySQL MariaDB SQL Server PostgreSQL SQLite

Fetch Rows Including Unmatched From Right Table

Return all rows from the right table and matching rows from the left, filling NULL where no match exists.

advanced joining MySQL MariaDB SQL Server PostgreSQL SQLite

Generate All Row Combinations With CROSS JOIN

Produce every possible pairing of rows from two tables using `CROSS JOIN`.

advanced windowing MySQL MariaDB SQL Server PostgreSQL SQLite

Group Consecutive Rows With A ROW_NUMBER Difference

Create stable run groups without a primary key by subtracting two `ROW_NUMBER()` calculations over the same ordered events.

advanced upserting MySQL MariaDB SQL Server PostgreSQL SQLite

Insert Or Update A Row With UPSERT

Atomically insert a row if it does not exist or update it if it does, using the database's native conflict-resolution syntax.

advanced joining MySQL MariaDB SQL Server PostgreSQL SQLite

Join A Table To Itself With A Self-Join

Reference the same table twice using aliases to compare or relate rows within the same dataset.

advanced windowing MySQL MariaDB SQL Server PostgreSQL SQLite

Number Rows Within Groups Using ROW_NUMBER

Assign sequential integers to rows within a partition without collapsing the result set the way `GROUP BY` does.

advanced windowing MySQL MariaDB SQL Server PostgreSQL SQLite

Rank Rows With Gaps Using RANK And DENSE_RANK

Assign rank numbers to rows within a partition, controlling whether tied ranks leave gaps in the sequence.

advanced json MySQL MariaDB SQL Server PostgreSQL SQLite

Read a Value From a JSON Column

Extract a scalar value from a JSON column using the JSON function or operator each engine supports.

advanced joining MySQL SQL Server PostgreSQL

Reference Outer Columns Inside a Subquery With LATERAL

Use `LATERAL` (or `CROSS APPLY` on SQL Server) to let a subquery reference columns from the preceding table in the `FROM` clause.

advanced date-time MySQL MariaDB SQL Server PostgreSQL SQLite

Round a Timestamp Down to a Specific Unit

Truncate a timestamp to the start of a month, day, or hour using engine-specific functions.

advanced windowing MySQL MariaDB SQL Server PostgreSQL SQLite

Select The Top Row Per Group

Use `ROW_NUMBER()` to rank rows within each group and keep only the highest-ranked row.

advanced defining MySQL MariaDB SQL Server PostgreSQL SQLite

Speed Up Queries With CREATE INDEX

Create an index on one or more columns to make lookups and joins faster.

advanced string-processing SQL Server PostgreSQL

Split A Delimited String Into Rows

Expand a comma-separated or delimited string into one row per element, turning a single cell into a proper set of rows.

advanced filtering MySQL MariaDB SQL Server PostgreSQL SQLite

Test For Row Existence With EXISTS

Use a correlated subquery inside `EXISTS` to include rows only when related rows are found in another table.

advanced recursion MySQL MariaDB SQL Server PostgreSQL SQLite

Traverse Hierarchies With A Recursive CTE

Use `WITH RECURSIVE` to repeatedly join a query to its own results, enabling traversal of tree or graph structures.

advanced updating MySQL MariaDB SQL Server PostgreSQL SQLite

Update Rows Using Values From Another Table

Use a join inside an `UPDATE` statement to copy or derive values from a related table.