Sitemap
Shivagni Singh Rajput

Shivagni Singh Rajput

Hackerrank Pattern Questions — SQL

2 min readJan 23, 2025

--

Problem:

P(R) represents a pattern drawn by Julia in R rows. The following pattern represents P(5):

* 
* *
* * *
* * * *
* * * * *

Write a query to print the pattern P(20).

Solution :

DELIMITER $$

CREATE PROCEDURE stars(r INT) -- procedure stars() with r as an int
BEGIN
DECLARE star INT DEFAULT 1; -- declare star as a variable
WHILE star <= r DO -- loop as long as star is <= r
SELECT REPEAT('* ', star); -- print out * star number of times
SET star = star + 1; -- increment +1 for each counted row
END WHILE;
END $$

CALL stars(20); -- call procedure with the value 20
  1. You’ll need to use two sets of variables (one input for the rows and another to count all of the rows), in my answer I used “r int” and “star int”.
  2. Make sure that your logic is “star <= r” as you want WHILE loop to only last as long as the value of r (which is your procedure input).

Output:

*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *

Happy coding!

Get Shivagni Singh Rajput’s stories in your inbox

Join Medium for free to get updates from this writer.

Having any questions about the topic? Share them in the comments below!

--

--