๊ด€๋ฆฌ ๋ฉ”๋‰ด

Coding Planet

leetcode > 183. Customers Who Never Order ๋ณธ๋ฌธ

SQL

leetcode > 183. Customers Who Never Order

jhj.sharon 2023. 2. 5. 23:37
๋ฐ˜์‘ํ˜•

โœจ ๋ฌธ์ œ

 

๐Ÿ’ป์ฝ”๋“œ

 

๐Ÿ‘ฉ‍๐Ÿ’ป ํ’€์ด ๋ฐฉ๋ฒ•

  • ๋น„๊ต์  ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ์ž„์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ํ‹€๋ ธ์—ˆ๋‹ค. ์ปฌ๋Ÿผ ์ด๋ฆ„๋งŒ ๋ณด๊ณ  id๋ฅผ ๊ธฐ์ค€์œผ๋กœ inner join์„ ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค
  • ํ‘œ์— ๋Œ€ํ•œ ์ƒ์„ธ์„ค๋ช…์„ ๋‹ค์‹œ ํ™•์ธํ•˜๋‹ˆ Orders ํ…Œ์ด๋ธ”์˜ customerID๊ฐ€ Customers ํ…Œ์ด๋ธ”์˜ ID๋ผ๋Š” ๊ฒƒ์„ ๋ฐœ๊ฒฌํ–ˆ๋‹ค.
  • WHERE ์กฐ๊ฑด์ ˆ์— ์„œ๋ธŒ์ฟผ๋ฆฌ๋กœ ORDERS ํ…Œ์ด๋ธ”์—์„œ CustomerId๋ฅผ ๋ฝ‘์•„๋‚ด๊ณ  NOT IN์„ ์ด์šฉํ–ˆ๋‹ค.

๐Ÿ”” ๋Š๋‚€์ 

  • ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์˜ solutiond์„ ๋ณด๋‹ˆ left join์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ‘ผ ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์•˜๋‹ค. ์•„์ง left join, right join์ด ์ต์ˆ™ํ•˜์ง€ ์•Š์€๋ฐ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ์“ธ ์ˆ˜ ์žˆ๋„๋ก ๊ณต๋ถ€ํ•ด์•ผ๊ฒ ๋‹ค.

 

SELECT c.name AS Customers 
FROM 
(customers AS c LEFT JOIN orders AS o ON c.id = o.customerId) 
WHERE o.id IS NULL;

 

๋ฐ˜์‘ํ˜•
Comments