Sample Header Ad - 728x90

Ignoring trailing spaces when grouping by varchar type in Redshift

2 votes
1 answer
99 views
I created a table in Redshift and entered data as below.
create table public.temp (
  id int,
  name varchar(20)
);

insert into public.temp (id, name) values (1, 'bob'), (2, 'bob  ');
And when counting based on the name column, it returns ('bob', 2).
select name, count(1) from public.temp group by name;

Result: ('bob', 2);
When grouping by varchar type in redshift it seems to ignore trailing spaces. And we ran another test.
select t, count(1)
from (
  select 'a' as t
  union all 
  select 'a  ' as t
) as a
group by t;
Surprisingly, it returns the result including the trailing spaces.
('a', 1), ('a  ', 1)
What is the difference between the two query execution methods and why do I get these results?
Asked by 임승현Seunghyun Lim (21 rep)
Jun 23, 2025, 02:08 AM
Last activity: Jun 23, 2025, 08:05 AM