Learn SQL Server from the first query
Write your first SELECT, then work through database changes, performance, and recovery.
Learning path
Foundations
Write your first query and explain which rows it returns.
Choose the columns a report needs
Return a clear result with only the requested columns.
Check passedRead values with their data types
Use explicit casts to make the type of a value clear.
Check passedSee how NULL changes a filter
Predict how a comparison with NULL affects rows in a WHERE clause.
Check passedLabel values with CASE
Turn a score into a readable label with a searched CASE expression.
Check passedCombine filters with AND and OR
Use parentheses to make a multi-part row filter clear.
Check passedChoose whether duplicate values stay
Use DISTINCT when a result needs each projected value once.
Check passedSort rows with a tie-breaker
Add a unique tie-breaker so equal sort values have a predictable order.
Check passedRead one ordered page
Use OFFSET and FETCH to return a page after sorting the full result.
Check passedName a row's grain and key
Describe the row grain and distinguish sample uniqueness from an enforced key.
Check passedMatch rows with an inner join
Join two row sources on their shared key and predict the joined grain.
Check passedKeep unmatched rows with LEFT JOIN
Preserve every left-side row when no right-side match exists.
Check passedSummarize rows by group
Use GROUP BY and aggregate functions to report one summary row per team.
Check passedFilter groups with HAVING
Apply a condition to aggregated groups with HAVING.
Check passedCombine compatible result sets
Use UNION to combine compatible row shapes and remove duplicate result rows.
Check passedRead an invalid-column error
Use an error number and message to choose the next check for a failed query.
Check passed
Practitioner
Build database features and check how changes affect data.
Design related tables with enforceable rules
Choose keys and constraints that make invalid application data fail at the database boundary.
Check passedKeep the intended row grain across joins
Recognize when a one to many join multiplies rows and aggregate the child data before joining it to a parent result.
Check passedTrack inserts, updates, and deletes with OUTPUT
Use inserted and deleted row images to record exactly what a data change changed.
Check passedRoll back the whole unit of work on error
Put related changes in one transaction and use TRY/CATCH to undo partial work when a required row is missing.
Check passedCall a reusable module with a bound parameter
Define a session local stored procedure and pass a typed input value by name.
Check passedBind values in dynamic SQL
Keep SQL text separate from user supplied values when a query must be executed dynamically.
Check passedRead a view as a named query
Explain how a view exposes a reusable query definition and how an outer query adds its own filter.
Check passedTurn a JSON array into typed rows
Use OPENJSON with an explicit schema to read application JSON as relational columns.
Check passedAsk a temporal table for an earlier row version
Use FOR SYSTEM_TIME AS OF to read the version of a system-versioned row that was valid at a chosen time.
Check passedRank and compare rows without collapsing them
Use window functions to add per-team rank, running totals, and previous-row comparisons while preserving each input row.
Check passedWalk a dependency tree with a recursive CTE
Separate a recursive CTE's anchor from its recursive member and carry depth and path through each level.
Check passedPage with a stable keyset cursor
Continue an ordered result from the last seen key without relying on an offset that shifts when earlier rows change.
Check passedDetect a competing update with rowversion
Use a rowversion token in an update predicate so an application can detect that another writer changed the row.
Check passedFilter a date range with a half-open boundary
Write a date range that includes the start, excludes the next period, and leaves the filtered column unwrapped in a function.
Check passedReview a permission request at object scope
Connect a least-privilege request to the exact permission, securable, and database principal it affects.
Check passed
Expert practice
Investigate performance, recover data, and review complex SQL.
Expand a bill of materials without looping
Trace a recursive CTE through a component graph, multiply quantities along each path, and stop revisiting a node on the same path.
Check passedCompare window frames and rank tied results
Choose explicit window frames, distinguish peer-aware running totals from row-by-row totals, and return the top ranks without losing ties.
Check passedReconcile sets and label subtotal rows
Compare distinct sets with `EXCEPT` and `INTERSECT`, then use `GROUPING SETS` and `GROUPING_ID` to distinguish detail rows from subtotals.
Check passedTest an index with measured plan evidence
Compare actual plans and logical reads before and after a covering index on a temporary training table.
Check passedExplain estimates with skewed data
Read estimated versus actual rows and connect a cardinality difference to data distribution and statistics.
Check passedFind a plan regression in Query Store
Compare captured plans and runtime measurements over time before recommending a plan correction.
Check passedTrace a live blocking chain
Identify blocked requests, their blocker session, current wait evidence, and the SQL text available for each session.
Check passedRead a deadlock graph and explain the victim
Reproduce a controlled two-session deadlock in a disposable database and use its graph to identify the cycle and victim.
Check passedReproduce a lost update and protect the write
Observe a read-then-write race at `READ COMMITTED` and compare it with an atomic update and an optimistic concurrency check.
Check passedClaim work safely from a shared queue
Use one atomic update with locking hints and OUTPUT to claim one ordered queue row per worker.
Check passedRecover a failed transaction safely
Use `SET XACT_ABORT`, `TRY...CATCH`, `XACT_STATE`, and rollback to leave a failed unit of work in a known state.
Check passedBuild and prove a point-in-time restore plan
Inspect a backup chain, verify file and log sequence evidence, and rehearse a restore to a separate database name.
Check passedAudit effective permissions without changing access
Inspect the caller's login, database principal, and effective database permissions before requesting or reviewing a role change.
Check passedValidate and shred a JSON feed
Check a JSON contract, project typed fields with `OPENJSON`, and preserve parent rows when an array is empty or the payload is invalid.
Check passedCapstone: explain a ticket SLA report
Build an auditable event-to-SLA report with staged CTEs, JSON extraction, `LAG`, window elapsed time, grouped ticket outcomes, and within-team ranking.
Check passedCapstone: reconcile a feed with a deduplicated MERGE
Build a deterministic source with a ranking CTE, update changed target rows, insert missing keys, and audit each action through `OUTPUT $action`.
Check passed
Quick labs for new joiners
Learn the work from small queries. Follow the rows, check the result, and use the Microsoft reference when a change reaches a real database.
The examples use synthetic values, read-only checks, or local temporary tables. Review each script before running it.
Check the SQL Server version
Read the engine version before choosing syntax.
Read a small result
Choose output columns and give each column a clear name.
Filter rows with NULL
Write a filter that treats missing values as a separate case.
Order a short result
Use a stable order before limiting rows.
Join related rows
Match rows by a key and keep unmatched rows when the report needs them.
Group rows into totals
Return one summary row for each group key.
Name a query step
Use a common table expression to name a result used by the next query.
Rank rows inside a group
Add a row number while keeping each source row in the output.
Change a temporary table
Create, alter, inspect, and remove a local lab table.
Roll back a lab change
Compare a temporary row before and after a rolled-back update.
Read a backup inventory
Build a recovery timeline from synthetic backup records.
Check one permission
Read the current session's permission without changing access.
Command reference
Browse 2,018 references for SQL Server and sqlcmd. Each entry connects syntax to a practice check. See scope and sources.
- Queries: SELECT and clauses 40
- Queries: FROM, joins, APPLY, PIVOT, temporal, graph 26
- Predicates and search conditions 6
- Hints: query, table, join 57
- DML 14
- Rowset functions (relational operators) 16
- Variables and assignment 5
- Control-of-flow 12
- Cursors 6
- Transactions 9
- Batches, execution, messages, admin 22
- Operators: logical and set 13
- Operators: scalar 46
- Expressions 6
- SET options 41
- DDL: CREATE 83
- DDL: ALTER 73
- DDL: DROP 65
- DDL: other 6
- DCL: permissions and security context 69
- Backup and restore 16
- Collations 4
- Service Broker 8
- DBCC 34
- Functions: AI 3
- Functions: Aggregate 18
- Functions: Analytic 9
- Functions: Bit manipulation 6
- Functions: Collation 2
- Functions: Configuration 14
- Functions: Conversion 6
- Functions: Cryptographic 26
- Functions: Cursor 4
- Functions: Data type 7
- Functions: Date & time 31
- Functions: Fuzzy string match 4
- Functions: Graph 7
- Functions: JSON 11
- Functions: Logical 4
- Functions: Mathematical 24
- Functions: Metadata 41
- Functions: ODBC scalar 1
- Functions: Ranking 5
- Functions: Regular expressions 7
- Functions: Replication 1
- Functions: Security 25
- Functions: String 33
- Functions: System 34
- Functions: System Statistical 11
- Functions: Text & Image 2
- Functions: Trigger 5
- Functions: Vectors 6
- Functions: overview 1
- Data types 33
- XML 16
- Spatial: geography 88
- Spatial: geometry 95
- hierarchyid methods 11
- Database Engine stored procedures 102
- Security stored procedures 58
- Management Data Warehouse stored procedures 5
- SQL Server Agent stored procedures 77
- Database Maintenance Plan stored procedures 7
- Fabric mirrored databases 6
- Database Engine 13
- Cursor stored procedures 13
- Catalog stored procedures 12
- Snapshot Backup stored procedures 2
- Log Shipping stored procedures 23
- Security 1
- Replication stored procedures 227
- Full-Text Search and Semantic Search stored procedures 18
- Distributed Queries stored procedures 14
- OLE Automation stored procedures 7
- PolyBase stored procedures 2
- Query Store 1
- Query Store stored procedures 8
- MSDTC stored procedures 2
- Database Mail stored procedures 25
- Data collector stored procedures 20
- Policy-Based Management stored procedures 19
- SQL Server Profiler stored procedures 5
- Log shipping 2
- Active Geo-Replication 1
- XML stored procedures 2
- Change event streaming 6
- Change Data Capture stored procedures 16
- Internal only mirroring stored procedures 13
- PolyBase 2
- FILESTREAM and FileTable stored procedures 1
- Change Tracking 3
- Replication 1
- Spatial indexes 2
- Spatial Index stored procedures 4
- FILESTREAM and FileTable 1
- Temporal tables 1
- General extended stored procedures 10
- General extended 1
- sqlcmd client commands 17