My query –
select cu.CustomerID,cu.FirstName,cu.LastName, COUNT(si.InvoiceID)as inv --1
from Customer as cu inner join SalesInvoice as si --2
on cu.CustomerID = si.CustomerID -- 3
-- put the WHERE clause here ! --4
group by cu.CustomerID,cu.FirstName,cu.LastName -- 5
where cu.FirstName = 'mark' -- 6
Output with correct code –

Error i get – Incorrect syntax near the keyword ‘where’.
Can you tell me why I get this error ? I want to know why WHERE comes before GROUP BY and not after.
You have the order wrong. The
WHEREclause goes before theGROUP BY:If you want to perform a filter after the
GROUP BY, then you will use aHAVINGclause:A
HAVINGclause is typically used for aggregate function filtering, so it makes sense that this would be applied after theGROUP BYTo learn about the order of operations here is article explaining the order. From the article the order of operation in SQL is:
Using this order you will apply the filter in the
WHEREprior to aGROUP BY. TheWHEREis used to limit the number of records.Think of it this way, if you were applying the
WHEREafter then you would return more records then you would want to group on. Applying it first, reduces the recordset then applies the grouping.