I am having problems with a left join SQL query but can\'t see why it isn\'t working. I have 3 tables: customers, purchases and payments, and i\'m trying to select customers who
because BALANCE is an ALIAS given on the expression. Use the expression on the WHERE clause instead of the ALIAS
WHERE COALESCE(b.totalCost , 0) - COALESCE(c.totalPayments , 0) > 0
the other way is to wrap the whole statement with a subquery so you can use the alias on the WHERE clause of the outer query.
The reason why you cannot use ALIAS that is created on the same level of the WHERE clause is because WHERE clause executes first than the SELECT clause where the ALIAS is created.
Here's the SQL Order of Operation: