It might be the case that you have selected MySQL in HackerRank, which runs on v.5.7.27, that does not support CTE. Instead, try switching to MS SQL Server for this approach