For loop example in MySQL
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
MySQL does not have a native FOR loop in the same way many procedural languages do. In stored programs, you usually express the same idea with WHILE, LOOP, or REPEAT, and in many cases the better answer is to avoid looping entirely and write a set-based query.
Use WHILE as the Closest Counter-Loop Equivalent
If you want the procedural shape of a classic counter loop, WHILE is usually the clearest choice.
This is the most natural translation of "start at 1, keep going while the condition holds, increment each iteration."
Use LOOP When Exit Logic Is More Manual
LOOP gives you a lower-level structure with an explicit exit.
This form is useful when the stop condition is more complicated than a simple counter check.
Use REPEAT for Do-While Behavior
REPEAT evaluates the stop condition at the end, which means the loop body runs at least once.
This is the MySQL equivalent of a procedural do-while pattern.
Prefer Set-Based SQL When Possible
A lot of SQL loops are a sign that the work could be expressed more efficiently as a set-based operation. For example, generating a short numeric sequence can often be done without procedural iteration.
Set-based SQL is usually easier for the optimizer to handle and easier for other SQL developers to reason about. Use procedural loops only when the task is genuinely stateful or row-by-row in nature.
Cursor Loops Are a Different Case
When each row from a query needs procedural handling, MySQL uses a cursor loop rather than a direct FOR row IN ... syntax.
That style is more verbose than many procedural languages, which is another reason to stay set-based unless iteration is truly necessary.
Add Safety Guards
Whenever you write a procedural loop in MySQL, think about runaway behavior. User-driven loop counts, missing counter updates, or bad exit conditions can create unexpectedly expensive procedures.
Guardrails such as maximum iteration limits or input validation help prevent that.
These checks matter more in SQL procedures than many developers expect because the loop runs inside the database engine itself.
Common Pitfalls
Expecting a direct FOR syntax in MySQL stored procedures is a common misunderstanding.
Using loops for work that should be expressed as set-based SQL often makes the procedure slower and harder to maintain.
Forgetting the counter update or exit condition leads to infinite loops surprisingly easily.
Summary
- MySQL typically uses
WHILE,LOOP, andREPEATinstead of a nativeFORloop. - '
WHILEis the closest match to a normal counter loop.' - '
LOOPandREPEATare useful for more specific control-flow shapes.' - Prefer set-based SQL whenever the logic does not truly require procedural iteration.
Related reading
- Force drop mysql bypassing foreign key constraint
- Force reload the clickhouse config?
- Foreign key between two databases
- Foreign key constraints When to use ON UPDATE and ON DELETE
- Formatting IPv6 as an int in C and storing it in SQL Server
- Frequent Setup and Tear Down of SQL Server Replication
- From an array of ids to an array of names mongo, nodejs
- Full text search with weight in mongoose

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.