Reputation: 8606
I have a query
SELECT bk_publisher, bk_price FROM books
GROUP BY bk_price, bk_publisher
and
SELECT bk_publisher ,bk_price FROM books
both are returning the same results. Means i have 12 records in my table and both queries returning the 12 records. What is the difference ? Although i am using group by, which is use with aggregate functions. But i want to know is group by making any difference here ?
Upvotes: 0
Views: 69
Reputation: 28741
GROUP BY clause is apparently showing no effect because there is no repeating combination of bk_price, bk_publisher
values.
Upvotes: 0
Reputation: 116048
When you use GROUP BY
, it will squeeze multiple rows having identical columns listed in GROUP BY
as single row in output.
It also means that in general, all other columns mentioned in SELECT
list must be wrapped in aggregate functions like sum()
, avg()
, count()
, etc.
Some SQL engines like MySQL permit not using aggregates, but many people consider this a bug.
Upvotes: 0
Reputation: 4331
The GROUP BY statement is used to group the result-set by one or more columns.Group by is used when you have repeating data and you want single record for each entry.
Upvotes: 0
Reputation: 19284
SQL group by helps you group different results by some identical value (using aggregation functions on other values)
In your case it doesn't mean anything, but when you want to aggregate values based on identical field, you use group by.
For example, if you want to get the max price of a publisher:
SELECT bk_publisher, max(bk_price) FROM books
GROUP BY bk_publisher
Upvotes: 0
Reputation: 125620
SELECT bk_publisher, bk_price FROM books
GROUP BY bk_price, bk_publisher
Will result distinct pairs of (publisher, price), even if your table contains duplicated data.
Upvotes: 2