Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I print a triangle of stars using SQL

Is it practically possible to create a triangle of stars like this as below in SQL.I know that this could be done easily in any other programming language like C,C++,Java but want to know whether it is really possible with just SQL or PL/SQL.I tried working on it with dual table in Oracle but couldn't get through it.

  *              *
 * *             * * 
* * *    or      * * *

Can someone please shed somelight if anyone knows about it.

like image 994
Teja Avatar asked Mar 29 '12 17:03

Teja


People also ask

How do you find a triangle in SQL?

Use: select case when (A+B<=C or B+C<=A or A+C<=B) then 'Not A Triangle' when (A=B and B=c) then 'Equilateral' when (A=B AND C<>B) or (B=C AND C<>A) or (A=C AND A<>B) then 'Isosceles' else 'Scalene' end as triangle_type from TRIANGLES; Here, it will strictly check for the type of the triangles.

How do you create a pyramid in SQL?

[Equilateral Traingle] We can make a pyramid with Oracle SQL as follows. select rpad(' ',5 -level) || rpad( '* ', level*2, '* ' ) from dual connect by level <= 5; ** Here 5 is the number of lines. Save this answer.


1 Answers

The simplest approach would be something like this. You can get more sophisticated particularly if you want to build the equilateral triangle rather than the right triangle.

SQL> ed
Wrote file afiedt.buf

  1  select rpad( '* ', level*2, '* ' )
  2    from dual
  3* connect by level <= 3
SQL> /

RPAD('*',LEVEL*2,'*')
--------------------------------------------------------------------------------
*
* *
* * *
like image 116
Justin Cave Avatar answered Sep 21 '22 19:09

Justin Cave