sqlcmd.net validated sql reference

Intermediate commands

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

29 commands in this rank Step through by experience level
intermediate date-time MySQL MariaDB SQL Server PostgreSQL SQLite

Add Or Subtract Days From A Date

Calculate a future or past date by adding or subtracting an interval from an existing date.

intermediate filtering MySQL MariaDB SQL Server PostgreSQL SQLite

Avoid Duplicate Parent Rows With EXISTS

Use `EXISTS` when you only need to test whether a related row exists, not return one output row per match.

intermediate set-operations MySQL MariaDB SQL Server PostgreSQL SQLite

Choose UNION Or UNION ALL Intentionally

`UNION` removes duplicates, while `UNION ALL` keeps them. Picking the wrong one silently changes row counts.

intermediate set-operations MySQL MariaDB SQL Server PostgreSQL SQLite

Combine Query Results With UNION

Merge result sets from two `SELECT` statements into one, removing duplicate rows by default.

intermediate set-operations MySQL MariaDB SQL Server PostgreSQL SQLite

Combine Results With UNION ALL

Append result sets together without removing duplicates.

intermediate defining MySQL MariaDB SQL Server PostgreSQL SQLite

Create A Reusable Query With CREATE VIEW

Save a `SELECT` statement as a named view so it can be queried like a table.

intermediate string-processing MySQL MariaDB SQL Server PostgreSQL SQLite

Extract Part Of A String With SUBSTRING

Return only part of a text value by position and length.

intermediate date-time MySQL MariaDB SQL Server PostgreSQL SQLite

Extract The Year From A Date

Return the year component from a date or timestamp value.

intermediate joining MySQL MariaDB SQL Server PostgreSQL SQLite

Fetch Rows Including Unmatched With LEFT JOIN

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

intermediate grouping MySQL MariaDB SQL Server PostgreSQL SQLite

Filter Grouped Results With HAVING

Use `HAVING` to filter aggregate results after `GROUP BY`, the same way `WHERE` filters individual rows before grouping.

intermediate joining MySQL MariaDB SQL Server PostgreSQL SQLite

Filter LEFT JOIN Matches In The ON Clause

Put right-table filters in the `ON` clause when you want to keep unmatched left-side rows.

intermediate data-quality MySQL MariaDB SQL Server PostgreSQL SQLite

Find Duplicate Values With GROUP BY And HAVING

Group rows by the candidate key and keep only values that appear more than once.

intermediate set-operations MySQL MariaDB SQL Server PostgreSQL SQLite

Find Rows Common To Two Queries With INTERSECT

Return only the rows that appear in both result sets using `INTERSECT`.

intermediate set-operations MySQL MariaDB SQL Server PostgreSQL SQLite

Find Rows In One Query But Not Another With EXCEPT

Return rows from the first query that do not appear in the second query using `EXCEPT`.

intermediate joins MySQL MariaDB SQL Server PostgreSQL SQLite

Find Rows With No Matching Related Row

Use a `LEFT JOIN` and filter for `NULL` on the joined table to find missing relationships.

intermediate conversion MySQL MariaDB SQL Server PostgreSQL SQLite

Format A Number As A Decimal String

Convert a numeric value into a formatted string with a fixed number of decimal places, optionally including thousands separators.

intermediate grouping MySQL MariaDB SQL Server PostgreSQL SQLite

Group Rows And Count

Aggregate rows by category with `GROUP BY` and count how many rows fall into each group.

intermediate date-time MySQL MariaDB SQL Server PostgreSQL SQLite

Group Rows By Month

Bucket dates into month starts, then aggregate counts or totals per month.

intermediate aggregation MySQL MariaDB SQL Server PostgreSQL

Group Rows By Multiple Columns

Use more than one column in `GROUP BY` when each combination defines a separate aggregate bucket.

intermediate transactions MySQL MariaDB SQL Server PostgreSQL SQLite

Group Statements Into A Transaction

Use `BEGIN`, `COMMIT`, and `ROLLBACK` to execute multiple statements as an atomic unit.

intermediate joining MySQL MariaDB SQL Server PostgreSQL SQLite

Join Tables With INNER JOIN

Combine rows from two tables when matching keys exist in both tables.

intermediate composition MySQL MariaDB SQL Server PostgreSQL SQLite

Name A Subquery With A CTE

Use `WITH` to define a named temporary result set that can be referenced in the main query, improving readability over inline subqueries.

intermediate subqueries MySQL MariaDB SQL Server PostgreSQL SQLite

Nest A Query Inside Another With Subqueries

Embed a `SELECT` inside another query to filter, compute, or supply values that depend on aggregated or derived data.

intermediate pagination MySQL MariaDB SQL Server PostgreSQL SQLite

Paginate With Offset

Skip earlier rows and return the next slice of a sorted result set.

intermediate conversion MySQL MariaDB SQL Server PostgreSQL SQLite

Parse A Formatted String Into A Date

Convert a text string in a known format into a native DATE value using engine-specific parsing functions.

intermediate conditional-logic MySQL MariaDB SQL Server PostgreSQL SQLite

Return Different Values With CASE WHEN

Evaluate conditions row-by-row inside a `SELECT` to produce computed columns based on branching logic.

intermediate null-handling MySQL MariaDB SQL Server PostgreSQL SQLite

Return NULL When Two Values Are Equal With NULLIF

Use `NULLIF` to convert a specific value to `NULL`, most commonly to prevent division-by-zero errors.

intermediate null-handling MySQL MariaDB SQL Server PostgreSQL SQLite

Use NOT IN Safely When NULLs Are Possible

Filter `NULL` values out of the right side before using `NOT IN`, or the predicate can exclude everything.

intermediate grouping MySQL MariaDB SQL Server PostgreSQL SQLite

Use WHERE Before GROUP BY And HAVING After

`WHERE` filters individual rows before grouping. `HAVING` filters grouped results after aggregates are computed.