Why do I not get the same results when running the two queries? If I run the second one I get the course with the smallest amount of credits and when I run the first one I get the courses ordered by courseid
select min(credits), title, courseid
from course
group by title, courseid
select min(credits)
from course
An aggregation query is any query that has a group by
or an aggregation function in the select
.
An aggregation query returns one row per group, where a "group" is defined as the unique combination of values of the keys in the group by
clause. If there is no group by
clause, then all rows are taken to be a single group and one row is returned.
So, your first query returns one row for each combination of title
and courseid
in the course
table. That row contains the minimum value of credits
for that combination. If the course
table has only one row per courseid
, then the results are very similar to the contents of the table.
The second query returns one row overall, with the minimum number of credits of all rows.
If you want to get one row from with the minimum number of credits, then you don't want an aggregation query. Instead, you can use:
select c.*
from course c
order by c.credits
limit 1;