Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

SQL ZERO TO HERO

DAY-11

IMPORTANT GROUP BY
SQL QUERIES

Created by:
Devikrishna R

Youtube - Vision Board: https://youtu.be/ibDBm_ZPZv8

LinkedIn - https://www.linkedin.com/in/devikrishna-r-
%F0%9F%87%AE%F0%9F%87%B3-92085678/
GROUP BY

--1. In the Person.Person table how many people are associated with each
person type?
--2. Using one Query, Find out how many products in Production.products are the
color ‘Silver’ and How many are as ‘Black’
--3. Using Sales.SalesOrderHeader how many sales occurred in each
salesPersonID between May 1 ,2011 and Deceber 31,2012. Order by the results by
the sale count in descending order.
--4. Select the count of TerritoryID based on the two tables Sales.Customer and
Sales.SalesTerritory group by based on the TerritoryID in high to low
--Answers

--1. In the Person.Person table how many people are associated with each
person type?

Select * from Person.Person --19972 records

Select distinct PersonType from Person.Person --6 category

Select Person.PersonType,count(*) as Personcount from Person.Person group by


Person.PersonType

--1. In the Person.Person table how many people are associated with each
person type and order by their count desc?

Select Person.PersonType,count(*) as Personcount from Person.Person group by


Person.PersonType order by Personcount desc

--2. Using one Query, Find out how many products in Production.products are the
color ‘Silver’ and How many are as ‘Black’

Select * from Production.Product --504 records

Select distinct Color from Production.Product -- 10 colors (NULL also include)

Select color as 'productcolor', count(*) as 'productcount' from Production.Product


group by Color

Select color as 'productcolor', count(*) as 'productcount' from Production.Product


where color in ('Black','Silver') group by Color

---ORDER

--Where --->group By -->order by

--3. Using Sales.SalesOrderHeader how many sales occurred in each


salesPersonID between May 1 ,2011 and Deceber 31,2012. Order by the results by
the sale count in descending order.

Select * from Sales.SalesOrderHeader

Select SalesPersonID,count(*) as 'SalesPersonIDcount' from


sales.SalesOrderHeader where OrderDate between '5/1/2011' and '12/31/2012'
group by SalesPersonID order by 'SalesPersonIDcount' desc

--4. Select the count of TerritoryID based on the two tables Sales.Customer and
Sales.SalesTerritory group by based on the TerritoryID in high to low

Select * from Sales.Customer


Select * from Sales.SalesTerritory

SELECT sc.TerritoryID, COUNT(sc.TerritoryID) AS NumberOfTerritoryID FROM


Sales.Customer as sc
LEFT JOIN Sales.SalesTerritory as ss ON sc.TerritoryID = ss.TerritoryID
GROUP BY sc.TerritoryID order by TerritoryID desc;

SELECT sc.TerritoryID, COUNT(sc.TerritoryID) AS NumberOfTerritoryID FROM


Sales.Customer as sc
LEFT JOIN Sales.SalesTerritory as ss ON sc.TerritoryID = ss.TerritoryID
GROUP BY sc.TerritoryID order by NumberOfTerritoryID desc;

You might also like