-1

I have the following sql statement in my code:

SELECT country, count(*) as hits WHERE website=? from a GROUP BY country ORDER BY hits DESC

When I run this, it seems to fail. What is wrong with this? Where should I place the WHERE statement?

  • 1
    `SELECT x FROM y WHERE z` The rest is correct, the _from_ goes before the _where_. [See here](https://dev.mysql.com/doc/refman/8.0/en/select.html) for the complete reference for a _select_ query. – Matt Clark Mar 27 '21 at 15:54
  • Thanks, that did the trick. – Streamer358 Mar 27 '21 at 15:55

2 Answers2

1

The order of clause is fixed in SQL. For the ones you are using:

  • SELECT
  • FROM
  • WHERE
  • GROUP BY
  • ORDER BY

This is the language. So you want:

SELECT country, count(*) as hits 
FROM a
WHERE website = ? 
GROUP BY country
ORDER BY hits DESC
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0
  SELECT country, count(*) as hits 
  FROM a  
  WHERE website=? 
  GROUP BY country 
  ORDER BY hits DESC

This query will work after providing Where filter condition