I don't understand the way Oracle do the sorting of a string with MIN/MAX. I guess, that small letter(a, b, c) should be returned before the big one (A, B, C). That's why my expectation is to get as MIN - "bob" - small b is "smaller" than big "J". With MAX opposite of it.
I'm getting in Postgres and Sql Server correct result. Is it caused due to some collation/language/character set differences?
DROP TABLE test;
CREATE TABLE test (a INT, name VARCHAR2(10));
INSERT INTO test(a, name)
VALUES (1, 'John')
, (2, 'bob')
;
COMMIT;
SELECT MIN(name) -- return John instead of bob
, MAX(name) -- return bob instead of John
FROM test;
My Oracle 23c XE configuration:
SELECT *
FROM V$NLS_PARAMETERS;
If you do:
Then the output is:
And you can see than the ASCII code for
J
is 74 andb
is 98 so comparing the underlying byte-values then 74 < 98 soJ
<b
.If you have:
Then the output is:
and you are comparing the values as binary.
If you change to a linguistic comparison and set the sort to be case-insensitive:
Then the output is:
And
b
<J
.If you do not want to change the default NLS settings then you can explicitly specify the sort order in the query:
Which outputs:
fiddle