Looks like a HackerRank issue. Run a select version(). On other challenges, it returns 8.0.33, which matches the version on their execution environment page. But on this challenge it returns 5.7. MySQL prior to v8 does not support CTEs. Answer from Beefourthree on reddit.com
🌐
Reddit
reddit.com › r/sql › can someone please tell me what i am doing wrong here in hackerrank sql question ??
r/SQL on Reddit: can someone please tell me what I am doing wrong here in hackerrank sql question ??
October 1, 2024 - Hackerrank likely uses a version of MySQL that's older than version 8.0. See this fiddle, where version 5.7 produces the same error as you, while version 8 works fine. ... hackerrank doesn't love CTEs for some reason.
🌐
Reddit
reddit.com › r/sql › why is cte not working on hackerrank in my account?
r/SQL on Reddit: Why is CTE not working on HackerRank in my account?
September 8, 2023 -

I am new to Hackerank, so I am not sure if this is something I missed or system-wide.

Use this as an example - https://www.hackerrank.com/challenges/harry-potter-and-wands/problem?isFullScreen=true

select * from wands is fine

But...

with cte_t as

(select * from wands)

select * from cte_t

gives me this error

  • ERROR 1064 (42000) at line 28: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'cte_t as

  • (select * from wands)

  • select * from cte_t' at line 1

I have been trying to solve SQL without CTE, but I am at the point this is not possible without it.

//////////////////////////////

Edits:

Some comments led me to try other compiler, so by using "MS SQL Server" instead solves the problem that "MySQL" has as above.

//////////////////////////////

Another edits:

Still having issues with CTE

The following is fine:

select

sub.id, sub.age, min(sub.coins_needed) as min_coins_needed, sub.power

from (

select

w.id, wp.age, w.coins_needed, w.power

from wands as w

left join wands_property as wp

on w.code = wp.code

where wp.is_evil = 0

) as sub

group by sub.id, sub.age, sub.power

order by sub.power desc, sub.age desc

But this is not:

with fullt as (

select

sub.id, sub.age, min(sub.coins_needed) as min_coins_needed, sub.power

from (

select

w.id, wp.age, w.coins_needed, w.power

from wands as w

left join wands_property as wp

on w.code = wp.code

where wp.is_evil = 0

) as sub

group by sub.id, sub.age, sub.power

order by sub.power desc, sub.age desc

)

select * from fullt;

and giving this (uninformative) error message:

  • Msg 156, Level 15, State 1, Server dbrank-tsql, Line 16

  • Incorrect syntax near the keyword 'select'.

  • Msg 102, Level 15, State 1, Server dbrank-tsql, Line 28

  • Incorrect syntax near ')'.

🌐
HackerRank
hackerrank.com › domains › sql
Solve SQL Code Challenges
SQL · Solve Challenge · Solve Challenge · Solve Challenge · Solve Challenge · Solve Challenge · Solve Challenge · Solve Challenge · Solve Challenge · Solve Challenge · Solve Challenge · Status · Solved · Unsolved · Skills · SQL (Basic) SQL (Intermediate) SQL (Advanced) Difficulty ·
🌐
Stack Overflow
stackoverflow.com › questions › 42349414 › sql-syntax-error-on-hackerrank-challenge-ollivanders-inventory
mysql - SQL syntax error on Hackerrank challenge - Ollivander's Inventory - Stack Overflow
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'm As (Select w.power, wp.age, Min(w.coins_needed) As min_coins' at line 2 · I don't see anything wrong. What am I missing? ... In mysql you should use a select from ( select ... ) and not with
🌐
Stack Overflow
stackoverflow.com › questions › 75936574 › why-in-hackerrank-i-am-getting-wrong-answer-for-the-correct-mysql-code
sql - Why in HackerRank I am getting wrong answer for the correct mysql code? - Stack Overflow
It is a glitch in (or limitation of) the HackerRank parser for SQL input, which apparently cannot handle line breaks very well. If you put at least all of the case expression on one line it will work, like so:
🌐
HackerRank
hackerrank.com › challenges › challenges › forum › comments › 206261
Challenges Discussions | SQL | HackerRank
SQL · Basic Join · Challenges · Discussions · Problem · Submissions · Leaderboard · Discussions · You are viewing a single comment's thread. Return to all comments → · nizamovich 9 years ago+ 4 comments · MSSQL solution with CTE ·
🌐
HackerRank
hackerrank.com › challenges › revising-the-select-query › forum › comments › 442814
Revising the Select Query I Discussions | SQL | HackerRank
SQL · Basic Select · Revising the Select Query I · Discussions · Problem · Submissions · Leaderboard · Discussions · Editorial · You are viewing a single comment's thread. Return to all comments → · marinskiy 8 years ago+ 26 comments · Here is Oracle solution from my HackerrankPractice repository: SELECT * FROM CITY WHERE COUNTRYCODE = 'USA' AND POPULATION > 100000; Feel free to ask if you have any questions :) View more Comments..
Find elsewhere
🌐
HackerRank
hackerrank.com › challenges › weather-observation-station-5 › forum › comments › 290231
Weather Observation Station 5 Discussions | SQL | HackerRank
SQL · Basic Select · Weather Observation Station 5 · Discussions · Problem · Submissions · Leaderboard · Discussions · You are viewing a single comment's thread. Return to all comments → · harikabuddaraju1 9 years ago+ 5 comments · compiler is throwing error(wrong answer) for this query...
🌐
Reddit
reddit.com › r/sql › can someone tell me what i'm doing wrong on this hackerrank question?
r/SQL on Reddit: Can someone tell me what I'm doing wrong on this HackerRank question?
February 26, 2023 - The goal of /r/SQL is to provide a place for interesting and informative SQL content and discussions. ... I haven't used HackerRank specifically too much, but I assume you need to run them as one query.
🌐
HackerRank
hackerrank.com › challenges › weather-observation-station-6 › forum › comments › 123905
Weather Observation Station 6 Discussions | SQL | HackerRank
SQL · Basic Select · Weather Observation Station 6 · Discussions · Problem · Submissions · Leaderboard · Discussions · You are viewing a single comment's thread. Return to all comments → · eternal 10 years ago+ 99 comments · simple solution for MySQL ·
🌐
Reddit
reddit.com › r/learnsql › i'm having trouble with a hacker rank sql problem can someone explain to me why i am wrong.
r/learnSQL on Reddit: I'm having trouble with a hacker rank SQL problem can someone explain to me why I am wrong.
July 25, 2022 -

I have used a nested case statement :

1st case statement checks for if the number is a root or not

2nd case statement check whether n is in p to check it is a leaf or inner node

The following is the error message. First link is my code, second link is the hackerrank question. Thank you in advance.

Error Message :

  • ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'case

  • when n is not in (select p from bst)

  • then 'Leaf'

  • ' at line 4

https://paste.ofcode.org/m2D6x27U8wiZUcVYXTnKKk

https://www.hackerrank.com/challenges/binary-search-tree-1/problem?isFullScreen=true

🌐
HackerRank
hackerrank.com › challenges › interviews › forum › comments › 1376228
Interviews Discussions | SQL | HackerRank
you can't use cte because hackerrank is using mysql < 8.0. if you wanna use cte, you can change mysql to ms sql server. check the link below https://stackoverflow.com/questions/72134462/tried-using-cte-on-hackerrank-its-not-working-what-could-be-the-alternate-sol
🌐
Medium
medium.com › @triyoza.aprianda › sql-hackerrank-solutions-difficulty-easy-part-i-3b39d147fc3a
SQL HackerRank Solutions (Difficulty: Easy) Part I | by Triyoza Aprianda | Medium
July 10, 2025 - In this post, I’ll share my solutions to some SQL problems on HackerRank, categorized as “Easy.” I used the MySQL option on the platform to solve each of the challenges. The case (uppercase or lowercase) of table names, column names, and values in queries does not matter in HackerRank. For example, even if the table or column names are in uppercase in the description, writing them in lowercase still works...
🌐
HackerRank
hackerrank.com › challenges › contest-leaderboard › forum › comments › 1452633
Contest Leaderboard Discussions | SQL | HackerRank
One option is to not use with and make it as a subquery Another is to use MS SQL SERVER instead of MYSQL · This is very strange behaviour because I was able to use WITH in other questions but I went back and checked and found that MYSQL version was 8.0 · https://stackoverflow.com/questions/72134462/tried-using-cte-on-hackerrank-its-not-working-what-could-be-the-alternate-sol
🌐
HackerRank
hackerrank.com › challenges › the-pads › forum › comments › 317988
The PADS Discussions | SQL | HackerRank
SQL · Advanced Select · The PADS · Discussions · Problem · Submissions · Leaderboard · Discussions · You are viewing a single comment's thread. Return to all comments → · dbustosp 9 years ago+ 30 comments · is this solution working for you? I got this when I run the test: Your code did not pass this test case.
🌐
Reddit
reddit.com › r/sql › hackerrank problem solving sql:
r/SQL on Reddit: HackerRank Problem Solving SQL:
June 23, 2022 -

Hi friends

I was solving this hacker rank sql problem " Query the list of CITY names from STATION that do not start with vowels. Your result cannot contain duplicates."

and my solution was

"

Select Distinct CITY From STATION

WHERE City LIKE '[!aeiou]%' ;

"

But for some reason it is not working IS there any mistake that I'm doing , Please let me know !!!

🌐
HackerRank
hackerrank.com › challenges › the-blunder › forum
The Blunder Discussions | SQL | HackerRank
What it asks you for is not what the correct code outputs. The question definitely needs some editing. ... Let me share my analogy before giving the final solution: (i) In SQL Server, when AVG() is applied on an integer expression, SQL Server returns an integer average, which means the fractional part is truncated.
🌐
HackerRank
hackerrank.com › challenges › the-report › forum › comments › 319903
The Report Discussions | SQL | HackerRank
Hi @manikantareddy_2.. Your code works!.. Can you explain this portion of your code "select case when grade<8 then 'NULL' else s.name end,"... This NULL will be assigned to which column..