WHILE in SQL – Repetition Structure
Posted: Sun Feb 02, 2025 6:15 am
See how the repetition structure with WHILE works in SQL and learn how to repeat a block of code multiple times.
If you prefer this content in video lesson format, watch the video below or access our YouTube channel !
SQL Icon
SQL
Impressive
Master the most important language in the world for those who work with Data, whether to stand out in your current company, to gain an edge in selection processes or even to enter data areas.
Start now
Right arrow
SQL icon used as backgroundThree images of SQL course classes
WHILE in SQL – Repetition Structure
In today's lesson, we'll explore how the WHILE loop structure works in SQL. This is a common construct in many programming languages, allowing you to repeat a block of code multiple times.
The idea behind using WHILE is to avoid unnecessary repetition of writing the same block of code. By doing this, we can keep executing part of the code until the condition being checked is false.
We will also discuss how to avoid the famous “infinite loop”, which occurs when we do not configure WHILE correctly.
So, follow me as today I will demonstrate how to twitter data create a repetition structure with WHILE in SQL.
Repetition Structure – Logic
Repetition structures are common in many programming languages, and are also present in SQL .
Before we discuss the WHILE structure in SQL in a practical way, let's first understand the logic behind a repetition structure, using a real-life example.
For example, obtaining a driver's license can only be done when we reach 18 years of age. Therefore, here we have a logic of repetition that says: “As long as the person is a minor, he or she cannot drive.” This age verification is done year after year, until the person turns 18 and can drive.
This is a basic example, but it illustrates how a looping structure works. It checks a condition and until that condition is met, it continues to perform a certain action.
WHILE logic in SQL
The logic of WHILE in SQL is exactly this: until a condition is met, the code will continue executing a certain action.
To exemplify this application, we will create a code that performs a count while the determined condition is not met.
To implement this logic, we will need to work with procedures , which we saw in the last lesson . Basically, a procedure is a complete block of code that can be called for execution and is defined by custom delimiters.
Let's create a procedure called counter that receives an integer parameter called limit . Inside it, we will have the integer variable i with an initial value of zero that will help us control the count within the code.
With that done, let's create our while loop structure . The while loop will always be followed by a condition and a block of code to be executed while that condition is true.
In this case, our condition will be that the value of i must be less than the limit defined in the procedure . When the value of i is equal to the limit , this loop will be stopped.
Inside the loop, we will display the value of i with the select command and then we will reset its value by adding 1. This way, as the repetitions go by, the value of i will increase, until the moment it is equal to the defined limit, thus ending the loop.
If you prefer this content in video lesson format, watch the video below or access our YouTube channel !
SQL Icon
SQL
Impressive
Master the most important language in the world for those who work with Data, whether to stand out in your current company, to gain an edge in selection processes or even to enter data areas.
Start now
Right arrow
SQL icon used as backgroundThree images of SQL course classes
WHILE in SQL – Repetition Structure
In today's lesson, we'll explore how the WHILE loop structure works in SQL. This is a common construct in many programming languages, allowing you to repeat a block of code multiple times.
The idea behind using WHILE is to avoid unnecessary repetition of writing the same block of code. By doing this, we can keep executing part of the code until the condition being checked is false.
We will also discuss how to avoid the famous “infinite loop”, which occurs when we do not configure WHILE correctly.
So, follow me as today I will demonstrate how to twitter data create a repetition structure with WHILE in SQL.
Repetition Structure – Logic
Repetition structures are common in many programming languages, and are also present in SQL .
Before we discuss the WHILE structure in SQL in a practical way, let's first understand the logic behind a repetition structure, using a real-life example.
For example, obtaining a driver's license can only be done when we reach 18 years of age. Therefore, here we have a logic of repetition that says: “As long as the person is a minor, he or she cannot drive.” This age verification is done year after year, until the person turns 18 and can drive.
This is a basic example, but it illustrates how a looping structure works. It checks a condition and until that condition is met, it continues to perform a certain action.
WHILE logic in SQL
The logic of WHILE in SQL is exactly this: until a condition is met, the code will continue executing a certain action.
To exemplify this application, we will create a code that performs a count while the determined condition is not met.
To implement this logic, we will need to work with procedures , which we saw in the last lesson . Basically, a procedure is a complete block of code that can be called for execution and is defined by custom delimiters.
Let's create a procedure called counter that receives an integer parameter called limit . Inside it, we will have the integer variable i with an initial value of zero that will help us control the count within the code.
With that done, let's create our while loop structure . The while loop will always be followed by a condition and a block of code to be executed while that condition is true.
In this case, our condition will be that the value of i must be less than the limit defined in the procedure . When the value of i is equal to the limit , this loop will be stopped.
Inside the loop, we will display the value of i with the select command and then we will reset its value by adding 1. This way, as the repetitions go by, the value of i will increase, until the moment it is equal to the defined limit, thus ending the loop.