• + 0 comments

    I’m encountering an issue while running my query on HackerRank. The same query works perfectly in Oracle SQL Developer, but when I try to run it on HackerRank, I get an error. The query is designed to find prime numbers up to 1000 and list them separated by &.

    Here is my query:

    WITH numbers AS ( SELECT LEVEL AS num FROM dual CONNECT BY LEVEL <= 1000 ),

    prime_candidates AS ( SELECT num FROM numbers WHERE num > 1 ),

    primes AS ( SELECT num FROM prime_candidates np
    WHERE NOT EXISTS ( SELECT 1 FROM prime_candidates pc WHERE pc.num < np.num AND MOD(np.num, pc.num) = 0 ) )

    SELECT LISTAGG(num, '&') WITHIN GROUP (ORDER BY num) AS prime_numbers FROM primes; It works fine in Oracle SQL Developer, but on HackerRank, I get the error:

    output:-

    SP2-0734: unknown command beginning "prime_cand..." - rest of line ignored. SP2-0734: unknown command beginning "primes AS ..." - rest of line ignored. ORA-00942: table or view does not exist

    Could anyone explain why I get this error on HackerRank and if there’s something wrong with my query? I am using Oracle SQL for this query.