Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9019479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:47:13+00:00 2026-06-16T04:47:13+00:00

My query – select cu.CustomerID,cu.FirstName,cu.LastName, COUNT(si.InvoiceID)as inv –1 from Customer as cu inner join

  • 0

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 –

enter image description here

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:47:14+00:00Added an answer on June 16, 2026 at 4:47 am

    You have the order wrong. The WHERE clause goes before the GROUP BY:

    select cu.CustomerID,cu.FirstName,cu.LastName, COUNT(si.InvoiceID)as inv 
    from Customer as cu 
    inner join SalesInvoice as si 
       on cu.CustomerID = si.CustomerID 
    where cu.FirstName = 'mark' 
    group by cu.CustomerID,cu.FirstName,cu.LastName
    

    If you want to perform a filter after the GROUP BY, then you will use a HAVING clause:

    select cu.CustomerID,cu.FirstName,cu.LastName, COUNT(si.InvoiceID)as inv 
    from Customer as cu 
    inner join SalesInvoice as si 
       on cu.CustomerID = si.CustomerID 
    group by cu.CustomerID,cu.FirstName,cu.LastName
    having cu.FirstName = 'mark' 
    

    A HAVING clause is typically used for aggregate function filtering, so it makes sense that this would be applied after the GROUP BY

    To learn about the order of operations here is article explaining the order. From the article the order of operation in SQL is:

    To start out, I thought it would be good to look up the order in which SQL directives get executed as this will change the way I can optimize:

    FROM clause
    WHERE clause
    GROUP BY clause
    HAVING clause
    SELECT clause
    ORDER BY clause
    

    Using this order you will apply the filter in the WHERE prior to a GROUP BY. The WHERE is used to limit the number of records.

    Think of it this way, if you were applying the WHERE after then you would return more records then you would want to group on. Applying it first, reduces the recordset then applies the grouping.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Query 1: SELECT COUNT (A.EMPNO) AS [PRESENT] FROM ERPDATA.NIITESS.DBO.ZEMP_MASXST_WEB_AL A INNER JOIN PPA_EVENTWISEEMPLOYEEMASTER B
Query: select distinct (Customer) ,CustomerID from history where Customer != '' and Customer LIKE
My query of interest is below SELECT COUNT(a.id) FROM users AS a RIGHT JOIN
Query: SELECT u.user_id,r.totalVotes votes,r.totalPoints rating,@row:=@row+1 rank FROM mismatch_user u LEFT JOIN ratingItems r ON
Query: SELECT DISTINCT ([Equipment List].ID) AS Expr1, [Job Assignments].Job FROM [Equipment List] LEFT JOIN
Query: select `r`.`id` as `id` from `tbl_rls` as `r` left join `tblc_comment_manager` as `cm`
Query I'm using: SELECT COUNT(*), SUM(amount) AS amount, FROM_UNIXTIME(added, '%W (%e/%m)') AS daily FROM
Query : select field1 as Word,count(*) as Count from TestAdd1214190 group by field1 order
Query - Select * FROM tbl1 LEFT OUTER JOIN tbl2 ON tbl1.id = tbl2.id
QUERY: mysql> SELECT DISTINCT (stories.id), stories.news_type, story_keywords.keyword FROM (`stories`) LEFT JOIN `story_keywords` ON `stories`.`id`

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.