| Oracle FAQ | Your Portal to the Oracle Knowledge Grid | |
Home -> Community -> Usenet -> comp.databases.theory -> Re: Why using "Group By"
>> This is a theoretical question. Why do I need to add the "Group By"
in the following SQL:
SELECT customer_city, COUNT(*)
FROM Customers;
Isn't it clear that I want to get the number of rows per city, so why is it necessary to add: "GROUP BY customer_city" <
To keep syntax consistent. You can also write;
SELECT COUNT(*) FROM Customers;
If you only need the aggreagates (I have some examples of that trick in SQL FOR SMARTIES and you can look at an UPDATE problem posted by a guy names Myron on the SQL Server newsgroup).
Or you can write nested code that plays with levesl of aggregation.
SELECT split, COUNT(*)
FROM (SELECT MOD(customer_age, 2) FROM Customers) AS X(split)
GROUP BY split;
Here is how a SELECT works in SQL ... at least in theory. Real products will optimize things when they can.
If there is a SELECT DISTINCT, then redundant duplicate rows are removed. For purposes of defining a duplicate row, NULLs are treated as matching (just like in the GROUP BY).
f) Nested query expressions follow the usual scoping rules you would expect from a block structured language like C, Pascal, Algol, etc. Namely, the innermost queries can reference columns and tables in the queries in which they are contained. Received on Mon Mar 17 2003 - 18:28:55 CST
![]() |
![]() |