MetallicPriest
MetallicPriest

Reputation: 30815

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?

Upvotes: 0

Views: 60

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270503

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;

Upvotes: 1

Related Questions