diff --git a/changelogs/unreleased/gh-5976-fix-assertion-during-indexed-by.md b/changelogs/unreleased/gh-5976-fix-assertion-during-indexed-by.md
new file mode 100644
index 0000000000000000000000000000000000000000..4a8a5d7d9431839346a405d6858ba1aa943cfd9f
--- /dev/null
+++ b/changelogs/unreleased/gh-5976-fix-assertion-during-indexed-by.md
@@ -0,0 +1,4 @@
+## bugfix/sql
+
+* Fixed assertion when INDEXED BY was used with an index that was at
+  least third in space (gh-5976).
diff --git a/src/box/sql/where.c b/src/box/sql/where.c
index 3474df39f4c718caad1569d6c725448a6d64cfb3..80b4fe880d11026c1dde92c7c450c6603f7b0099 100644
--- a/src/box/sql/where.c
+++ b/src/box/sql/where.c
@@ -4635,10 +4635,10 @@ sqlWhereBegin(Parse * pParse,	/* The parser context */
 					assert(wctrlFlags &
 					       WHERE_ONEPASS_DESIRED);
 					while (pJ->def->iid != idx_def->iid) {
-						iIndexCur++;
 						iid++;
 						pJ = pTabItem->space->index[iid];
 					}
+					iIndexCur++;
 				} else {
 					for(uint32_t i = 0;
 					    i < space->index_count; ++i) {
diff --git a/test/sql-luatest/gh_5976_indexed_by_assertion_test.lua b/test/sql-luatest/gh_5976_indexed_by_assertion_test.lua
new file mode 100644
index 0000000000000000000000000000000000000000..fdcb5171a41901f5e7d602cf6af6912092c0ab2e
--- /dev/null
+++ b/test/sql-luatest/gh_5976_indexed_by_assertion_test.lua
@@ -0,0 +1,24 @@
+local server = require('test.luatest_helpers.server')
+local t = require('luatest')
+local g = t.group()
+
+g.before_all(function()
+    g.server = server:new({alias = 'test_assertion_in_indexed_by'})
+    g.server:start()
+end)
+
+g.after_all(function()
+    g.server:stop()
+end)
+
+g.test_assertion_in_indexed_by = function()
+    g.server:exec(function()
+        local t = require('luatest')
+        box.execute([[CREATE TABLE t (i INT PRIMARY KEY, s STRING);]])
+        box.space.T:create_index('I1', {parts = {2, 'string'}})
+        box.space.T:create_index('I2', {parts = {2, 'string'}})
+        local sql = [[UPDATE t INDEXED BY i2 SET s = 'a' WHERE s = 'z';]]
+        t.assert_equals(box.execute(sql), {row_count = 0})
+        box.execute([[DROP TABLE t;]])
+    end)
+end