๐Ÿคนโ€โ™€๏ธ ์ฃผ๋ฌธ ์„ ํƒ [SQL]

3120 ๋‹จ์–ด beginnersdatabasemysqlsql
  • WHERE์—์„œ SUM()์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋Š” ์ด์œ ๋Š” ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?
  • WHERE ๋˜๋Š” GROUP BY์—์„œ AS๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋Š” ์ด์œ ๋Š” ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?

  • ๐Ÿ˜Ž ์™œ ๊ทธ๋Ÿฐ์ง€ ์ด์œ ๋ฅผ ์•Œ๊ฒŒ ๋˜์‹ค๊ฑฐ์—์š”

    ๐Ÿ’Ž ์ฃผ๋ฌธ ์„ ํƒ



    ์ด SQL์„ ๋ณด๋ฉด ์ฃผ๋ฌธ์— ๋Œ€ํ•ด ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹ญ๋‹ˆ๊นŒ?

    SELECT * FROM clients 
    WHERE    age >= 30
    GROUP BY usa
    HAVING   salary > 1000
    ORDER BY id desc
    LIMIT    100
    


    SQL์ด SELECT์—์„œ ์‹คํ–‰๋˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ž…๋‹ˆ๊นŒ?
    ์‚ฌ์‹ค์€ ์‚ฌ์‹ค์ด ์•„๋‹ˆ๋‹ค

    -- SQL executes in this order
    FROM
    |
    WHERE
    |
    GROUP BY 
    |
    HAVING
    |
    SELECT
    |
    ORDER BY
    |
    LIMIT
    


    ๐Ÿ’Ž ์งˆ๋ฌธ 1



    SUM() ํ•จ์ˆ˜๋ฅผ WHERE์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋Š” ์ด์œ ๋Š” ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?




    FROM
    |
    WHERE ๐Ÿ‘ˆ ๐Ÿ‘ˆ
    |
    GROUP BY ๐Ÿ‘ˆ ๐Ÿ‘ˆ
    |
    HAVING
    |
    SELECT
    |
    ORDER BY
    |
    LIMIT
    


    WHERE๊ฐ€ ์‹คํ–‰๋˜๊ธฐ ์ „์— GROUP BY๊ฐ€ ์•„์ง ์‹คํ–‰๋˜์ง€ ์•Š์•˜์œผ๋ฏ€๋กœ WHERE์—์„œ COUNT() SUM()์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

    ์ฆ‰, ์ด๋Ÿฌํ•œ ๊ธฐ๋Šฅ์„ HAVING์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    ๐Ÿ’Ž ์งˆ๋ฌธ 2



    WHERE ๋˜๋Š” GROUP BY์—์„œ AS๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋Š” ์ด์œ ๋Š” ๋ฌด์—‡์ž…๋‹ˆ๊นŒ? ORDER BY์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ๋ถˆ๊ณตํ‰ํ•ฉ๋‹ˆ๋‹ค!!




    FROM
    |
    WHERE ๐Ÿ‘ˆ ๐Ÿ‘ˆ
    |
    GROUP BY ๐Ÿ‘ˆ ๐Ÿ‘ˆ
    |
    HAVING
    |
    SELECT ๐Ÿ‘ˆ ๐Ÿ‘ˆ
    |
    ORDER BY ๐Ÿ‘€ ๐Ÿ‘€ 
    |
    LIMIT
    


    ์ง„์ •ํ•ด. ORDER BY๋Š” SELECT ์ดํ›„์— ์‹คํ–‰๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.
    ์ •์˜ํ•˜๊ธฐ ์ „์—๋Š” "AS"๋กœ ์ƒ์„ฑ๋œ ์ด๋ฆ„์„ ์ฝ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

    ์ฝ์–ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค๐Ÿค—

    ์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ