Monday, May 10, 2021

SQL Interview - Puzzle

 Source:

Expected Output:



Query:



Script:

with sample as (
select 1 col1,'a' col2 from dual
union
select 2 col1,'b' col2 from dual
union
select 3 col1,'c' col2 from dual
)
select listagg(t.col2) within group (order by col2) magical_output from sample t
,table (cast (multiset (select level from dual connect by level<=t.col1) as sys.odciNumberList )) lines
group by col2;


1 comment:

  1. with t as (select 1 col1,'a' col2 from dual
    union all
    select 2 col1,'b' col2 from dual
    union all
    select 3 col1,'c' col2 from dual
    union all
    select 4 col1,'d' col2 from dual )
    select listagg(a.col2) within group(order by a.col1) col from t a,t b
    where a.col1>=b.col1
    group by a.col1
    order by a.col1;

    ReplyDelete