Id | LastName | FirstName | Age | City |
1 | Smith | John | 33 | Newyork |
2 | Smith | Mike | 34 | Boston |
3 | Paul | Ryan | 39 | Chicago |
4 | William | Brian | 45 | Baltimore |
5 | Rao | Sunita | 41 | Dayton |
6 | Zing | Sue | 33 | Owings Mills |
7 | Smith | Robert | 45 | Newyork |
8 | Paul | Zean | 33 | Chicago |
9 | Srivastava | Hemant | 33 | Baltimore |
10 | Rao | Venkat | 45 | Dayton |
and we need to find out the frequency of the each age.
SELECT Age, COUNT(Age)AS Frequency
FROM Persons
GROUP BY Age
ORDER BY
COUNT(Age) DESC
We get following result:
Age
|
Frequency
|
33
|
4
|
45
|
3
|
34
|
1
|
39
|
1
|
Now suppose we want to get the ‘Age’ which occurs more than equal to 2. For this query, we need to add HAVING clause after GROUP BY clause.
That gives result as:
SELECT Age, COUNT(Age)AS Frequency
FROM Persons
GROUP BY Age
HAVING COUNT(Age) >= 2
That gives result as:
Age
|
Frequency
|
33
|
4
|
45
|
3
|
No comments:
Post a Comment