Logic of "group by
If you use any aggregate function, and also wants to display another column along size it, you must use "GROUP by"
select char_length(city) as citynamesize,city from station group by city order by citynamesize DESC;
SELECT count(CustomerID), CustomerName,City FROM Customers where CustomerName Like '%Ho%' Group by City ;
If you run below querry, it will give 7 in count column, because it's illogical, you can find the people/row, whos names contains "Ho", but against what, you cant display 7|Hosa ?
SELECT count(CustomerID), CustomerName FROM Customers where CustomerName Like '%Ho%';
So that's why it should be more meaningful, like display the names and count of people, whos name contains "Ho" per city,
So against each city, you will find count of people. whos name starts with "Ho" but again displaying names doesn't make sense, if you sane two person in Dlehi, with name "Hosa,Hola", what you will display in name coulum ?
| count(CustomerID) | CustomerName | City |
|---|---|---|
| 1 | Chop-suey Chinese | Bern |
| 1 | Rancho grande | Buenos Aires |
| 1 | Gourmet Lanchonetes | Campinas |
| 1 | Princesa Isabel Vinhoss | Lisboa |
| 2 | Consolidated Holdings | London |
| 1 | Let's Stop N Shop | San Francisco |
Comments
Post a Comment