Skip to content

Instantly share code, notes, and snippets.

@jxu
Last active May 2, 2024 23:20
Show Gist options
  • Save jxu/848f7452c60ae5aaf82bcb32aad9530d to your computer and use it in GitHub Desktop.
Save jxu/848f7452c60ae5aaf82bcb32aad9530d to your computer and use it in GitHub Desktop.
Specifically for T-SQL, probably for other SQLs
-- Syntax order
-- https://learn.microsoft.com/en-us/sql/t-sql/queries/select-transact-sql
WITH cte AS (cte_query)
SELECT cols
FROM table
WHERE cond
GROUP BY col
HAVING cond
WINDOW window_expr
ORDER BY col
-- Logical execution order
-- https://learn.microsoft.com/en-us/sql/t-sql/queries/select-transact-sql#logical-processing-order-of-the-select-statement
FROM
ON
JOIN
WHERE
GROUP BY
HAVING
SELECT
DISTINCT
ORDER BY
TOP
-- Check column unique
-- Not considering duplicate NULLs
-- https://stackoverflow.com/questions/26199765/sql-query-to-determine-that-values-in-a-column-are-unique
SELECT COUNT(col), COUNT(distinct col) FROM table
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment