201k views
0 votes
MySQL: Group By Condition Select the expression that causes a MySQL error. Pick ONE OR MORE options SELECT customer_id, COUNT( * ) FROM transactions GROUP BY customer_id HAVING COUNT( * )>10 SELECT customer_id, COUNT( * ) AS transactions FROM transactions GROUP BY customer_id HAVING transactions > 10 SELECT customer_id, COUNT( * ) AS transactions FROM transactions WHERE transactions > 10 GROUP BY customer_id SELECT customer_id, COUNT( * ) AS transactions FROM transactions GROUP BY 1 HAVING transactions > 10

User CyberEd
by
7.9k points

1 Answer

4 votes

It is to be noted that the expression that causes a MySQL error is "Count transactions, filter more than 10. Incorrect: Use "transactions" in WHERE, refer alias in GROUP BY 1."

Imagine you have a list of transactions, and you want to count how many each customer made. Now, you want to find customers with more than 10 transactions. Here's the catch:

1. The first and second options are correct. They count transactions and then filter those with more than 10.

2. The third option is wrong because you can't use "transactions" in the WHERE clause; you should use HAVING for this after the GROUP BY.

3. The fourth option is also incorrect. In SQL, you use numbers (like 1) to refer to columns, not aliases like "transactions." So, it causes an error.

User TitanFighter
by
8.0k points