SQL Aggregation - GROUP BY City HAVING...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Today's Code - Aggregation Mote’s Notes 3/2/2011 -- Total number of employees SELECT COUNT(EID) AS Employees FROM tblEmployee -- Number of male and female employees SELECT Gender, COUNT(EID) AS Employees FROM tblEmployee GROUP BY Gender -- Number of employees and genders for each job SELECT JobTitle, COUNT(EID) AS Employees, COUNT(Distinct Gender) AS Genders FROM tblEmployee GROUP BY JobTitle -- Number of employees and average salaries in each city/state SELECT City, State, COUNT(EID) AS Employees, AVG(AnnualSalary) AS Average FROM tblEmployee GROUP BY City, State -- Number of employees and average salaries in each Texas city SELECT City, COUNT(EID) AS Employees, AVG(AnnualSalary) AS Average FROM tblEmployee WHERE State='Tx' GROUP BY City -- Texas cities with at least 5 employees SELECT City FROM tblEmployee WHERE State='Tx'
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: GROUP BY City HAVING COUNT(EID)>=5 -- Three Texas cities with the most employees earning over 35K SELECT TOP 3 WITH TIES City FROM tblEmployee WHERE State='Tx' AND AnnualSalary>=35000 GROUP BY City ORDER BY COUNT(EID) DESC -- Texas city with the lowest average salary SELECT TOP 1 WITH TIES City FROM tblEmployee WHERE State='Tx' GROUP BY City ORDER BY AVG(AnnualSalary) -- DOBs with multiple employees SELECT DOB FROM tblEmployee GROUP BY DOB HAVING COUNT(EID)>=2 ORDER BY DOB -- Months and days with multiple birthdays SELECT Month(DOB) AS Month, Day(DOB) AS Day FROM tblEmployee GROUP BY Month(DOB), Day(DOB) HAVING COUNT(EID)>=2 ORDER BY Month(DOB), Day(DOB) -- Month and day with the most birthdays SELECT TOP 1 WITH TIES Month(DOB) AS Month, Day(DOB) AS Day FROM tblEmployee GROUP BY Month(DOB), Day(DOB) ORDER BY COUNT(EID) DESC...
View Full Document

Page1 / 2

SQL Aggregation - GROUP BY City HAVING...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online