New
Pages
2102
Published
2023
T-SQL Fundamentals
A complete foundation in Transact-SQL for SQL Server and Azure SQL development
Master T-SQL from first SELECT to advanced window functions, so you can write correct, efficient queries against any SQL Server or Azure SQL database.
T-SQL Fundamentals by Itzik Ben-Gan gives you a precise, logically grounded understanding of Transact-SQL β the query language at the heart of SQL Server and Azure SQL. Rather than teaching syntax by rote, the book builds your mental model of how SQL processes data, covering set theory, predicate logic, and the logical query-processing order that determines why queries behave the way they do. From joins and subqueries to window functions, transactions, and error handling, every chapter connects theory to practical, production-quality code.
About this book
Most T-SQL tutorials show you what to type. This book shows you why it works β and why it sometimes doesn't. Itzik Ben-Gan, a SQL Server MVP and one of the most respected names in the T-SQL community, builds the conceptual foundation that separates developers who can write queries from developers who understand them.
The book opens with the relational model and set theory, not because they are academic curiosities, but because every JOIN, every NULL, and every unexpected result trace back to those foundations. You will learn the logical query-processing order β the sequence in which SQL Server actually evaluates clauses β and use that knowledge to predict output, eliminate bugs, and reason about performance before touching an execution plan.
From there the book moves through every major query construct in precise, incremental steps. Filtering with WHERE and HAVING. Grouping and aggregation. All varieties of JOIN. Subqueries and table expressions including derived tables, CTEs, views, and inline table-valued functions. Set operators. Window functions, where Ben-Gan is widely considered the leading authority. Pivoting and unpivoting data. Modifying data with INSERT, UPDATE, DELETE, and MERGE. Temporal tables. Transactions and error handling.
- Logical query processing explained from first principles, not just memorized rules
- Set-based thinking applied to real filtering, grouping, and aggregation problems
- All JOIN types β inner, outer, cross, self β with precise behavioral definitions
- Subqueries and every form of table expression, including when to choose each
- Window functions covered in depth: partitioning, ordering, framing, and ranking
- Data modification including the full MERGE syntax and its edge cases
- Temporal tables for tracking row history without custom audit triggers
- Transactions, isolation levels, and structured error handling with TRY/CATCH
The code samples are written for SQL Server 2022 and Azure SQL Database but the core T-SQL concepts apply across recent versions. Every example is self-contained, runnable, and designed to illustrate a specific behavior β not just demonstrate that something compiles.
Whether you are writing your first SELECT or refactoring a stored procedure that has puzzled your team for months, T-SQL Fundamentals gives you the precise vocabulary and the reasoning tools to do it correctly.
π― What you'll learn
- Explain why SQL Server evaluates query clauses in a specific logical order and use that order to predict and debug results
- Write correct inner, outer, cross, and self joins by understanding set-based semantics, not just syntax
- Build readable, efficient table expressions using derived tables, CTEs, views, and inline TVFs
- Apply window functions with precise control over partitioning, ordering, and frame boundaries
- Construct MERGE statements that handle insert, update, and delete in a single atomic operation
- Use temporal tables to query point-in-time and period-range data without custom audit infrastructure
- Manage transactions and handle errors with TRY/CATCH and appropriate isolation levels
- Reason about NULL behavior consistently across filters, joins, aggregations, and comparisons
π€ Who is this book for?
- Developers new to SQL Server who want a rigorous, correct foundation rather than trial-and-error syntax knowledge
- Application developers who write T-SQL alongside their main language and need to stop guessing why queries return unexpected rows
- Database administrators who query data regularly but never formally studied the relational model or logical query processing
- Analysts and data engineers moving from Excel or basic SQL dialects to production SQL Server or Azure SQL environments
- Experienced T-SQL writers who want to fill in conceptual gaps β particularly around NULLs, window functions, and isolation levels
Table of contents
-
01
Background to T-SQL Querying and Programming
Establishes the theoretical underpinnings of T-SQL: the relational model, set theory, predicate logic, and the SQL standard. You leave with a clear mental model of why SQL behaves differently from procedural code.
-
02
Single-Table Queries
Covers the core SELECT statement clause by clause, including logical query-processing order, filtering with WHERE, sorting with ORDER BY, and working with NULLs from the start.
-
03
Joins
Defines every JOIN type β cross, inner, and all outer variants β through set-based reasoning. You write queries that return exactly the rows you intend, including multi-table and self joins.
-
04
Subqueries
Introduces correlated and non-correlated subqueries in SELECT, WHERE, and FROM clauses. You learn when subqueries are the clearest solution and how to trace their behavior step by step.
-
05
Table Expressions
Covers derived tables, common table expressions, views, and inline table-valued functions. You choose the right form for each problem and write readable multi-step query logic.
-
06
Set Operators
Explains UNION, INTERSECT, and EXCEPT with precise set-based definitions, including the difference between UNION and UNION ALL and how these operators handle duplicate elimination.
-
07
Beyond the Fundamentals of Querying
Introduces window functions in depth: partitioning, ordering, and frame specification for ranking, offset, and aggregate windows. You rewrite common GROUP BY problems as cleaner, more capable window calculations.
-
08
Data Modification
Walks through INSERT, UPDATE, DELETE, and MERGE in full, including output clauses, non-standard UPDATE FROM syntax, and the complete MERGE specification with its documented edge cases.
-
09
Temporal Tables
Explains system-versioned temporal tables, how SQL Server stores row history, and how to query point-in-time and period-range data without writing custom audit logic.
-
10
Transactions and Error Handling
Covers transaction control, isolation levels and their trade-offs, and structured error handling with TRY/CATCH and THROW. You write procedures that fail cleanly and protect data consistency.
Frequently asked questions
Do I need prior SQL experience to read this book?
No prior SQL experience is assumed. The book starts from the relational model and builds up. Readers with some SQL background will move through the early chapters quickly and find the deeper explanations of NULL behavior and logical processing order useful regardless.
Which version of SQL Server does the book target?
The code is written for SQL Server 2022 and Azure SQL Database. The core T-SQL concepts apply to SQL Server 2016 and later with only minor syntax differences, and the book notes version-specific features where relevant.
Is this book for querying only, or does it cover stored procedures and T-SQL programming?
The primary focus is on query writing: SELECT, data modification, and the constructs around them. Transactions and error handling are covered thoroughly, giving you the foundation for procedural T-SQL work, though full stored procedure design is not the central topic.
At over 2100 pages, is this a reference book or meant to be read cover to cover?
It is designed to be read sequentially β each chapter builds on the previous one. The depth makes it long, but the structure is linear, not encyclopedic, so reading from start to finish is the intended experience.
Does the book include downloadable sample code?
The book references T-SQL scripts you can run against SQL Server or Azure SQL. Check the publisher page at Microsoft Press for any companion file downloads associated with this edition.
Is this book useful if I work with Azure SQL rather than on-premises SQL Server?
Yes. T-SQL behaves consistently across SQL Server 2022 and Azure SQL Database, and the book explicitly covers both environments. Most examples run without modification in either context.
You might also like
New
New
SQL Cookbook
Problem-Solution Recipes for SQL Across Multiple Database Platforms
by Anthony Molinaro, Robert de Graaf
New