I have two tables - Customers and Orders. Customers just contains an ID and the name. Orders contains an Order ID, a Customer ID, and the amount of the Order. My goal is to sum the orders for each customer,even for customers who didn't have any orders (so, a 0 in the result set).
Problem I actually need to show only the entries for which the total for each customer id < 500. I have a WHERE clause for this, but I get an error.
SELECT * FROM orders;
SELECT * FROM customer;
SELECT
c.customer_id, c.customer_name, ISNULL(sum(o.order_total),0) as grand_total
FROM
customer c
LEFT JOIN
orders o
ON
c.customer_id = o.customer_id
--WHERE
--grand_total < 500
GROUP BY
c.customer_id, c.customer_name
ORDER BY
grand_total

I was able to do this with a LEFT JOIN,and have the Customers table on the "left", to get every customer, and to show a 0 when they had no orders. I aliased the sums of each order (by customer) as grand_total. When I use grand_total in and ORDER_BY, the alias is recognized, and the grand_total column sorts as expected. But when I reference the grand_total alias in my WHERE statement (i.e. WHERE grand_total < 500), I get the error Invalid column name 'grand_total'
I'm confused as to why grand_total is recognized in the ORDER_BY but not the WHERE. Aside from that, I'm trying to find the right syntax for a WHERE clause to include only the rows for which grand_total < 500.