Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

It's possible to have a WHERE clause after a HAVING clause?

Tags:

sql

having

where

Is it possible to use a WHERE clause after a HAVING clause?

The first thing that comes to my mind is sub queries, but I'm not sure.

P.S. If the answer is affirmative, could you give some examples?

like image 481
cc. Avatar asked Nov 04 '09 06:11

cc.


People also ask

Can we use both HAVING and WHERE clause together?

Thatswhy, HAVING clause is also called as Post-filter. We cannot use the HAVING clause without SELECT statement whereas the WHERE clause can be used with SELECT, UPDATE, DELETE, etc. WE can use aggregate functions like sum, min, max, avg, etc with the HAVING clause but they can never be used with WHERE clause.

Can we use WHERE after HAVING in SQL?

Yes, an SQL query can contain a WHERE and HAVING clause. You will use these together when you want to extract (or filter) rows for a group of data using a WHERE clause and apply a condition on the aggregate using the HAVING clause.

Can WHERE and HAVING be used in same statement?

Yes, you can absolutely apply a WHERE clause in a query that also utilizes a HAVING statement. When you apply a WHERE clause in the same query, it must always be before any GROUP BY , which in turn must be before any HAVING . As a result, the data is essentially filtered on the WHERE condition first.

Can we use HAVING Without WHERE clause?

These differences are valid for almost all major databases like MySQL, Oracle, SQL Server, and PostgreSQL. Just remember that WHERE is used to filter rows before grouping while HAVING is used to filter rows after grouping. You can also use the AGGREGATE function along with the HAVING clause for filtering.


5 Answers

No, not in the same query.

The where clause goes before the having and the group by. If you want to filter out records before the grouping the condition goes in the where clause, and if you want to filter out grouped records the condition goes in the having clause:

select ...
from ...
where ...
group by ...
having ...

If neither of those are possible to use for some odd reason, you have to make the query a subquery so that you can put the where clause in the outer query:

select ...
from (
   select ...
   from ...
   where ...
   group by ...
   having ...
) x
where ...
like image 116
Guffa Avatar answered Oct 02 '22 13:10

Guffa


A HAVING clause is just a WHERE clause after a GROUP BY. Why not put your WHERE conditions in the HAVING clause?

like image 32
Nestor Avatar answered Oct 02 '22 12:10

Nestor


If it's a trick question, it's possible if the WHERE and the HAVING are not at the same level, as you mentionned, with subquery.

I guess something like that would work

HAVING value=(SELECT max(value) FROM foo WHERE crit=123)

p.s.: why were you asking? Do you have a specific problem?

p.s.s: OK silly me, I missed the "interview*" tag...

like image 41
pascal Avatar answered Oct 02 '22 14:10

pascal


From SELECT help

Processing Order of WHERE, GROUP BY, and HAVING Clauses The following steps show the processing order for a SELECT statement with a WHERE clause, a GROUP BY clause, and a HAVING clause:

The FROM clause returns an initial result set.

The WHERE clause excludes rows not meeting its search condition.

The GROUP BY clause collects the selected rows into one group for each unique value in the GROUP BY clause.

Aggregate functions specified in the select list calculate summary values for each group.

The HAVING clause additionally excludes rows not meeting its search condition.

So, no you can not.

like image 29
Adriaan Stander Avatar answered Oct 02 '22 12:10

Adriaan Stander


Within the same scope, answer is no. If subqueries is allowed then you can avoid using HAVING entirely.

I think HAVING is an anachronism. Hugh Darwen refers to HAVING as "The Folly of Structured Queries":

In old SQL, the WHERE clause could not be used on results of aggregation, so they had to invent HAVING (with same meaning as WHERE):

SELECT D#, AVG(Salary) AS Avg_Sal
  FROM Emp
 GROUP 
    BY D#
HAVING AVG(Salary) > 999;

But would we ever have had HAVING if in 1979 one could write:

SELECT * 
  FROM (
        SELECT D#, AVG(Sal) AS Avg_Sal
          FROM Emp
         GROUP 
            BY D# 
       )
      AS dummy
WHERE Avg_Sal > 999;

I strongly suspect the answer to Darwen's question is no.

like image 31
onedaywhen Avatar answered Oct 02 '22 12:10

onedaywhen