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

HackerRank: [SQL Alternative Queries] (1/3) DRAW THE TRIANGLE 1 | set, limit, mysql & ms-sql solutions
I started studying SQL from a very famous site - HackerRank. Here I will try to provide multiple approaches & solutions to the same problem. It will help you learn and understand SQL in a better way.

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.


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

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

SET @no_of_lines = 20 + 1;

SELECT REPEAT('* ', @no_of_lines := @no_of_lines -1) 
FROM INFORMATION_SCHEMA.TABLES
WHERE @no_of_lines > 0;


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

SET @no_of_lines = 20 + 1;

SELECT REPEAT('* ', @no_of_lines := @no_of_lines -1) 
FROM INFORMATION_SCHEMA.TABLES
LIMIT 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 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-4: (MS SQL Query):

DECLARE @i INT = 20
WHILE (@i > 0) 
BEGIN
   PRINT REPLICATE('* ', @i) 
   SET @i = @i - 1
END


NOTE: 
  • 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:

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



--------------------------------------------------------------------------------
Click here to see solutions for all Machine Learning Coursera Assignments.
&
Click here to see more codes for Raspberry Pi 3 and similar Family.
&
Click here to see more codes for NodeMCU ESP8266 and similar Family.
&
Click here to see more codes for Arduino Mega (ATMega 2560) and similar Family.
Feel free to ask doubts in the comment section. I will try my best to answer it.
If you find this helpful by any mean like, comment and share the post.
This is the simplest way to encourage me to keep doing such work.

Thanks & Regards,
-Akshay P Daga
إرسال تعليق (0)
أحدث أقدم