SQL 기초 실무/SQL 알고리즘 문제

23.09.29. SQL Developer 알고리즘 문제 ②

잇꼬 2023. 10. 4. 10:54
728x90
반응형
SMALL

2. 구구단 1단 ~ 9단 출력

-- 예제112. 구구단 출력. 
select level as num
    from dual
    connect by level <= 9 ;

select level+1 as num 
    from dual
    connect by level <= 9 ;
    
with loop_table as ( select level as num
                                from dual
                                connect by level <= 9 ) ,
        gugu_table as ( select level+1 as gugu
                                    from dual
                                    connect by level <= 8 )
select to_char(a.num) || ' X ' || to_char(b.gugu) || ' = ' || to_char(b.gugu * a. num) as 구구단
    from loop_table a, gugu_table b ;

-- 문제1. 위의 결과에서 2, 5, 7단만 출력하세요. 
with loop_table as ( select level as num
                                from dual
                                connect by level <= 9 ) ,
        gugu_table as ( select level+1 as gugu
                                    from dual
                                    connect by level <= 8 )
select to_char(a.num) || ' X ' || to_char(b.gugu) || ' = ' || to_char(b.gugu * a. num) as 구구단
    from loop_table a, gugu_table b 
    where a.num in (2, 5, 7) ;

-- 문제2. 구구단 전체에서 짝수 단만 출력하세요.
with loop_table as ( select level as num
                                from dual
                                connect by level <= 9 ) ,
        gugu_table as ( select level+1 as gugu
                                    from dual
                                    connect by level <= 8 )
select to_char(a.num) || ' X ' || to_char(b.gugu) || ' = ' || to_char(b.gugu * a. num) as 구구단
    from loop_table a, gugu_table b 
    where mod(a.num, 2) = 0 ;
728x90
반응형
LIST