ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [SQL] data.world tutorial SQL Inline Subquery 문 예제 풀이
    데이터 분석/DB & SQL 2020. 5. 3. 22:06

    Subquery 의 다른 방법 Inline Subquery 

     

    Subquery / nested query / Inner query 내부쿼리

    main query / containing query / / outer query 외부쿼리 

     

    In a previous section we built subqueries using the keyword WITHAnother kind of subquery is an inline subquery.

    이전 포스트에서는 WITH 문을 통해 subquery 를 작성하는 법을 알아보았다. Subquery 의 다른 방법으로 Inline Subquery 가 있다. 

     

    In a standard inline subquery, the subquery is a component of a FROM clause, taking the place of a table name.

    Inline Subquery 에서 FROM 절의 구성요소로, 테이블 이름을 대신핸다. 

     

    A caveat of this subquery is that it must be uncorrelated, i.e., not referencing any fields from the containing query.

    주의해아할 점은 subquery 가 비상관성을 유지해야한다. (독립성을 유지해야한다.) 예를 들어 이 subquery 를 담고있는 containing query (외부쿼리) 의 어느 필드도 참고하고 있지 않아야 한다. 

     


    WITH 문을 활용한 Subquery

    WITH big_accounts AS (
    SELECT account 
    FROM accounts
    WHERE revenue > 10000
    ),

    total_pipeline AS (
    SELECT teams.manager, teams.sales_agent, pipeline.account, pipeline.deal_stage, pipeline.close_value 
    FROM sales_pipeline pipeline
    JOIN sales_teams teams ON pipeline.sales_agent = teams.sales_agent
    JOIN big_accounts big_accounts ON pipeline.account = big_accounts.account
    )

    SELECT manager, account, COUNT(close_value) 
    FROM total_pipeline
    WHERE deal_stage = "Won"
    GROUP BY manager, account
    ORDER BY COUNT(close_value) DESC

     

    Inline Subquery 

    SELECT manager, account, COUNT(close_value) 
    FROM (
            SELECT teams.manager, teams.sales_agent, pipeline.account, pipeline.deal_stage, pipeline.close_value 
            FROM sales_pipeline pipeline
            JOIN sales_teams teams ON pipeline.sales_agent = teams.sales_agent
            WHERE pipeline.account IN (
                    SELECT account 
                    FROM accounts
                   WHERE revenue > 10000
            )
    ) AS total_pipeline

    WHERE deal_stage = "Won"
    GROUP BY manager, account
    ORDER BY COUNT(close_value) DESC

     


    Tip. While inline subqueries of this type are supported, it’s always better form to use WITH than a subquery in a FROM clause.

    FROM 절을 대신하는 Subquery 는 Inline suvquery 방식을 지원하기는 하지만, WITH 문을 사용하는 것이 항상 더 좋은 방식이다. 

     

    Tip. 상관쿼리와 비상관쿼리 

     

    uncorrelated query 비상관 쿼리 

    만약 서브쿼리가 외부 쿼리의 어떤 것도 참조하지 않고 단독으로 사용되면 비상관쿼리!

    내부 쿼리가 우선 실행되고, 그리고 나서 결과가 외부쿼리에서 활용됩니다.

    FROM: 특정 데이터셋을 추가해서 가져올 때 활용
    WHERE/HAVING: 특정 데이터의 통계값을 기준으로 조건부로 가져올 때 활용

     

     correlated query 상관 쿼리

    서브쿼리가 외부 쿼리의 특정값을 참조해서 사용되면 상관쿼리!

    외부 쿼리가 row 한 개씩 실행 될 때, 그 때마다 서브쿼리가 동작한다. 일반적으로는 한 개의 결과값만 허용한다. (WHRER 조건부 IN 의 대상으로 subquery 를 사용하지 않는 한)

    SELECT: 특정 행에 대해서 다른 테이블에 있는 연관된 컬럼을 가져올 수도 있고,
    WHERE: 조건으로 포함여부를 결정할 수도 있다.  

     

    https://haloaround.tistory.com/209

     

    서브쿼리 Sub Query - 상관쿼리와 비상관쿼리

    서브쿼리에서 어려운 점은 구조가 아니고, 쿼리의 어느 부분이 서브 쿼리인지 또는 서브 쿼리를 사용할 필요가 있는지를 알아내는 것입니다. Q. 내 리스트에 있는 사람들 중에 누가 연봉이 가장

    haloaround.tistory.com

     

    댓글

Designed by Tistory.