Print Prime Numbers SQL Hacker Rank Solution

Hello coders, In this post, you will learn how to solve the Print Prime Numbers SQL Hacker Rank Solution. This problem is a part of the SQL Hacker Rank series.

Print Prime Numbers SQL Hacker Rank Solution
Print Prime Numbers SQL Hacker Rank Solution

Print Prime Numbers SQL Hacker Rank Solution

Problem

Write a query to print all prime numbers less than or equal to 1000. Print your result on a single line, and use the ampersand (&) character as your separator (instead of a space).

For example, the output for all prime numbers  would be:

2&3&5&7

Print Prime Numbers SQL Hacker Rank Solution

SELECT GROUP_CONCAT(NUMB SEPARATOR '&')
FROM (
    SELECT @num:[email protected]+1 as NUMB FROM
    information_schema.tables t1,
    information_schema.tables t2,
    (SELECT @num:=1) tmp
) tempNum
WHERE NUMB<=1000 AND NOT EXISTS(
		SELECT * FROM (
			SELECT @nu:[email protected]+1 as NUMA FROM
			    information_schema.tables t1,
			    information_schema.tables t2,
			    (SELECT @nu:=1) tmp1
			    LIMIT 1000
			) tatata
		WHERE FLOOR(NUMB/NUMA)=(NUMB/NUMA) AND NUMA<NUMB AND NUMA>1
	)

Disclaimer: The above Problem (Print Prime Numbers) generated by Hackerrank but the Solution is Provided by Chase2Learn. This tutorial is only for Educational and Learning purposes.

Sharing Is Caring