Filtering with HAVING

Improving Query Performance in SQL Server

Dean Smith

Founder, Atamai Analytics

HAVING processing order

1.  FROM
4.  WHERE
6.  HAVING
7.  SELECT
Improving Query Performance in SQL Server

Grouping with WHERE row filter

SELECT Team, 
    SUM(TotalPoints) AS TotalSGTeamPoints
FROM PlayerStats
WHERE Position = 'SG'
GROUP BY Team
Team TotalSGTeamPoints
ATL 2034
BOS 1606
BRK 2126
CHI 2905
CHO 2661
CLE 2489
... ...
Improving Query Performance in SQL Server

Row filtering with HAVING

SELECT Team, 
    SUM(TotalPoints) AS TotalSGTeamPoints
FROM PlayerStats
WHERE Position = 'SG'
GROUP BY Team
SELECT Team, 
    SUM(TotalPoints) AS TotalSGTeamPoints
FROM PlayerStats
--WHERE Position = 'SG'
GROUP BY Team, Position
HAVING Position = 'SG'

Don't use HAVING to filter individual or ungrouped rows

Team TotalSGTeamPoints
ATL 2034
BOS 1606
BRK 2126
CHI 2905
CHO 2661
CLE 2489
... ...
Improving Query Performance in SQL Server

Aggregating by group

SELECT 
    Team, 
    SUM(DRebound+ORebound) AS TotRebounds,
    SUM(DRebound) AS TotDef,
    SUM(ORebound) AS TotOff
FROM PlayerStats
GROUP BY Team;
Team TotRebounds TotDef TotOff
ATL 3436 2693 743
BOS 3645 2878 767
BRK 3644 2852 792
CHI 3663 2873 790
CHO 3728 2901 827
CLE 3455 2761 694
... ... ... ...
Improving Query Performance in SQL Server

Group filtering with WHERE

SELECT 
    Team, 
    SUM(DRebound+ORebound) AS TotRebounds,
    SUM(DRebound) AS TotDef,
    SUM(ORebound) AS TotOff
FROM PlayerStats
WHERE ORebound >= 1000
GROUP BY Team;
  • Use WHERE to filter individual rows and HAVING for a numeric filter on grouped rows
Team TotRebounds TotDef TotOff
Improving Query Performance in SQL Server

Without an aggregate function

SELECT 
    Team, 
    SUM(DRebound+ORebound) AS TotRebounds,
    SUM(DRebound) AS TotDef,
    SUM(ORebound) AS TotOff
FROM PlayerStats
GROUP BY Team
HAVING ORebound >= 1000;
  • Apply the HAVING filter to a numeric column using an aggregate function
------------------------------------------------
-- ERROR
Column 'PlayerStats.ORebound' is invalid in the 
HAVING clause because it is not contained in 
either an aggregate function or the GROUP BY 
clause.
Improving Query Performance in SQL Server

With an aggregate function

SELECT 
    Team, 
    SUM(DRebound+ORebound) AS TotRebounds,
    SUM(DRebound) AS TotDef,
    SUM(ORebound) AS TotalOff
FROM PlayerStats
GROUP BY Team
    -- aggregate function SUM()
HAVING SUM(ORebound) >= 1000; 
Team TotRebounds TotDef TotOff
OKC 3695 2671 1024
Improving Query Performance in SQL Server

Summary

  • Do not use HAVING to filter individual or ungrouped rows
  • Use WHERE to filter individual rows and HAVING for a numeric filter on grouped rows
  • HAVING can only be applied to a numeric column in an aggregate function filter
Improving Query Performance in SQL Server

Let's practice!

Improving Query Performance in SQL Server

Preparing Video For Download...