Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Generate a sequence of square numbers till 10

How to generate a sequence of SQUARE numbers till 10 in MYSQL? (1^2,2^2, etc)

I was only able to generate a numerical sequence from 1 to 10.

WITH RECURSIVE cte (n) AS
(
  SELECT 1
  UNION ALL
  SELECT (n + 1) FROM cte WHERE n < 10
)
SELECT n FROM cte;

But if I add the POW() function, the result will be

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

WITH RECURSIVE cte (n) AS
(
  SELECT 1
  UNION ALL
  SELECT POW((n + 1),2) FROM cte WHERE n < 10
)
SELECT n FROM cte;

RESULT:
1
4
25

>Solution :

you need to square the resul of the cte

WITH RECURSIVE cte (n) AS
(
  SELECT 1
  UNION ALL
  SELECT (n + 1) FROM cte WHERE n < 10
)
SELECT POW(n,2) FROM cte;
POW(n,2)
1
4
9
16
25
36
49
64
81
100

fiddle

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading