Skip to content
Advertisement

How to name a column that is also a function name in SQL?

For example, this doesn’t work,

SELECT a.score, a.r as Rank FROM 
    (SELECT score, dense_rank() over (order by Score desc) as r
        FROM Scores) a
ORDER BY a.r;

That is because Rank is also a function. I tried to replace Rank with [Rank] but that still doesn’t work. So, what is the solution?

Advertisement

Answer

You can give it a different name. That is what I would recommend. But absent that, the escape character in MySQL is the backtick:

SELECT a.score, a.r as `Rank`
FROM (SELECT score, dense_rank() over (order by Score desc) as r
      FROM Scores
     ) s
ORDER BY a.r;

Or the SQL standard, double quotes:

SELECT a.score, a.r as "Rank"
FROM (SELECT score, dense_rank() over (order by Score desc) as r
      FROM Scores
     ) s
ORDER BY a.r;
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement