-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathAggregate Function.sql
More file actions
28 lines (22 loc) · 957 Bytes
/
Copy pathAggregate Function.sql
File metadata and controls
28 lines (22 loc) · 957 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
--Age Stats Since Opening
SELECT MAX(Age) Oldest,MIN(Age) Youngest,AVG(Age) AverageAge FROM Person
--Return the Oldest and Youngest Person
SELECT PersonName, Age FROM Person
WHERE Age = (SELECT MAX(Age) FROM Person)
SELECT PersonName,Age FROM Person -- Youngest Person
WHERE Age = (SELECT MIN(Age) FROM Person)
--Yearly Age Statistics
SELECT Year(EntryDate) EntryDate,MAX(Age) Oldest,MIN(Age) Youngest,AVG(Age) AverageAge FROM Person
GROUP BY Year(EntryDate)
--Return how many people under 25
SELECT COUNT(*) Persons FROM Person
WHERE Age < 25
--Return how many people entered every year in the second half of the year
SELECT Year(EntryDate) EntryDate, COUNT(*) Persons FROM Person
WHERE MONTH(EntryDate) > 6
GROUP BY Year(EntryDate)
--Count Persons by their initials Appearing more than once
SELECT LEFT(PersonName,1) Initials,COUNT(*) Total FROM Person
GROUP BY LEFT(PersonName,1)
HAVING COUNT(*) > 1
ORDER BY Initials