0% found this document useful (0 votes)
351 views3 pages

HAVING Clause Practice Problems

This document provides examples of SQL queries using the HAVING clause to filter aggregated results. The queries retrieve sales data from various tables in the AdventureWorks database, filtering for territories with over $10 million in sales, colors associated with at least 20 products, products ordered over 200 times in 2006, and customers with 6 or more orders between 2005-2006. The solutions demonstrate the use of aggregation functions like SUM and COUNT along with the HAVING clause to filter on aggregated values.

Uploaded by

fakhrulfz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
351 views3 pages

HAVING Clause Practice Problems

This document provides examples of SQL queries using the HAVING clause to filter aggregated results. The queries retrieve sales data from various tables in the AdventureWorks database, filtering for territories with over $10 million in sales, colors associated with at least 20 products, products ordered over 200 times in 2006, and customers with 6 or more orders between 2005-2006. The solutions demonstrate the use of aggregation functions like SUM and COUNT along with the HAVING clause to filter on aggregated values.

Uploaded by

fakhrulfz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

HAVING

 Clause  Practice  Problems  


 
1) Find  the  total  sales  by  territory  for  all  rows  in  the  Sales.SalesOrderHeader  table.    
Return  only  those  territories  that  have  exceeded  $10  million  in  historical  sales.    
Return  the  total  sales  and  the  TerritoryID  column.  
2) Using  the  query  from  the  previous  question,  join  to  the  Sales.SalesTerritory  table  
and  replace  the  TerritoryID  column  with  the  territory’s  name.  
3) Using  the  Production.Product  table,  find  how  many  products  are  associated  with  
each  color.    Ignore  all  rows  where  the  color  has  a  NULL  value.    Once  grouped,  return  
to  the  results  only  those  colors  that  had  at  least  20  products  with  that  color.  
4) Starting  with  the  Sales.SalesOrderHeader  table,  join  to  the  Sales.SalesOrderDetail  
table.    This  table  contains  the  line  item  details  associated  with  each  sale.    From  
Sales.SalesOrderDetail,  join  to  the  Production.Product  table.    Return  the  Name  
column  from  Production.Product  and  assign  it  the  column  alias  “Product  Name”.    
For  each  product,  find  out  how  many  of  each  product  was  ordered  for  all  orders  that  
occurred  in  2006.    Only  output  those  products  where  at  least  200  were  ordered.  
5) Find  the  first  and  last  name  of  each  customer  who  has  placed  at  least  6  orders  
between  July  1,  2005  and  December  31,  2006.    Order  your  results  by  the  number  of  
orders  placed  in  descending  order.    (Hint:  You  will  need  to  join  to  three  tables  –  
Sales.SalesOrderHeader,  Sales.Customer,  and  Person.Person.    You  will  use  every  
clause  to  complete  this  query).  
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
HAVING  Clause  Practice  Problem  Solutions  
 
Question  1:  
SELECT    
  TerritoryID,    
  SUM(TotalDue)  AS  TotalSales  
FROM  Sales.SalesOrderHeader  
GROUP  BY  TerritoryID  
HAVING  SUM(TotalDue)  >  10000000  
 
Question  2:  
SELECT    
  ST.Name  AS  TerritoryName,    
  SUM(TotalDue)  AS  TotalSales  
FROM  Sales.SalesOrderHeader  SOH  
LEFT  OUTER  JOIN  Sales.SalesTerritory  ST  
ON  ST.TerritoryID  =  SOH.TerritoryID  
GROUP  BY  ST.Name  
HAVING  SUM(TotalDue)  >  10000000  
 
Question  3:  
SELECT    
  Color,    
  COUNT(*)  AS  ProductCount  
FROM  Production.Product  
WHERE  Color  IS  NOT  NULL  
GROUP  BY  Color  
HAVING  COUNT(*)  >=  20  
 
Question  4:  
SELECT    
  P.Name  AS  [Product  Name],    
  SUM(SOD.OrderQty)  AS  ProductOrderCount  
FROM  Sales.SalesOrderHeader  SOH  
INNER  JOIN  Sales.SalesOrderDetail  SOD  
ON  SOD.SalesOrderID  =  SOH.SalesOrderID  
INNER  JOIN  Production.Product  P  
ON  P.ProductID  =  SOD.ProductID  
WHERE  SOH.OrderDate  BETWEEN  '1/1/2006'  AND  '12/31/2006'  
GROUP  BY  P.Name  
HAVING  SUM(SOD.OrderQty)  >=  200  
 
Question  5:  
SELECT    
  P.FirstName,    
  P.LastName,    
  COUNT(*)  AS  OrdersPlaced  
FROM  Sales.SalesOrderHeader  SOH  
INNER  JOIN  Sales.Customer  C  
ON  C.CustomerID  =  SOH.CustomerID  
INNER  JOIN  Person.Person  P  
ON  P.BusinessEntityID  =  C.PersonID  
WHERE  OrderDate  BETWEEN  '7/1/2005'  AND  '12/31/2006'  
GROUP  BY  P.FirstName,  P.LastName  
HAVING  COUNT(*)  >=  6  
ORDER  BY  OrdersPlaced  DESC  
 
 

You might also like