Complete SQL Mastery / Aggregate Functions / The HAVING Clause / Syntax Error

Here is my code:

SELECT
c.customer_id,
c.first_name,
c.last_name,
SUM(oi.quantity * oi.unit_price) AS spendings
FROM customers c
JOIN orders o USING (customer_id)
JOIN order_items oi USING (order_id)
WHERE state = ‘VA’
ORDER BY
c.customer_id,
c.first_name,
c.last_name
HAVING spendings > 100

Resulting Error Code:

Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘HAVING spendings > 100’ at line 12

Thank you for your help.

You can’t use HAVING without a GROUP BY clause. Since you’re selecting customer details I guess you forgot a GROUP BY c.customer_id clause. And BTW: It doesn’t make much sense to order by first_name and last_name when you already ordered the results by the unique customer_id.

1 Like