Study/SQL

[SQL] SQL로 알고리즘 문제 풀기 - 구구단

truthyun 2023. 1. 25. 13:59
728x90
반응형

1. 구구단 2단

WITH LOOP_TABLE AS (SELECT LEVEL AS NUM
                    FROM DUAL
                    CONNECT BY LEVEL <= 9)

SELECT '2' || ' X ' || NUM || ' = ' || 2 * NUM AS "2단"
FROM LOOP_TABLE;

  • CONNECT BY절에는 LEVEL의 출력 조건을 입력할 수 있다.

 

반응형
728x90

 

2. 구구단(1단 ~ 9단)

-- 1단 ~ 9단
WITH LOOP_TABLE AS (SELECT LEVEL AS NUM
                    FROM DUAL
                    CONNECT BY LEVEL <= 9)

SELECT TO_CHAR(A.NUM) || ' X ' || TO_CHAR(B.NUM) 
                      || ' = ' || TO_CHAR(A.NUM * B.NUM) AS 구구단
FROM LOOP_TABLE A, LOOP_TABLE B;

-- 2단 ~ 9단
WITH LOOP_TABLE AS (SELECT LEVEL + 1 AS NUM
                    FROM DUAL
                    CONNECT BY LEVEL <= 8),
     GUGUDAN_TABLE AS (SELECT LEVEL AS GUGU
                       FROM DUAL
                       CONNECT BY LEVEL <= 9)

SELECT TO_CHAR(A.NUM) || ' X ' || TO_CHAR(B.GUGU) 
                      || ' = ' || TO_CHAR(A.NUM * B.GUGU) AS 구구단
FROM LOOP_TABLE A, GUGUDAN_TABLE B;

728x90
반응형