'How is a binary/bytes column sorted in sql?

The following query sorts a binary column in BigQuery:

with tbl as (
  select B'123' as col union all select B'234'
) select * from tbl order by col;
----------------------------------
Row col f0_
 1  MTIz false 
 2  MjM0 false 

Is there a convention for how a Binary column is sorted? The above example is tested against BigQuery.



Solution 1:[1]

The BINARY type is similar to CHAR, except that it stores binary strings rather than nonbinary strings. That is, it stores byte strings rather than character strings.

This means they have the binary character set and collation, and comparison and sorting are based on the numeric values of the bytes in the values.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sakshi Gatyan