SQL Server
SELECT DISTINCT LOWER(email) FROM users;
With the default case-insensitive collation, DISTINCT email returns the same count, and which casing you see for each group is arbitrary.
DISTINCT works on computed values the same way it works on columns.
Filed under Querying
The query itself is fully portable. The result is not: the same DISTINCT email returns fewer rows on SQL Server and MySQL than on PostgreSQL, SQLite and Oracle, because their default collations ignore case. Write the folding expression explicitly when the count matters. DISTINCT ON exists only in PostgreSQL.
Any expression can follow DISTINCT and the engine deduplicates the computed values. The syntax is identical in all five engines. What differs is how the comparison treats case: on SQL Server and MySQL's default collations, DISTINCT email already merges '[email protected]' and '[email protected]'.
SELECT DISTINCT LOWER(email) FROM users;
With the default case-insensitive collation, DISTINCT email returns the same count, and which casing you see for each group is arbitrary.
SELECT DISTINCT LOWER(email) FROM users;
Comparison is case-sensitive, so LOWER() changes the result. DISTINCT ON (LOWER(email)) * … ORDER BY LOWER(email), id keeps one full row per value.
SELECT DISTINCT LOWER(email) FROM users;
Default *_ci collations already fold case (utf8mb4_0900_ai_ci also folds accents), so DISTINCT email gives the same count.
SELECT DISTINCT lower(email) FROM users;
Case-sensitive by default. lower() folds ASCII letters only unless the ICU extension is loaded.
SELECT DISTINCT LOWER(email) FROM users;
Case-sensitive by default. SELECT UNIQUE is an old Oracle synonym for DISTINCT.
The whole result row. SELECT DISTINCT a, b returns distinct (a, b) pairs; there is no way to deduplicate on one column while returning others. For that, use GROUP BY with aggregates, ROW_NUMBER() … WHERE rn = 1, or PostgreSQL's DISTINCT ON.
Collation. SQL Server's and MySQL's default collations compare '[email protected]' and '[email protected]' as equal, so DISTINCT keeps one. PostgreSQL, SQLite and Oracle compare bytes by default and keep both. DISTINCT LOWER(email) gives the same count everywhere (for ASCII data).
A complete row per group without aggregating every other column. The ORDER BY decides which row wins: DISTINCT ON (customer_id) * … ORDER BY customer_id, created_at DESC keeps each customer's newest order. The leading ORDER BY expressions must match the DISTINCT ON list.
One window-function query that runs everywhere, plus PostgreSQL's DISTINCT ON shortcut for one row per group.
Whether = ignores case depends entirely on the column's collation.
Alphabetical order is wrong for statuses; map them to a rank instead.
SQLly is a native SQL workbench for macOS, Windows and Linux. Its formatter and plan viewer work in all five dialects.