Let’s see how we can print the pattern of various type using SQL.
Syntax :
Declare @variable_name DATATYPE -- first declare all the
-- variables with datatype
-- like (int)
select @variable = WITH_ANY_VALUE -- select the variable and
-- initialize with value
while CONDITION -- condition like @variable > 0
begin -- begin
print replicate('*', @variable) -- replicate insert the *
-- character in variable times
set increment/decrement -- in increment/decrement
-- @variable= @variable+1
END -- end while loop
First Pattern :
DECLARE @var int -- Declare SELECT @var = 5 -- Initialization WHILE @var > 0 -- condition BEGIN -- Begin PRINT replicate('* ', @var) -- Print SET @var = @var - 1 -- decrement END -- END |
chevron_right
filter_none
Output :
* * * * * * * * * * * * * * *
Second Pattern :
DECLARE @var int -- Declare SELECT @var = 1 -- initialization WHILE @var <= 5 -- Condition BEGIN -- Begin PRINT replicate('* ', @var) -- Print SET @var = @var + 1 -- Set END -- end |
chevron_right
filter_none
Output :
* * * * * * * * * * * * * * *
Third Pattern :
DECLARE @var int, @x int -- declare two variable SELECT @var = 4,@x = 1 -- initialization WHILE @x <=5 -- condition BEGINPRINT space(@var) + replicate('*', @x) -- here space for -- create spaces SET @var = @var - 1 -- set set @x = @x + 1 -- set END -- End |
chevron_right
filter_none
Output :
* ** *** **** *****
Fourth Pattern :
DECLARE @var int, @x int -- declare two variable SELECT @var = 0,@x = 5 -- initialization WHILE @x > 0 -- condition BEGINPRINT space(@var) + replicate('*', @x) -- here space for -- create spaces SET @var = @var + 1 -- set set @x = @x - 1 -- set END -- End |
chevron_right
filter_none
Output :
*****
****
***
**
*
Recommended Posts:
- Program to print hollow rectangle or square star patterns
- Program to print diagonal star patterns
- Print Patterns in PL/SQL
- Program to print number with star pattern
- Python | Print an Inverted Star Pattern
- Program to print half diamond Number-Star pattern
- Program to print Hut Star pattern
- Print the Alphabets A to Z in Star Pattern
- Program to print half Diamond star pattern
- Programs to print Interesting Patterns
- Program to print solid and hollow square patterns
- Program to print solid and hollow rhombus patterns
- Program to print right and left arrow patterns
- Recursive program to print triangular patterns
- Programs to print Triangle and Diamond patterns using recursion
- SQL | Procedures in PL/SQL
- SQL | Difference between functions and stored procedures in PL/SQL
- Program for Arrow Star Pattern
- Printing triangle star pattern using a single loop
- Programs for printing pyramid patterns in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

