Draw The Triangle 2 in SQL | HackerRank Solution

Hello coders, today we are going to solve Draw The Triangle 2 HackerRank Solution in SQL.

Draw The Triangle 2

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 – Draw The Triangle 2 in SQL

MySQL

set @row := 0;
select repeat('* ', @row := @row + 1) from information_schema.tables where @row < 20

Disclaimer: The above Problem (Draw The Triangle 2) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

3 thoughts on “Draw The Triangle 2 in SQL | HackerRank Solution”

  1. DECLARE @row int
    SELECT @row = 1
    WHILE @row < 21
    BEGIN
    PRINT replicate('* ', @row)
    SET @row = @row + 1
    END

    OR

    DECLARE @row int = 1
    WHILE @row < 21
    BEGIN
    PRINT replicate('* ', @row)
    SET @row = @row + 1
    END

  2. Pratiksha Kharat

    set @number = 21;
    select repeat(‘* ‘, @number := @number – 1) from information_schema.tables limit 20;

  3. Mehalan Frankco

    SET @rows = 0;
    SELECT REPEAT(‘* ‘, @rows := @rows + 1)
    FROM INFORMATION_SCHEMA.TABLES
    WHERE @rows < 20;

Leave a Comment

Your email address will not be published. Required fields are marked *