What is a cursor, and why is it generally discouraged in favor of set-based operations?

Last updated 3 July 2026

  • A cursor is a database object that processes a result set one row at a time (DECLARE, OPEN, FETCH in a loop, CLOSE, DEALLOCATE).

  • It is discouraged because row-by-row work adds per-row overhead and holds locks longer. Set-based statements let the optimizer use indexes, parallelism and batched I/O.

  • Cursors are acceptable for genuinely sequential, order-dependent tasks (like per-object maintenance) or small result sets where clarity matters more.

  • https://medium.com/@mike.besso/t-sql-red-flag-cursors-avoid-them-9ab5c071ecf2
  • https://www.youtube.com/watch?v=NyvPAPVvG-o
  • https://evidhya.com/subjects/sql/sql-cursors-vs-set-based-processing
  • https://www.tutorialspoint.com/article/set-based-solution-vs-cursor-based-solution
  • https://learn.microsoft.com/en-us/sql/relational-databases/cursors?view=sql-server-ver17
  • https://blog.dataengineerthings.org/database-cursors-in-sql-explaining-basic-and-advanced-usages-fb9c5dfbfba9
All SQL interview questions