WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

MySQL · Basic · question 13 of 100

What is the difference between the WHERE and HAVING clauses in a SELECT statement?

📕 Buy this interview preparation book: 100 MySQL questions & answers — PDF + EPUB for $5

In a ‘SELECT‘ statement, the ‘WHERE‘ and ‘HAVING‘ clauses are used to filter the results based on certain conditions.

The ‘WHERE‘ clause is used to filter the rows that are returned in the result set based on a condition that is applied to one or more columns of the table. It is used in conjunction with the ‘SELECT‘, ‘UPDATE‘, and ‘DELETE‘ statements. The condition can consist of a comparison between two values, a range of values, or a set of values. For example, the following query selects all rows from a table named "customers" where the country is "USA":

SELECT * FROM customers WHERE country = 'USA';

The ‘HAVING‘ clause is used to filter the groups of rows returned in a result set based on a condition that is applied to the group as a whole. The ‘HAVING‘ clause is used together with the ‘GROUP BY‘ clause. It applies a condition to the groups created by the ‘GROUP BY‘ clause, and only returns those groups that satisfy the condition. For example, the following query selects all countries from the "customers" table and the number of customers from each country who have made more than 5 orders:

SELECT country, COUNT(*) FROM customers 
GROUP BY country HAVING COUNT(*) > 5;

In this query, the ‘GROUP BY‘ clause groups the rows by country, and the ‘HAVING‘ clause filters the groups where the number of customers is greater than 5.

In summary, the ‘WHERE‘ clause filters individual rows based on a condition, while the ‘HAVING‘ clause filters groups of rows based on a condition.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic MySQL interview — then scores it.
📞 Practice MySQL — free 15 min
📕 Buy this interview preparation book: 100 MySQL questions & answers — PDF + EPUB for $5

All 100 MySQL questions · All topics