Brian's Blog

items I see across my tribes

SQL Where – Group By - Having

November 09
by briancarter 9. November 2009 08:36

The difference between “having” and “where” is often a source of confusion.  The short answer is that the WHERE clause filters BEFORE grouping. The HAVING clause filters AFTER grouping has occurred.  Below we examine a SQL statement.

Here is how a SELECT works in SQL ... at least in theory. Real products will optimize things when they can:

Example:

SELECT j.Name, COUNT(i.IndividualID) as ICount
FROM Job j
  INNER JOIN Individual i ON j.JobID = i.JobID
WHERE j.IsActive = 1 and i.IsActive = 1
GROUP BY j.Name
HAVING COUNT(i.IndividualID) ≥ 1
ORDER BY ICount

image

  • Start in the FROM clause and build a working table from all of the joins, unions, intersections, and whatever other table constructors are there. The table expression> AS <correlation name> option allows you give a name to this working table which you then have to use for the rest of the containing query.  As you can see with SQL Server, AS is option.  Both “j” and “i” are alias. 
  • Go to the WHERE clause and remove rows that do not pass criteria; that is, that do not test to TRUE (reject UNKNOWN/NULL and FALSE). The WHERE clause is applied to the working in the FROM clause. 
  • Go to the optional GROUP BY clause, make groups and reduce each group to a single row, replacing the original working table with the new grouped table. The rows of a grouped table must be group characteristics: (1) a grouping column (2) a statistic about the group (i.e. aggregate functions) (3) a function or (4) an expression made up of the those three items.
  • Go to the optional HAVING clause and apply it against the grouped working table; if there was no GROUP BY clause, treat the entire table as one group.
  • Go to the optional ORDER BY and sort the grouped working table.
  • Go to the SELECT clause and construct the expressions in the list. This means that the scalar sub queries, function calls and expressions in the SELECT are done after all the other clauses are done. The AS operator can give a name to expressions in the SELECT list, too. If there is a SELECT DISTINCT, then redundant duplicate rows are removed. For purposes of defining a duplicate row, NULLs are treated as matching (just like in the GROUP BY). 
  • Nested query expressions follow the usual scoping rules you would expect from a block structured language like C#, VB, etc. Namely, the innermost queries can reference columns and tables in the queries in which they are contained.

Categories: Development

Comments

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading




 Questions or Feedback, my contact information is located on my About page.


The opinions, thoughts, and comments made in these blog posts are solely my own (unless otherwise stated). They do not reflect the opinions, thoughts or practices of my employer, my universities, my family, or anyone else. Also, I retain the right to change my mind about anything I publish here without having to go back and edit posts that occurred in the past. 

These are my opinions, or just as likely, someone else's opinions that I leveraged for my own.