Bird
Raised Fist0
Interview Prepdatabase-management-systemsmediumAmazonGoogleMicrosoftSwiggyCREDPhonePe

Clustered vs Non-Clustered Index - When to Use Which

Choose your preparation mode3 modes available
▶
Steps
setup

Initialize B-tree representing clustered index

We start with a B-tree of order 3 representing a clustered index on keys [10, 20, 30, 40, 50]. The root node contains keys 20 and 40, with three children nodes.

💡 Setting up the initial B-tree structure is essential to visualize how clustered indexes organize data physically in sorted order.
Line:b_tree = BTree(order=3) b_tree.insert(10) b_tree.insert(20) b_tree.insert(30) b_tree.insert(40) b_tree.insert(50)
💡 The B-tree structure reflects the clustered index where data is stored sorted by key.
📊
Clustered vs Non-Clustered Index - When to Use Which - Watch the Algorithm Execute, Step by Step
Watching the step-by-step traversal and modification of the B-tree helps understand the fundamental differences between clustered and non-clustered indexes and their impact on query performance.
Step 1/15
·Active fill★Answer cell
setup
2040
10
leaf
30
leaf
50
leaf
Initial B-tree setup representing clustered index
searchKey: 30
2040
10
leaf
30
leaf
50
leaf
At root, key 30 is between 20 and 40, traverse middle child
searchKey: 30
2040
10
leaf
30
leaf
50
leaf
Key 30 found in leaf node of clustered index
setup
2545
15
leaf
35
leaf
55
leaf
Initial B-tree setup representing non-clustered index
searchKey: 35
2545
15
leaf
35
leaf
55
leaf
At root, key 35 is between 25 and 45, traverse middle child
searchKey: 35
2545
15
leaf
35
leaf
55
leaf
Key 35 found in leaf node of non-clustered index
insertKey: 25
2040
10
leaf
30
leaf
50
leaf
At root, key 25 is between 20 and 40, traverse middle child for insertion
insertKey: 25
2040
10
leaf
2530
leaf
50
leaf
Inserted key 25 into leaf node without splitting
insertKey: 60
2545
15
leaf
35
leaf
55
leaf
At root, key 60 > 45, traverse right child for insertion
splitKey: 60↑ promoted: 55
254555
15
leaf
35
leaf
55
leaf
60
leaf
Leaf node split after inserting key 60, key 55 promoted to root
insertKey: 55↑ promoted: 55
254555
15
leaf
35
leaf
55
leaf
60
leaf
Root updated with promoted key 55 after leaf split
traverse
Clustered index stores data physically sorted, ideal for range queries
traverse
Non-clustered index stores keys separately, useful for multiple indexing needs
traverse
2040
10
leaf
2530
leaf
50
leaf
Final clustered index B-tree after insertion
traverse
254555
15
leaf
35
leaf
55
leaf
60
leaf
Final non-clustered index B-tree after insertion and split

Key Takeaways

✓ Clustered indexes store data physically sorted by key, optimizing range queries.

This insight is hard to see from code alone because physical data ordering is a conceptual property, not just a data structure detail.

✓ Non-clustered indexes maintain separate B-tree structures with keys and pointers, enabling flexible lookups without affecting data order.

Visualizing separate B-trees clarifies how non-clustered indexes differ structurally and operationally from clustered indexes.

✓ B-tree node splitting and key promotion maintain balance and order, critical for efficient insertions in both index types.

Watching splits and promotions step-by-step reveals the dynamic balancing process that is abstracted away in code.

Practice

(1/5)
1. You have a SQL query with multiple clauses: SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, and LIMIT. To correctly process the query and produce the expected result, which approach best models the order in which these clauses are logically executed by the database engine?
easy
A. Execute clauses in the written order from top to bottom as they appear in the query.
B. Execute clauses in the logical order: FROM -> WHERE -> GROUP BY -> HAVING -> SELECT -> ORDER BY -> LIMIT.
C. Execute all filtering clauses first (WHERE and HAVING), then grouping, then selection, ignoring FROM order.
D. Execute SELECT first to identify columns, then FROM to get tables, then apply filters and limits.

Solution

  1. Step 1: Understand SQL logical execution order

    SQL does not execute clauses in the written order. Instead, it first identifies data sources (FROM), then filters rows (WHERE), groups rows (GROUP BY), filters groups (HAVING), selects columns (SELECT), orders results (ORDER BY), and finally limits output rows (LIMIT).
  2. Step 2: Match problem description to logical execution order

    The logical order ensures correct filtering and grouping before selection and ordering, which matches Execute clauses in the logical order: FROM -> WHERE -> GROUP BY -> HAVING -> SELECT -> ORDER BY -> LIMIT exactly.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Logical execution order matches database engine processing [OK]
Hint: SQL executes clauses logically, not in written order [OK]
Common Mistakes:
  • Assuming SQL executes clauses top-to-bottom as written
2. Which of the following is a common misconception about optimistic concurrency control in high-contention environments?
medium
A. Optimistic concurrency control always improves throughput by avoiding locks
B. It can lead to increased transaction aborts and retries under contention
C. It assumes conflicts are rare and validates data before commit
D. It reduces blocking but may increase latency due to retries

Solution

  1. Step 1: Understand optimistic concurrency assumptions

    Optimistic control assumes conflicts are rare and avoids locking during transaction execution.
  2. Step 2: Analyze behavior under high contention

    When conflicts are frequent, many transactions abort and retry, reducing throughput.
  3. Step 3: Evaluate misconception

    Believing optimistic control always improves throughput ignores the cost of retries and aborts in high-contention scenarios.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Optimistic concurrency is beneficial mainly when conflicts are infrequent.
Hint: Optimistic concurrency shines only when conflicts are rare, not always.
Common Mistakes:
  • Assuming no locks means always better performance
  • Ignoring the cost of transaction retries
  • Confusing optimistic validation with locking
3. Which of the following statements about cardinality in ER diagrams is INCORRECT?
medium
A. In a 1:1 relationship, both entities must have the same number of instances.
B. A 1:N relationship means one entity instance on the 'one' side can relate to many instances on the 'many' side.
C. An N:M relationship requires an associative entity to be properly implemented in a relational schema.
D. Cardinality constraints define the minimum and maximum number of entity instances involved in a relationship.

Solution

  1. Step 1: Analyze statement A

    Incorrect: 1:1 relationship restricts each instance to relate to at most one on the other side, but does not require equal number of instances in both entities.
  2. Step 2: Analyze statement B

    Correct: 1:N means one instance relates to many on the other side.
  3. Step 3: Analyze statement C

    Correct: N:M relationships require associative entities for relational implementation.
  4. Step 4: Analyze statement D

    Correct: Cardinality defines numeric constraints (min and max) on relationships.
  5. Final Answer:

    Option A -> Option A
  6. Quick Check:

    1:1 cardinality restricts relationships, not entity counts.
Hint: Cardinality limits relationships per instance, not total entity counts.
Common Mistakes:
  • Assuming 1:1 means equal number of entities on both sides
  • Confusing cardinality with entity population size
4. Which of the following statements about deadlocks in Two-Phase Locking (2PL) is INCORRECT?
medium
A. Strict 2PL completely prevents deadlocks by ordering lock acquisition
B. Deadlock detection algorithms can resolve deadlocks by aborting one of the transactions
C. Deadlocks occur when two or more transactions wait indefinitely for locks held by each other
D. Deadlocks can arise even if transactions follow the 2PL protocol

Solution

  1. Step 1: Understand deadlock conditions

    Deadlocks happen when transactions cyclically wait for locks held by each other, which can occur under 2PL.
  2. Step 2: Analyze deadlock prevention

    Strict 2PL does not guarantee deadlock prevention; it only ensures serializability.
  3. Step 3: Evaluate options

    A, B, and D are correct statements. C is incorrect because strict 2PL does not prevent deadlocks by ordering lock acquisition.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Deadlocks require detection and resolution even under strict 2PL.
Hint: Strict 2PL ensures serializability but not deadlock prevention [OK]
Common Mistakes:
  • Assuming strict 2PL prevents deadlocks
  • Confusing deadlock detection with prevention
  • Believing 2PL eliminates all concurrency issues
5. Consider a relation R(A,B,C,D) with functional dependencies F = {A -> B, B -> C, C -> D}. You decompose R into R1(A,B), R2(B,C), and R3(C,D). Which of the following statements about this decomposition is TRUE regarding lossless join and dependency preservation?
hard
A. The decomposition is neither lossless nor dependency preserving because the chain of dependencies is broken across relations.
B. The decomposition is lossless and dependency preserving because each FD is represented in one of the decomposed relations.
C. The decomposition is dependency preserving but not lossless because the common attributes do not form keys in the decomposed relations.
D. The decomposition is lossless but not dependency preserving because enforcing A -> B requires joining R1 and R2.

Solution

  1. Step 1: Check lossless join condition

    Each pair of relations shares a key attribute: R1 and R2 share B (key in R2), R2 and R3 share C (key in R3), so lossless join holds.
  2. Step 2: Check dependency preservation

    All FDs appear in one of the relations: A -> B in R1, B -> C in R2, C -> D in R3.
  3. Step 3: Confirm no FD requires join enforcement

    Each FD is enforceable in its own relation, so dependency preservation holds.
  4. Final Answer:

    Option B -> Option B
  5. Quick Check:

    Options A, B, and C incorrectly claim loss or preservation failures.
Hint: If each FD is in one relation and common attributes are keys, decomposition is lossless and dependency preserving.
Common Mistakes:
  • Assuming chain dependencies require joins to enforce
  • Believing lossless join requires all keys in all relations
  • Confusing dependency preservation with global key presence
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard