niyander-logo

Niyander Tech

Learn with fun

  • Hacker Rank SQL

Draw The Triangle 2 Solution

Draw The Triangle 2 Solution

Hello Friends in this article i am gone to share Hacker Rank SQL Solutions with you | Draw The Triangle 2 Solution

Also Visit:  Draw The Triangle 1 Solution

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

What is a star in programming .

In any programming language, star patterns are a common pattern widely used because it helps to improve logical thinking and flow control knowledge. You must use two or three loops to create a star pattern in the C language. The number of loops depends on the pattern that you need to create. For pattern, a minimum of two is used i.e. one for a row and one for a column. The First loop is called an outer loop that shows the rows, and the second loop is called an inner loop that shows columns.

Star patterns are a series of * or any other character used to create some pattern or any geometrical shape such as – square, triangle(Pyramid), rhombus, heart etc. These patterns are often prescribed by many programming books and are best for practicing flow control statement.

Many programmers around world extremely recommended pattern problems, to enhance logical thinking capabilities. As a novice programmer you must practice these patterns to get a good hands on logical thinking and program flow control.

Disclaimer: The above Problems are generated by Hacker Rank but the Solutions are Provided by NYANDER.COM. All Hacker Rank SQL Solutions Shared only for Educational and Learning Purpose.

Leave a Reply Cancel reply

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

Save my name, email, and website in this browser for the next time I comment.

  • GDPR Privacy Policy
  • Privacy Policy
  • Terms and Conditions

Greetings, Hey i am Niyander, and I hail from India, we strive to impart knowledge and offer assistance to those in need.

do-while-loop-SQL-with-repeat-function.png

Hackerrank – Draw the Triangle 1 – SQL – Solution

Publish Date - November 22nd, 2022

Last Modified - March 7th, 2023

Another really interesting SQL Hackerrank problem, which forces you to break down a problem and build a procedure. While I know there are multiple ways to handle this problem (like most), I feel like this way allows you to flex multiple skills and try out some different functions.

Table of Contents

The 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) .

The solution

As stated before, I built out a procedure which called stars which executes when called. You need to know the inner workings of how SQL procedures work, which can easily be learned by watching some tutorials on Youtube, or learning SQL on Udemy.

This problem first confused me greatly, because I thought I could just print the stars out (which you can do and that’s extremely tedious). If you wanted to could just print out a bunch of stars.

Which returns this:

Not very clean or reusable code though. Therefore, I used 5 major components to solve this question, so that it could be done programmatically for any number inputted. Those components are the following:

  • DELIMITER – Due to the need to execute multiple lines of queries (including the procedure call at the end).
  • PROCEDURE itself, which allows you to start and end a block of SQL code.
  • A DO WHILE loop which run for a set amount of time dictated.
  • REPEAT function, which actually does all the heavy lifting and the printing of the stars (*).
  • SET, which sets the actual incrementation

I’ve added comments to the procedure to walk you through it:

A tougher one for sure, but doing it this way really cements how to handle procedures and the neat things you can do in SQL! There is a very similar question, “Draw the Triangle 2” that takes this to the next level!

Feel free to check out my articles on related technologies:

  • Best Web Development Courses on Udemy
  • Best Python courses on Udemy
  • Best Digital marketing udemy course

Leave a Comment Cancel Reply

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

Save my name, email, and website in this browser for the next time I comment.

  • Alternative Queries

Draw The Triangle 2

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) .

Query Examples

Print Pattern Examples Using Sql

Print Pattern Examples Using Sql

There are 11 star pattern examples in this tutorial. I will add more example…

In Sql we use replicate function to print pattern easily.

How Replicate Function Works In Sql

The Replicate function get to parameters. The first parameter is the character we want to print and the second parameter is, how many times we want to print the character. Example Select replicate(‘*’,5); The Code prints star character 5 times side by side. Now we can print patterns ;

1. Pattern: Print Left Triangle Example

write a query to print the pattern p(20)

2. Pattern: Print Right Triangle Example

write a query to print the pattern p(20)

3. Pattern: Print Reverse Left Triangle Example

write a query to print the pattern p(20)

4. Pattern: Print Reverse Right Triangle Example

write a query to print the pattern p(20)

5. Pattern: Print Pyramid Example

write a query to print the pattern p(20)

6. Pattern: Print Diamon Example

write a query to print the pattern p(20)

7. Pattern: Print Twisted Sentence Example

write a query to print the pattern p(20)

8. Pattern: Print Pyramid Example

Result of Code

write a query to print the pattern p(20)

9. Pattern: Print Hollow Diamond Example

You can change max value to see different view. But the value must be odd number.

write a query to print the pattern p(20)

10. Pattern: Print Right Arrow Example

write a query to print the pattern p(20)

11. Pattern: Print A Word Triangle Example

write a query to print the pattern p(20)

12. Pattern: Print Right Arrow – 2

write a query to print the pattern p(20)

That’s all for now. I will write new ones soon as possible. I hope you like it.

YOU MAY WANT TO SEE OUR ALL EXAMPLES PAGE, THEN CLICK HERE

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)

You may also like

write a query to print the pattern p(20)

Mysql Add Two Number With Stored Procedure and Output...

Örnek kütüphane veritabanı i̇ndir (verili), find the month deference between two dates, find the day difference between two dates, sql multistatement table valued function examples, adım adım mssql serverda veritabanı bağlama (attach)....

[…] IF YOU WANT TO SEE MORE THAN CLICK […]

[…] IF YOU WANT TO SEE MORE PATTERN EXAMPLES THAN CLICK […]

HEY I WANT A SQL QUERY FOR THISPATTERN 1 11 111 1111 11111 22222 2222 222 22 2

Answer… Declare @i int,@a int Set @a = 4 Set @i = -@a; While (@i<=@a) Begin Set @i = @i + 1 if(@i<0) Print replicate('1 ',@a - abs(@i)) else if(@i=0) Begin Print replicate('1 ',@a - abs(@i)) Print replicate('2 ',@a - abs(@i)) End else Print replicate('2 ',@a - abs(@i)) End

Leave a Comment X

Save my name, email, and website in this browser for the next time I comment.

Notify me of follow-up comments by email.

Notify me of new posts by email.

This site uses Akismet to reduce spam. Learn how your comment data is processed .

Tech Lead Curiosity

write a query to print the pattern p(20)

SQL Challenge: Draw The Triangle 1

(easy) hackerrank challenge.

write a query to print the pattern p(20)

Challenge : 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).

In this article, I try explain two solutions in MS SQL using WHILE statement approach and recursive CTE approach. There are some other hacky solutions for this challenge but I decided to skip them as I couldn’t perceive any educational value for them.

Solution 1 :

The first solution uses WHILE statement. WHILE is a control-flow statement that allows you to execute a block code (starts with BEGIN and finishes with END ) repeatedly as long as the specified condition is TRUE .

First DECLARE a variable and give it an initial value. A variable stores some information temporarily during the execution of code.

Then start the WHILE statement with the condition @counter > 0 . The code block is to PRINT a replication of '* ' . As you can see, in each repetition, we decrease the counter value by 1.

Solution 2 (In MS SQL):

Here, I solve this problem with recursive CTE, which references itself. In recursive CTE, it repeatedly executes and returns subsets of data until it returns the complete result set.

Here is the final solution.

Now, let’s dig more into the recursive CTE code. It can be broken down into three parts:

Part 1: Initial query to set the base result. The base result is also known as anchor member. In this recursive CTE, the anchor member is 20.

Part 2: A recursive query that calls the CTE and perform a recursive action using the anchor member. Keep in mind that this recursive query must be UNION ALL -ed with the anchor member.

Part 3: defining a termination condition for the recursive query. In this problem, the termination condition is when the counter becomes zero.

As you can see the whole goal of recursive CTE in this solution is to create a list of 20 numbers.

If you found this article helpful, share it with your friends and colleagues. If you have any other questions, you can find me on Linkedin or send me an email [email protected].

write a query to print the pattern p(20)

Ready for more?

Coding Made Simple

Draw The Triangle 1 in SQL | HackerRank Solution

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

Draw The Triangle 1

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 1 in SQL

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

Leave a Comment Cancel Reply

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

Save my name, email, and website in this browser for the next time I comment.

Could someone explain me why this code creates automatic 20 lines?

Hello everyone! I was trying to solve some internet exercises these days, and got some doubts on this one here.

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

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

I found on internet the solution

SELECT REPEAT(’* ', @NUMBER := @NUMBER - 1) FROM information_schema.tables, (SELECT @NUMBER :=21) t LIMIT 20

But I really didn`t understand this code. Can someone explain me this code, and how is this code able to create 20 new lines?

@text5047161651 ,

Excellent question!

In SQL, when you just SELECT a number or string with no FROM clause, the query will return one row with that value. However, when you SELECT a number or string FROM a certain table, the query will return that value once per row for every row in the table.

In this query…

…you are selecting FROM the table called information_schema.tables , which generally has more than 20 rows. Thus, the number or string you select from that table will appear in the same number of rows that information_schema.tables contains.

Now, because the SELECT statement decreases @NUMBER by 1 every time, each row shows one less star. So, technically, after 20 rows, each following row contain '* ' less than one time ( @NUMBER will be less than one). This will show up as empty rows, which is why you limit the query to the 20 rows that actually contain something.

Hope this helps!

Happy coding.

APDaga DumpBox : The Thirst for Learning...

  • 🌐 All Sites
  • _APDaga DumpBox
  • _APDaga Tech
  • _APDaga Invest
  • _APDaga Videos
  • 🗃️ Categories
  • _Free Tutorials
  • __Python (A to Z)
  • __Internet of Things
  • __Coursera (ML/DL)
  • __HackerRank (SQL)
  • __Interview Q&A
  • _Artificial Intelligence
  • __Machine Learning
  • __Deep Learning
  • _Internet of Things
  • __Raspberry Pi
  • __Coursera MCQs
  • __Linkedin MCQs
  • __Celonis MCQs
  • _Handwriting Analysis
  • __Graphology
  • _Investment Ideas
  • _Open Diary
  • _Troubleshoots
  • _Freescale/NXP
  • 📣 Mega Menu
  • _Logo Maker
  • _Youtube Tumbnail Downloader
  • 🕸️ Sitemap

HackerRank: [SQL Alternative Queries] (2/3) DRAW THE TRIANGLE 2 | set, limit, mysql & ms-sql solutions

HackerRank: [SQL Alternative Queries] (2/3) DRAW THE TRIANGLE 2 | set, limit, mysql & ms-sql solutions

Please make use of my blog posts for learning purpose only and feel free to ask your questions in the comment box below in case of any doubt.

Click Here for the previous blog-post in the series.

Recommended SQL Courses: Udemy: SQL - MySQL for Data Analytics and Business Intelligence LinkedIn: Master SQL for Data Science edX: Databases: Advanced Topics in SQL edureka: SQL Essentials Training & Certification Coursera: Learn SQL Basics for Data Science Eduonix: SQL Queries

SQL Problem Statement:

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-1: (MySQL Query):

  • The INFORMATION_SCHEMA.TABLES view allows you to get information about all tables and views within a database. By default it will show you this information for every single table and view that is in the database.
  • the actual values of INFORMATION_SCHEMA.TABLES is not required as such but for running the sql query it has to refer to a table.

Solution-2: (MySQL Query):

Solution-3: (ms sql query):.

  • The DECLARE statement is used to declare a variable in SQL and its value is set to NULL.
  • SET is for initializing the variable you declared previously, and in MS-SQL, you cannot SET the variable until you DECLARE it.

Expected Output:

Our website uses cookies to improve your experience. Learn more

Contact form

  • Trending Now
  • Data Structures & Algorithms
  • Foundational Courses
  • Data Science
  • Practice Problem
  • Machine Learning
  • System Design
  • DevOps Tutorial
  • Web Development
  • Web Browser

Related Articles

  • Print Patterns in PL/SQL
  • Finding sum of first n natural numbers in PL/SQL
  • PLSQL | VSIZE Function
  • PLSQL | LTRIM Function
  • PLSQL | SINH Function
  • No. of vowels and consonants in a given string in PL/SQL
  • Convert the given numbers into words in Pl/SQL
  • GCD of two numbers in PL/SQL
  • PLSQL | FLOOR Function
  • PLSQL | CEIL Function
  • PLSQL | TRANSLATE Function
  • PLSQL : || Operator
  • PLSQL | LOWER Function
  • PLSQL | ASIN Function
  • Swap two numbers in PL/SQL without using temp
  • PLSQL | ABS Function
  • PLSQL | CONVERT Function
  • PLSQL | SUBSTR Function
  • Check whether a string is palindrome or not in PL/SQL

Print different star patterns in SQL

Please login to comment....

  • pattern-printing
  • 10 Best ChatGPT Prompts for Lawyers 2024
  • What is Meta’s new V-JEPA model? [Explained]
  • What is Chaiverse & How it Works?
  • Top 10 Mailchimp Alternatives (Free) - 2024
  • Dev Scripter 2024 - Biggest Technical Writing Event By GeeksforGeeks

Improve your Coding Skills with Practice

 alt=

What kind of Experience do you want to share?

IMAGES

  1. Python Exercise: Print alphabet pattern P

    write a query to print the pattern p(20)

  2. How To Write An Email Query Letter

    write a query to print the pattern p(20)

  3. Python Programming for Pattern Printing

    write a query to print the pattern p(20)

  4. Python Program to Print Square Number Pattern

    write a query to print the pattern p(20)

  5. How to solve any number patterns in python

    write a query to print the pattern p(20)

  6. P(R) represents a pattern drawn by Julia i

    write a query to print the pattern p(20)

VIDEO

  1. 5. Query Processing (part 2)

  2. 43 Execute Query with Select Statement ResultSet Loop to print Prepared Statement

  3. Python pattern programs How to print stars "*" in square shape (Tutorial 33)

  4. Write C Program to Print Pattern N With Input Character & No of Rows

  5. Write c program to print following pattern. ( pyramid)

  6. Paper Pattern + Strategy

COMMENTS

  1. mysql

    The following pattern represents P(5): Write a query to print the pattern P(20). the question. I have written the answer like this: DELIMITER $$ BEGIN DECLARE CH VARCHAR(255); DECLARE CT INT DEFAULT 20; SET CH = ' *'; WHILE CT>0 DO SELECT REPEAT(CH,CT); SET CT=CT-1; END WHILE END $$ DELIMITER ; Somehow it's not working..what's missing?

  2. Draw The Triangle 2 in SQL

    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.

  3. HackerRank: [SQL Alternative Queries] (1/3) DRAW THE TRIANGLE 1

    Write a query to print the pattern P(20). Solution-1: (MySQL Query): SET @ no_of_lines = 20 + 1; SELECT REPEAT('* ', @ no_of_lines : = @ no_of_lines -1) FROM INFORMATION_SCHEMA.TABLES; NOTE: The INFORMATION_SCHEMA.TABLES view allows you to get information about all tables and views within a database. By default it will show you this information ...

  4. How to print a triangle pattern in SQL

    Write a query to print the pattern P(20). I will call the asterisk a star for this article. Note that each star is separated from the one next to it by only one space. STEP 1: To solve this query, you first need to check how your pattern is designed. From the above picture, we can see that the first row has only one star, and they increase by ...

  5. Draw The Triangle 2 Solution

    Write a query to print the pattern P(20). Solution - Draw The Triangle 2 MySQL Code set @row := 0; select repeat('* ', @row := @row + 1) from information_schema.tables where @row < 20 What is a star in programming ? In any programming language, star patterns are a common pattern widely used because it helps to improve logical thinking and ...

  6. Hackerrank

    The 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).. The solution-- For an N amount of rows (with an input) print the descending amount of stars.

  7. Draw the Triangle 2

    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). \n SOLUTION \n. WITH RECURSIVE rnum(n)\n AS (SELECT 1 \n UNION ALL \n SELECT n + 1 \n FROM rnum\n WHERE n < 20)\n SELECT REPEAT(' * ', n)\n FROM rnum; \n ...

  8. Draw The Triangle 2

    Draw The Triangle 2. 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). Enter your query here.

  9. Print Pattern Examples Using Sql

    The Replicate function get to parameters. The first parameter is the character we want to print and the second parameter is, how many times we want to print the character. Example Select replicate ('*',5); The Code prints star character 5 times side by side. Now we can print patterns ; 1. Pattern: Print Left Triangle Example.

  10. SQL Challenge: Draw The Triangle 1

    Challenge: 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). In this article, I try explain two solutions in MS SQL using WHILE statement approach and recursive CTE approach. There are some other hacky solutions for this challenge but I decided to skip them as I couldn't perceive ...

  11. SQL HackerRank Solutions. A complete solution for SQL problems on…

    Given the table schemas below, write a query to print the company_code, founder name, total number of lead managers, total number of senior managers, ... Write a query to print the pattern P(20). set @row := 0; select repeat('* ', @row := @row + 1) from information_schema.tables where @row < 20.

  12. Hackerrank

    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). The Solution ... (20);: This line calls the draw_triangle stored procedure with an argument of 20, meaning it will draw a triangle with 20 rows of stars.

  13. GitHub: Let's build from here · GitHub

    {"payload":{"allShortcutsEnabled":false,"fileTree":{"":{"items":[{"name":"African Cities","path":"African Cities","contentType":"file"},{"name":"Asian Population ...

  14. Draw The Triangle 1 in SQL

    Write a query to print the pattern P(20). Solution - Draw The Triangle 1 in SQL MySQL SELECT REPEAT('* ', @NUMBER := @NUMBER - 1) FROM information_schema.tables, (SELECT @NUMBER:=21) t LIMIT 20. Disclaimer: The above Problem (Draw The Triangle 1) is generated by Hacker Rank but the Solution is Provided by CodingBroz.

  15. How to draw with SQL?

    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). My solution for MySQL 8.0 (here and below):

  16. Could someone explain me why this code creates automatic 20 lines?

    Hello everyone! I was trying to solve some internet exercises these days, and got some doubts on this one here. P® represents a pattern drawn by Julia in R rows. The following pattern represents P(5):. Write a query to print the pattern P(20).. I found on internet the solution

  17. Hacker rank|SQL(Easy) |Draw The Triangle 2 |By Kiruthicka GP

    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). ROW_NUMBER() OVER (): This is a window function that assigns a ...

  18. HackerRank: [SQL Alternative Queries] (2/3) DRAW THE TRIANGLE 2

    Write a query to print the pattern P(20). Solution-1: (MySQL Query): SET @ no_of_lines = 0; SELECT REPEAT('* ', @ no_of_lines : = @ no_of_lines + 1) FROM INFORMATION_SCHEMA.TABLES WHERE @ no_of_lines < 20; NOTE: The INFORMATION_SCHEMA.TABLES view allows you to get information about all tables and views within a database. By default it will show ...

  19. Print different star patterns in SQL

    Print different star patterns in SQL. Read. Courses. 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.

  20. How to draw a triangle in SQL Server?

    You can use REPLICATE to repeat a character a certain number of times. To generate a sequence of numbers from 1 to 20 you don't need a WHILE anyway - SQL doesn't really need the WHILE statement to work with data.. Number sequences are always useful which is why almost every SQL developer creates a Numbers table.. If you don't already have one, a quick and dirty way to generate 20 numbers is to ...

  21. sql

    Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question.Provide details and share your research! But avoid …. Asking for help, clarification, or responding to other answers.