patternsqlMinor
In sqlite, how are return values chosen for columns that are not used for aggregation or grouping?
Viewed 0 times
columnsgroupingarereturnusedsqlitechosenthatforhow
Problem
Given an SQLite database with three columns (char, char, int), the following query
will return the average values of
What will the following query return?
I came across this case while playing with Codeacademy's SQL tutorial. This particular query returns a value of
select column2, avg(column3) from table_name group by column2;will return the average values of
column3 aggregate for each distinct value of column2. What will the following query return?
select column1, column2, avg(column3) from table_name group by column2;I came across this case while playing with Codeacademy's SQL tutorial. This particular query returns a value of
column1 for each distinct value of column2, but I can't figure out how these values were chosen.Solution
The database will return the value from some arbitrary row in the group.
(In the current implementation, it's the value from that last row in the group that was processed, but due to indexes or other optimizations, the processing order might not be predictable.)
Since SQLite 3.7.11, using MIN() or MAX() guarantees that values from a row with the minimum/maximum value are returned.
(In the current implementation, it's the value from that last row in the group that was processed, but due to indexes or other optimizations, the processing order might not be predictable.)
Since SQLite 3.7.11, using MIN() or MAX() guarantees that values from a row with the minimum/maximum value are returned.
Context
StackExchange Database Administrators Q#150533, answer score: 2
Revisions (0)
No revisions yet.