T-SQL Flashcards

(17 cards)

1
Q

In one sentence, what is T-SQL, and where do you typically run it?

A

T-SQL (Transact-SQL) is Microsoft SQL Server’s SQL dialect: ANSI SQL plus extensions for procedural logic, metadata, administration, and server-specific features. You use it in SSMS, Azure Data Studio, sqlcmd, Agent jobs, stored procedures, and embedded application SQL.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a batch in SQL Server, and what is GO?

A

A batch is a set of statements sent together and compiled as a unit. GO is a client batch separator (SSMS, ADS; sqlcmd recognizes it) — not T-SQL itself. Some statements must start a batch or cannot share a batch with others (e.g. CREATE VIEW edge cases — verify BOL).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Bucket these into DDL, DML, or DCL: CREATE TABLE, SELECT, GRANT, MERGE, TRUNCATE TABLE.

A

DDL — define/change objects: CREATE / ALTER / DROP TABLE, INDEX, PROC, VIEW, FUNCTION; TRUNCATE TABLE is often treated like DDL operationally. DMLrows: SELECT, INSERT, UPDATE, DELETE, MERGE. DCLpermissions: GRANT, DENY, REVOKE.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Name core query building blocks you’d list in a panel (joins, paging, windows, upsert).

A

SELECT with WHERE, GROUP BY / HAVING, ORDER BY, TOP or OFFSETFETCH. Joins: INNER, LEFT/RIGHT/FULL OUTER, CROSS JOIN, CROSS/OUTER APPLY for table-valued expressions. Window functions with OVER (PARTITION BY … ORDER BY …). MERGE for upsert (know concurrency / trigger caveats).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

When would you prefer EXISTS over IN for a subquery?

A

For existence checks, EXISTS is often preferred — it short-circuits on a match and avoids large IN lists; IN vs EXISTS tradeoffs come up in correlated vs non-correlated subquery discussions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a CTE, and what is its scope?

A

A CTE (WITH name AS (…)) is a named, in-query result referenced in the same outer statement (SELECT, INSERT, UPDATE, DELETE, MERGE). It is not a physical table — only that one statement can use it; a new statement needs its own WITH.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do chained CTEs work?

A

List multiple CTEs separated by commas: WITH a AS (…), b AS (SELECT … FROM a …) SELECT … FROM b. Later CTEs can reference earlier ones in the chain.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How does a recursive CTE work, and what must you guarantee?

A

Anchor query UNION ALL recursive part that references the CTE. You must ensure termination (e.g. a WHERE that eventually returns no rows). Uses: org charts, BOMs, paths in graphs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

CTE vs #temp / @table vs subquery — how do you contrast them in an interview?

A

CTElogical; optimizer may inline like subqueries — not a promise of “materialize once.” #temp / @table — real tempdb objects; #temp can have indexes; session or procedure scope. Subquery — inline; CTE wins readability when the same step is reused by name in one statement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Why is MERGE “tricky,” and what would you say about it on a panel?

A

MERGE is powerful for upsert but has gotchas with triggers, concurrency, and behavior details — know there is controversy in the community; test carefully and understand exact semantics for your version.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

EXEC('…') vs sp_executesql — which do you prefer for dynamic SQL, and why?

A

Prefer sp_executesql when you need parameterization and plan reuse; raw EXEC with string concat risks injection and plan cache churn. Interview line: “I use sp_executesql for dynamic SQL when I need parameters.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Name key programmability pieces beyond basic SQL (variables through modules).

A

DECLARE / SET / SELECT @var, IF/ELSE, WHILE, TRY/CATCH, THROW / RAISERROR, BEGIN TRAN / COMMIT / ROLLBACK, SAVE TRAN, XACT_ABORT (know it affects batch abort / atomicity). Modules: stored procedures; functions (scalar, inline TVF, multi-statement TVFinline often better for inlining); triggers (DML/DDL).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Catalog views vs DMVs — what are they for?

A

Catalog views (sys.tables, sys.indexes, sys.columns, …) describe schema and objects. DMVs (sys.dm_exec_*, sys.dm_os_*, sys.dm_tran_*) expose runtime state: sessions, plans, waits, locks — pair with performance tuning.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is DBCC, and what’s the operational caution?

A

DBCC includes integrity checks (CHECKDB, CHECKTABLE) and informational commands. Some commands are heavy — run in maintenance windows, not casually in production peaks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Why do session SET options matter — name a few that interviewers like?

A

They can change results and plans. Examples: ANSI_NULLS, QUOTED_IDENTIFIER (needed for certain object definitions like indexed views at create time), ARITHABORT (can affect plan behavior with indexed views). READ_COMMITTED_SNAPSHOT is database-level — ties to blocking. Line:Session options matter — I don’t memorize every flag, but I verify consistency when debugging weird plan or NULL behavior.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you handle version-specific T-SQL features in an interview?

A

Newer versions add syntax (STRING_AGG, JSON, graph, window enhancements, etc.). Be honest about your footprint (2017 vs 2019 vs 2022) and say you’d check BOL for the exact version you’re targeting.

17
Q

Give a closing “T-SQL in one breath” line for a panel.

A

T-SQL is SQL Server’s procedural SQL: DDL for objects, DML for data, batches and GO, transactions, modules, and DMVs for ops. I prefer sp_executesql for dynamic SQL when I need parameters.”