I have a simple SQLite table called "message":
sequence INTEGER PRIMARY KEY
type TEXT
content TEXT
I want to get the content of the last message of each type (as determined by its sequence). To my surprise, the following simple query works:
SELECT MAX(sequence), type, content
FROM message
GROUP BY type
Surprise, because I know that MSSQL or Postgres would refuse to include a column in the SELECT list that is not part of the GROUP BY clause or an aggregate function and I d have to do a join, like this:
SELECT m.sequence, m.type, m.content
FROM
(
SELECT MAX(sequence) as sequence, type
FROM message
GROUP BY type
) g
JOIN message m
ON g.sequence = m.message_sequence
My question is: is it safe to use the first, much simpler, form of the query in SQLite? It intuitively makes sense that it selects the "content" value that matches the "MAX(sequence)" value, but the documentation doesn t seem to talk about this at all. Of course, if sequence was not unique then the result would be undefined. But if sequence is unique, as in my case, is this guaranteed or is it simply a lucky implementation detail that s subject to change?