'SQL query by adding two columns in where clause?

I have a database table that contains two scores:

  • scoreA
  • scoreB

I am trying to make a SQL query by adding these two values, such as

SELECT *,(scoreA+scoreB) as scoreC FROM data WHERE scoreC > 100 ORDER BY scoreC DESC

However, it shows an error:

ERROR: Unknown column 'scoreC' in 'where clause'

Is there any way to work around for this?

P.S. the reason I don't add a new column for scoreC is because scoreA/scoreB are updated continuously by other cron jobs and if I want to calculate scoreC, I need to make extra queries/updates for scoreC, which I would like to avoid to save system resources. However, if it is the only way to calculate scoreC by another cron job, I am also ok with it if it's the only solution. Thanks.



Solution 1:[1]

MySQL supports a non-standard extension that allows you to use the having clause in this case:

SELECT *, (scoreA+scoreB) as scoreC
FROM data
HAVING scoreC > 100
ORDER BY scoreC DESC;

Let me emphasize that this is MySQL-specific. For a simple expression such as this, I would just put the formula in the where clause. But sometimes the expression can be quite complicated -- say a distance formula or complex case or subquery. In those cases, the having extension is actually useful.

And, you don't need the formula in the order by clause. MySQL allows column aliases there.

Solution 2:[2]

In most ANSI compliant RDBMS, you won't be able to use the derived column ScoreC in the where clause. However, you can do this:

SELECT *
FROM 
(
   SELECT *, (scoreA + scoreB) as scoreC 
   FROM data 
) SummedScores
WHERE SummedScores.scoreC > 100 
ORDER BY SummedScores.scoreC DESC;

where SummedScores is a derived table

Fiddle here

Solution 3:[3]

You cannot use the calculated fields name in where and order by but you can use the formula:

SELECT scorea + scoreb FROM data WHERE scorea+scoreb > 100 order BY scorea+scoreb

You also could create a view with the field scorec.

Solution 4:[4]

SELECT *,(scoreA+scoreB) as scoreC 
FROM data 
WHERE (scoreA+scoreB) > 100 
ORDER BY (scoreA+scoreB) DESC

Solution 5:[5]

You can use HAVING clause for this.

SELECT *,(scoreA+scoreB) as scoreC 
FROM data 
HAVING scoreC > 100
ORDER BY scoreC DESC;

From documentation: "The SQL standard requires that HAVING must reference only columns in the GROUP BY clause or columns used in aggregate functions. However, MySQL supports an extension to this behavior, and permits HAVING to refer to columns in the SELECT list and columns in outer subqueries as well."

Solution 6:[6]

You might be looking for HAVING clause

SELECT *,(scoreA+scoreB) as scoreC 
FROM data HAVING scoreC > 100 
ORDER BY scoreC DESC

Solution 7:[7]

now it's possible to add condition with column names in where clause like this

select * from hotel_rooms where (total_pax + extra_beds >= 3);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gordon Linoff
Solution 2
Solution 3 scraatz
Solution 4 Simone Nigro
Solution 5 Alexander
Solution 6 Praveen Prasannan
Solution 7 kamalakar