WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

SQL & Databases · Intermediate · question 25 of 100

Can you explain the concepts of dirty read, non-repeatable read, and phantom read in the context of database transactions?

📕 Buy this interview preparation book: 100 SQL & Databases questions & answers — PDF + EPUB for $5

In the context of database transactions, dirty reads, non-repeatable reads, and phantom reads are phenomena that can occur when multiple transactions access the same data concurrently.

Dirty read: A dirty read occurs when a transaction reads data that has been modified by another transaction but not yet committed. This can happen in the READ UNCOMMITTED isolation level. Dirty reads can lead to incorrect or inconsistent data, as the uncommitted changes may be rolled back by the modifying transaction, leaving the data in an inconsistent state.

For example, consider two transactions T1 and T2, where T1 modifies a row in a table and T2 reads the same row before T1 commits the change. If T1 rolls back the change, T2 will have read a value that was never actually committed, resulting in a dirty read.

Non-repeatable read: A non-repeatable read occurs when a transaction reads the same data twice but gets a different result because another transaction has modified the data in between the two reads. This can happen in the READ COMMITTED and REPEATABLE READ isolation levels.

For example, consider two transactions T1 and T2, where T1 reads a row from a table, and then T2 modifies the same row before T1 reads it again. If T1 gets a different value on the second read, it results in a non-repeatable read.

Phantom read: A phantom read occurs when a transaction reads a set of rows that satisfy a certain condition, but a subsequent read of the same set of rows returns different results because another transaction has inserted or deleted rows that meet the same condition. This can happen in the REPEATABLE READ and SERIALIZABLE isolation levels.

For example, consider two transactions T1 and T2, where T1 reads all the rows from a table that meet a certain condition, and then T2 inserts a new row that meets the same condition. If T1 reads the same set of rows again, it will see the new row, resulting in a phantom read.

To prevent these phenomena, database systems use various techniques, such as locking, concurrency control, and isolation levels. Choosing the right isolation level for a transaction depends on the specific requirements of the application and the level of concurrency required. In general, higher isolation levels provide stronger guarantees of data consistency, but they can also lead to longer transaction times and lower concurrency. Conversely, lower isolation levels provide better performance and concurrency but may result in inconsistent data.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic SQL & Databases interview — then scores it.
📞 Practice SQL & Databases — free 15 min
📕 Buy this interview preparation book: 100 SQL & Databases questions & answers — PDF + EPUB for $5

All 100 SQL & Databases questions · All topics