The SQL Booklet · Practical reference

SQL Server constructions for querying and combining data.

Fifty-one entries for recognizing syntax, understanding its behavior, and using it within complete problems.

A guided path

Syntax in context.

The path begins with choosing rows and columns. It then adds relationships, aggregation, data changes, control flow, and reusable objects. Each construction is explained before it is combined with others.

The final ten entries move from an isolated piece to a complete case. The goal is to recognize what problem each construction solves and what should be checked when adapting it.

How to use it

How to use the reference.

  1. Start from the needIdentify which rows, columns, or relationship the query needs to produce.
  2. Read the general formLocate the construction and the scope of each clause before copying the example.
  3. Check the resultReview duplicates, missing values, ordering, and affected rows according to the operation.
  4. Continue with a caseApplied examples show how several pieces solve a complete need.

Editorial method

Syntax, results, and precautions.

Examples use fictional objects and data. Operations that modify information first delimit the target set and use checks or reversible transactions when appropriate.

Version, permission, and semantic differences are explained where they can change the result. Each form is presented with the context on which it depends.

Read the editorial methodology

The path

Four reference groups and ten applied cases.

The order preserves the editorial progression: individual constructions first, followed by problems that combine them.

Section 1

Read, filter, and relate data

The constructions that shape a result, retain matches, summarize rows, and express decisions.

  1. 01SELECT and FROM: choose what information to read
  2. 02WHERE: decide which rows meet the condition
  3. 03DISTINCT, TOP, and ORDER BY: reduce and sort a result
  4. 04INNER JOIN: keep matching rows
  5. 05LEFT, RIGHT, and FULL JOIN: keep unmatched rows
  6. 06CROSS JOIN, CROSS APPLY, and OUTER APPLY: combine or correlate rows
  7. 07COUNT, SUM, AVG, MIN, and MAX: summarize a set
  8. 08GROUP BY and HAVING: summarize and filter groups
  9. 09CASE: express decisions inside a query
  10. 10COALESCE, ISNULL, and NULLIF: work with missing values
Section 2

Modify, combine, and classify

Write operations, related sets, intermediate results, windows, and pagination.

  1. 11INSERT: add rows explicitly
  2. 12UPDATE: change only the intended rows
  3. 13DELETE and TRUNCATE: remove data with different scopes
  4. 14UNION and UNION ALL: combine compatible results
  5. 15Subqueries, EXISTS, and NOT EXISTS: use related results
  6. 16WITH and CTEs: name an intermediate result
  7. 17OVER and PARTITION BY: calculate without losing detail
  8. 18ROW_NUMBER, RANK, and DENSE_RANK: number and rank rows
  9. 19OFFSET and FETCH: split an ordered result into pages
Section 3

Types, control, and structures

Conversions, text, dates, transactions, variables, control flow, and schema changes.

  1. 20CAST, CONVERT, TRY_CAST, and TRY_CONVERT: control data types
  2. 21Text functions: build, locate, and normalize strings
  3. 22Date functions: calculate periods and safe boundaries
  4. 23Temporary tables and table variables: keep intermediate results
  5. 24Transactions and TRY/CATCH: complete or roll back a unit of work
  6. 25Comparison and logical operators: build precise conditions
  7. 26DECLARE, SET, and SELECT assignment: work with variables
  8. 27IF, ELSE, and BEGIN...END: decide which statements run
  9. 28WHILE, BREAK, and CONTINUE: control repeated execution
  10. 29CREATE, ALTER, and DROP: manage database objects
  11. 30CREATE TABLE and constraints: define a reliable table
  12. 31ALTER TABLE: evolve an existing table deliberately
Section 4

Reusable objects and formats

Views, procedures, functions, specialized transformations, JSON, and compilation options.

  1. 32Views: name and reuse a query
  2. 33Stored procedures: encapsulate a parameterized operation
  3. 34User-defined functions: package reusable calculations
  4. 35PIVOT and UNPIVOT: reshape rows and columns
  5. 36INTERSECT and EXCEPT: compare compatible sets
  6. 37OUTPUT, inserted, and deleted: inspect affected rows
  7. 38STRING_AGG and STRING_SPLIT: join and split delimited text
  8. 39JSON: read, transform, and produce documents in SQL Server
  9. 40MERGE: understand its scope and important cautions
  10. 41OPTION and query hints: influence compilation
Section 5

Applied cases

Complete problems that combine several constructions and show the result obtained in SQL Server.

  1. 42Query customers with confirmed orders
  2. 43Find customers who do not have orders yet
  3. 44Show the first available phone number for each customer
  4. 45Calculate total sales by customer and filter the highest totals
  5. 46Insert an order and its line items safely
  6. 47Update category prices with prior validation
  7. 48Combine current and historical orders
  8. 49Return the latest order for each customer
  9. 50Calculate daily sales and a running total
  10. 51Build paginated search with optional filters