Thursday 3 May 2018

What is the having clause in SQL?

Purpose:
  • SQL is so pervasive, yet many developers only have casual SQL knowledge. Especially with the advent of ORM technologies and NoSQL it is not uncommon to find developers that have very limited SQL knowledge.
  • That said you would be hard pressed to find an enterprise development environment where SQL does not form part of the mix. Having SQL knowledge also helps to optimize and diagnose bloated ORM generated statements.
  • This question is a simple test to see if someone has gone beyond the very basic SQL commands.

Potential Answer:
A HAVING clause in SQL specifies that an SQL SELECT statement should only return rows where aggregate values meet the specified conditions. It was added to the SQL language because the WHERE keyword could not be used with aggregate functions.

Put simpler it is like a where but works against the query that contains a group by.

SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;

No comments:

Post a Comment