EXPLORING MYSQL
This was a question I encountered on HackerRank, which asked us to print stars in decreasing order based on the given input.
Initially, I had no idea what to do because I didn’t know that SQL also supports loops and conditions. I tried solving this problem by exploring methods on GeeksforGeeks, W3Schools, and other resources. There, I learned how to create a procedure, but I was unable to fully understand how it actually worked.
Get Shaaf Ahmed Kittur’s stories in your inbox
Join Medium for free to get updates from this writer.
Lets see what this query is doing .
1 . The delimiter , which is nothing but what we use to end sentences in our programming languages like c , java, cpp use semicolon( ; ) as their delimiter.
2 . So we have to change the default delimiter to $$ from ; . And now you will be asking why , so first look at our procedure it consists of semicolon ( ; ) on every other line . The default behaviour of mySQL is it ends the statement on “ ; “, so if we don’t change this it can cause error.
3. Then we arrive at the Creation of procedure with syntax CREATE PROCEDURE procedure_name()
4. The two keywords BEGIN and END , define a block of code.
5. As we declare variables in c, java , python , similarly here we have declared variables i , j which are integer type and row_str which will store the array of stars.
6. The above while loop and it’s inner while loop is equivalent to :
i = 20
while i > 0:
j = 1
row_str = "" #Empty String
while j <= i:
row_str += "* " # Appending stars ' * '
j = j + 1
print(row_str)
i = i - 17. After that we END $$ , we are using $$ because we changed the delimiter to $$ earlier from ; .
8. Now again we change the delimiter to ; .
9. At the end we call the function.
Final Thoughts
This problem helped me realize that SQL is more powerful than I initially thought. It’s not just about queries — it also supports procedural programming concepts like loops, variables, and conditions.





