IBM DB2 Interview Questions
30 DB2 interview questions organised by experience level โ from beginner fundamentals through DBA-grade troubleshooting scenarios. Pairs with SQLMentor's IBM DB2 tutorial if you want to build the underlying skills first.
DB2 Interview Questions
Difficulty: Advanced ยท ~15 min read
Overview
A curated set of DB2 interview questions, organised by experience level. Click each answer to reveal it โ try to formulate your own answer first.
These questions are drawn from real interview loops for DB2 DBA, application developer, and database engineering roles. They favour explaining concepts you'd actually use on the job, not trivia.
Beginner (foundational)
Q1. What is DB2 and what editions exist?
Show answer
DB2 is IBM's family of relational database products. The three main editions are:
- Db2 LUW โ Linux, UNIX, Windows. Most modern enterprise deployments.
- Db2 for z/OS โ IBM mainframes. Different codebase tuned for OLTP at extreme scale.
- Db2 for i โ IBM Power (formerly AS/400). Integrated with the IBM i OS.
A free Db2 Community Edition of LUW exists for development (up to 4 cores, 16 GB RAM).
Q2. What is SYSIBM.SYSDUMMY1?
Show answer
A one-row, one-column system table DB2 provides for evaluating scalar expressions that don't need a real source table โ the equivalent of Oracle's DUAL. Example: SELECT CURRENT_DATE FROM SYSIBM.SYSDUMMY1.
Q3. How do you limit the number of rows returned in DB2?
Show answer
Use the SQL-standard FETCH FIRST n ROWS ONLY clause. For pagination, combine with OFFSET: ... ORDER BY id OFFSET 20 ROWS FETCH FIRST 10 ROWS ONLY (DB2 11.1+).
Q4. Difference between CHAR and VARCHAR?
Show answer
CHAR(n) always uses n characters of storage, padding shorter values with trailing spaces. VARCHAR(n) stores only the bytes you write plus a tiny length prefix. Use VARCHAR for variable-length text; reserve CHAR for genuinely fixed-length codes (ISO country codes, etc.).
Q5. What are the four ACID properties?
Show answer
Atomicity (all-or-nothing), Consistency (constraints hold after every transaction), Isolation (concurrent transactions don't see each other's partial state), Durability (committed changes survive crashes).
Q6. What is a primary key vs. a unique constraint?
Show answer
A PRIMARY KEY is NOT NULL and UNIQUE, and there can only be one per table โ it's the row's identity. UNIQUE columns allow NULLs (multiple NULLs are not considered duplicates in DB2), and a table can have many unique constraints. Both create unique indexes under the hood.
Q7. Where vs. Having?
Show answer
WHERE filters rows before grouping; HAVING filters groups after aggregation. Example: WHERE region = 'EU' filters individual rows; HAVING COUNT(*) > 10 filters groups with more than ten members. WHERE cannot reference aggregate functions; HAVING can.
Intermediate (day-to-day developer)
Q8. What is MERGE and when would you use it?
Show answer
MERGE INTO target USING source ON match_condition performs an upsert โ runs WHEN MATCHED THEN UPDATE ... or WHEN NOT MATCHED THEN INSERT ... in one atomic statement. Use it for nightly sync jobs, replication staging tables, and any pattern where you'd otherwise write "if exists update else insert" in application code.
Q9. Difference between DELETE, TRUNCATE, and DROP?
Show answer
DELETE FROM tโ removes rows, fully logged, can be rolled back, table remainsTRUNCATE TABLE t IMMEDIATEโ removes all rows fast, minimally logged, table remainsDROP TABLE tโ removes table definition and data entirely
Q10. What are DB2's four isolation levels?
Show answer
- UR (Uncommitted Read) โ dirty reads allowed; cheapest
- CS (Cursor Stability) โ default; only sees committed; locks current row
- RS (Read Stability) โ rows you read stay stable for the whole transaction
- RR (Repeatable Read) โ full serializable
Q11. Explain a "covering" index.
Show answer
An index that contains every column the query needs โ both filter columns and projection columns. DB2 satisfies the query entirely from index leaf pages without touching the table. Use CREATE INDEX โฆ (filter_cols) INCLUDE (other_cols) to add non-key columns to the index without bloating its sort order.
Q12. What is a tablespace?
Show answer
A logical storage container that maps tables to physical disk (or memory). DB2 lets you have multiple tablespaces per database, each with its own page size, buffer pool, and containers. This separation enables I/O distribution and per-tablespace backup/recovery.
Q13. What is an MQT?
Show answer
A Materialized Query Table โ DB2's equivalent of a materialized view. It stores the result of a query physically and can be automatically used by the query optimizer for query rewrite when the user issues a similar query. Common for aggregation-heavy reporting workloads.
Q14. What does WITH CHECK OPTION do on a view?
Show answer
When you INSERT or UPDATE through the view, DB2 verifies the new row still satisfies the view's WHERE clause. Without it, you can write rows that "fall outside" the view โ they get stored but are invisible through the view.
Q15. Explain LEFT JOIN vs. INNER JOIN.
Show answer
INNER JOIN returns only rows where the join condition matches in both tables. LEFT JOIN returns every row from the left table plus matching rows from the right โ when there's no match, the right-side columns are NULL. Use LEFT JOIN when "missing on the right side" is information you care about (e.g. departments with no employees).
Q16. Why might WHERE LEFT_TABLE_COL = X after a LEFT JOIN behave like an INNER JOIN?
Show answer
Because the WHERE runs after the join. For unmatched rows the right side is NULL, and NULL = X is unknown (filtered out). Move the filter into the ON clause to keep the unmatched left rows.
Advanced (DBA / senior developer)
Q17. What is the purpose of RUNSTATS?
Show answer
RUNSTATS updates the statistics DB2's cost-based optimizer uses to choose query plans โ row counts, value distributions, index cardinalities. Out-of-date stats are the #1 cause of bad plans. Run it after large data changes, bulk loads, or any time the optimizer's choices look surprising.
Q18. How do you decide if a query is using an index?
Show answer
Run EXPLAIN PLAN FOR <query>, then format the plan with db2exfmt. Look for IXSCAN (good โ used an index) vs. TBSCAN (full table scan). The combination IXSCAN + FETCH means the index found the row IDs, then DB2 fetched the rows. An IXSCAN with no FETCH means index-only access โ the index covered everything the query needed.
Q19. What is a clustering index?
Show answer
A special index that controls the physical order of rows on disk. After REORG, rows are stored in clustering-key order. Range scans on the clustering key are then very fast because adjacent rows are on adjacent pages. A table can have only one clustering index.
Q20. How does DB2 detect and resolve deadlocks?
Show answer
DB2 runs a background deadlock detector at a configurable interval (default 10 seconds). When a deadlock is found, DB2 picks a victim (usually the transaction with the smaller log), rolls it back, and returns SQLCODE -911 reason code 2 to that transaction. Your application code should retry on -911 RC2.
Q21. What is the difference between BEFORE and AFTER triggers?
Show answer
BEFORE triggers run before the row change is applied โ they can modify the NEW row or reject the operation with SIGNAL. AFTER triggers run after the change is applied โ they can read the final state but can't change it. Use BEFORE to validate and rewrite; use AFTER to log and react.
Q22. Explain savepoints.
Show answer
A savepoint is a named marker inside a transaction. SAVEPOINT sp1 ON ROLLBACK RETAIN CURSORS creates one; ROLLBACK TO SAVEPOINT sp1 undoes only the work done after sp1 (the transaction stays open and earlier work is preserved). Useful for "try-this-then-retry" patterns inside a longer transaction.
Q23. When would you choose a cursor over set-based SQL?
Show answer
When you genuinely need per-row procedural logic that can't be expressed in a single SQL statement: branching to different external systems, calling stored procedures per row, complex conditional rewriting, or interactive paging. For most batch transformations, a single UPDATE, MERGE, or INSERT ... SELECT is dramatically faster.
Q24. Why is db2 -td@ often necessary?
Show answer
The default DB2 CLP terminator is ;. SQL PL procedure / trigger bodies contain ; internally โ between statements, after END IF, etc. With the default terminator, the CLP would cut the body off at the first internal ;. Use db2 -td@ -vf script.sql to set a different terminator (@) just for that script.
Q25. What's the difference between WITH UR, CS, RS, RR?
Show answer
| Level | Dirty read | Non-repeatable | Phantom |
|---|---|---|---|
UR |
possible | possible | possible |
CS |
impossible | possible | possible |
RS |
impossible | impossible | possible |
RR |
impossible | impossible | impossible |
CS is the default and right for most OLTP. Use WITH UR for dashboards and reports where speed > strict consistency. Use RR only when business logic truly needs serialisability.
Q26. What is BLU Acceleration?
Show answer
DB2's columnar storage option (introduced in DB2 10.5). Tables can be created ORGANIZE BY COLUMN, storing each column compressed in its own structure. For analytic / scan-heavy workloads this can be 10โ100ร faster than row-organized storage. Trade-off: slower point-row inserts, so it's best for data warehouses, not OLTP.
Scenario / problem-solving
Q27. A query that ran in 200 ms yesterday now takes 30 seconds. What's your first move?
Show answer
In order:
EXPLAIN PLAN FOR <query>โ has the access path changed?- Check
SYSCAT.TABLES.STATS_TIMEโ when were stats last collected? - If stats are stale or the table has grown significantly,
RUNSTATS WITH DISTRIBUTION AND DETAILED INDEXES ALL. - Re-
EXPLAIN. Most of the time the plan switches back. - If not, check
MON_GET_PKG_CACHE_STMTfor the actual rows-fetched, look for skew, and consider an index or query rewrite.
Q28. Inserts on a high-traffic table are slowing down. Why?
Show answer
The most common causes, in order of likelihood:
- Too many indexes โ every insert maintains every index. Audit
SYSCAT.INDEXES; drop unused ones. - Foreign-key cascade overhead โ verify referenced columns have indexes on the child side.
- Triggers running per-row โ review
SYSCAT.TRIGGERS; consider statement-level alternatives. - Lock contention โ
db2pd -locksto inspect. - Log device saturated โ measure I/O.
Q29. How would you migrate a large table to a new schema with zero downtime?
Show answer
A common pattern:
- Create the new table with the new schema.
- Backfill in chunks (commit per chunk) from the old table.
- Add a trigger on the old table to mirror new writes into the new table โ or use CDC / Replication.
- Once caught up and verified, switch app reads to the new table.
- Once writes are also using the new table, drop the trigger/CDC and the old table.
Q30. You see lots of SQLCODE -911 reason code 68 errors. What does that mean?
Show answer
Lock timeout โ the session waited for a lock longer than CURRENT LOCK TIMEOUT allowed. Different from RC2 (deadlock). Causes: long-running transactions holding locks, missing COMMITs, isolation level too high (RR on hot tables), or genuine contention. Fix: shorten transactions, commit more frequently, drop to CS/UR where appropriate, or add the right index so updaters touch fewer rows.
Practice Exercises
- Without looking, write out the four ACID properties and one sentence on each.
- Take any query you wrote in earlier topics and
EXPLAINit. Identify the access methods used. - Describe โ in 2โ3 sentences each โ when you'd reach for:
MERGE, a covering index, an MQT,WITH UR. - Pair up with a study partner: take turns asking each other Q1โQ30 above without looking at the answers.
Quick Quiz
Q1. What's the single biggest cause of bad query plans in DB2?
Show answer
Out-of-date optimizer statistics. Run RUNSTATS after large data changes. Without fresh stats, DB2 underestimates or overestimates cardinalities and picks the wrong access path.
Q2. Why use MERGE instead of IF EXISTS โฆ UPDATE โฆ ELSE INSERT โฆ in application code?
Show answer
MERGE is atomic and race-free at the database level. The app-side check-then-act pattern has a race window where another session can insert the row between your check and your insert, causing a duplicate-key error. MERGE evaluates the match condition and runs UPDATE or INSERT in a single, locked operation.
Q3. What command captures the latest query plan in a formatted file?
Show answer
db2exfmt -d <database> -1 -o <outfile>. The -1 means "the most recent explain". This writes a human-readable plan to <outfile> you can inspect for access methods, costs, and join order.
Next Up
That's the end of the curriculum. Use the DB2 Cheat Sheet as your day-to-day reference.