Q
Q
quadabrashell2019-06-03 18:10:43
SQL
quadabrashell, 2019-06-03 18:10:43

What is the difference between HAVING and WHERE?

Why can't this selection be rewritten using WHERE ?

SELECT StudentId FROM EXAM_RESULT GROUP BY StudentID HAVING MIN(Mark) = 5

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2019-06-03
@quadabrashell

From documentation :

The main difference between WHERE and HAVING is that WHERE first selects rows and then groups them and evaluates aggregate functions (so it selects rows to evaluate aggregates), while HAVING selects group rows after grouping and computing aggregate functions. As a consequence, the WHERE clause must not contain aggregate functions; it doesn't make sense to use aggregate functions to define strings to evaluate aggregate functions. The HAVING clause, on the other hand, always contains aggregate functions. (Strictly speaking, you can write a HAVING clause without using aggregates, but this is rarely useful. The same condition can work more efficiently in the WHERE stage.)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question