0% found this document useful (0 votes)
24 views4 pages

Group by

This document discusses the GROUP BY clause in SQL queries. It provides 4 examples of GROUP BY queries: 1. A query that counts the number of people associated with each person type using the Person.Person table. 2. A query that counts the number of products that are black and silver using the Production.Product table. 3. A query that counts the number of sales for each salesperson between two dates using the Sales.SalesOrderHeader table. 4. A query that counts the number of territories using the Sales.Customer and Sales.SalesTerritory tables.

Uploaded by

Kanishk Chouhan
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)
24 views4 pages

Group by

This document discusses the GROUP BY clause in SQL queries. It provides 4 examples of GROUP BY queries: 1. A query that counts the number of people associated with each person type using the Person.Person table. 2. A query that counts the number of products that are black and silver using the Production.Product table. 3. A query that counts the number of sales for each salesperson between two dates using the Sales.SalesOrderHeader table. 4. A query that counts the number of territories using the Sales.Customer and Sales.SalesTerritory tables.

Uploaded by

Kanishk Chouhan
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/ 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