LEFT JOIN
LEFT JOIN 키워드는 왼쪽 테이블(table1)의 모든 레코드와 오른쪽 테이블(table2)의 일치하는 레코드를 반환한다.
일치하는 레코드가 없는 경우 결과는 오른쪽에서 0개의 레코드가 생성된다.
⭐ 일부 SQL에서는 LEFT JOIN을 OUTER JOIN 이라고도 한다.
구문
SELECT column_name(s) FROM table1 LEFT JOIN table2 ON table1.column_name = table2.column_name;
데이터베이스 예시
고객 테이블
CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country |
---|---|---|---|---|---|---|
1 | Alfreds Futterkiste | Maria Anders | Obere Str. 57 | Berlin | 12209 | Germany |
2 | Ana Trujillo Emparedados y helados | Ana Trujillo | Avda. de la Constitución 2222 | México D.F. | 05021 | Mexico |
3 | Antonio Moreno Taquería | Antonio Moreno | Mataderos 2312 | México D.F. | 05023 | Mexico |
주문 테이블
OrderID | CustomerID | EmployeeID | OrderDate | ShipperID |
---|---|---|---|---|
10308 | 2 | 7 | 1996-09-18 | 3 |
10309 | 37 | 3 | 1996-09-19 | 1 |
10310 | 77 | 8 | 1996-09-20 | 2 |
모든 고객과 고객이 가질 수 있는 주문을 선택
⭐ LEFT JOIN 키워드는 오른쪽 테이블(주문)에 일치하는 항목이 없는 경우에도 왼쪽 테이블(고객)의 모든 레코드를 반환한다.
SELECT Customers.CustomerName, Orders.OrderID FROM Customers LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID ORDER BY Customers.CustomerName;
기본 예시
CustomerName | OrderID |
---|---|
Alfreds Futterkiste | null |
Ana Trujillo Emparedados y helados | 10308 |
Antonio Moreno Taquería | 10365 |
Around the Horn | 10355 |
Around the Horn | 10383 |
B’s Beverages | 10289 |
Berglunds snabbköp | 10278 |
Berglunds snabbköp | 10280 |
Berglunds snabbköp | 10384 |
Blauer See Delikatessen | null |
Blondel père et fils | 10265 |
Blondel père et fils | 10297 |
Blondel père et fils | 10360 |
Blondel père et fils | 10436 |
Bon app’ | 10331 |
Bon app’ | 10340 |
Bon app’ | 10362 |
Bottom-Dollar Marketse | 10389 |
Bottom-Dollar Marketse | 10410 |
Bottom-Dollar Marketse | 10411 |
Bottom-Dollar Marketse | 10431 |
Bólido Comidas preparadas | 10326 |
Cactus Comidas para llevar | null |
Centro comercial Moctezuma | 10259 |
Chop-suey Chinese | 10254 |
Chop-suey Chinese | 10370 |
Comércio Mineiro | 10290 |
Consolidated Holdings | 10435 |
Die Wandernde Kuh | 10301 |
Die Wandernde Kuh | 10312 |
Die Wandernde Kuh | 10348 |
Die Wandernde Kuh | 10356 |
Drachenblut Delikatessend | 10363 |
Drachenblut Delikatessend | 10391 |
Du monde entier | 10311 |
Eastern Connection | 10364 |
Eastern Connection | 10400 |
Ernst Handel | 10258 |
Ernst Handel | 10263 |
Ernst Handel | 10351 |
Ernst Handel | 10368 |
Ernst Handel | 10382 |
Ernst Handel | 10390 |
Ernst Handel | 10402 |
Ernst Handel | 10403 |
Ernst Handel | 10430 |
Ernst Handel | 10442 |
FISSA Fabrica Inter. Salchichas S.A. | null |
Familia Arquibaldo | 10347 |
Familia Arquibaldo | 10386 |
Familia Arquibaldo | 10414 |
Folies gourmandes | 10408 |
Folk och fä HB | 10264 |
Folk och fä HB | 10327 |
Folk och fä HB | 10378 |
Folk och fä HB | 10434 |
France restauration | null |
Franchi S.p.A. | 10422 |
Frankenversand | 10267 |
Frankenversand | 10337 |
Frankenversand | 10342 |
Frankenversand | 10396 |
Furia Bacalhau e Frutos do Mar | 10328 |
Furia Bacalhau e Frutos do Mar | 10352 |
GROSELLA-Restaurante | 10268 |
Galería del gastrónomo | 10366 |
Galería del gastrónomo | 10426 |
Godos Cocina Típica | 10303 |
Gourmet Lanchonetes | 10423 |
Great Lakes Food Market | null |
HILARIÓN-Abastos | 10257 |
HILARIÓN-Abastos | 10395 |
Hanari Carnes | 10250 |
Hanari Carnes | 10253 |
Hungry Coyote Import Store | 10375 |
Hungry Coyote Import Store | 10394 |
Hungry Coyote Import Store | 10415 |
Hungry Owl All-Night Grocers | 10298 |
Hungry Owl All-Night Grocers | 10309 |
Hungry Owl All-Night Grocers | 10335 |
Hungry Owl All-Night Grocers | 10373 |
Hungry Owl All-Night Grocers | 10380 |
Hungry Owl All-Night Grocers | 10429 |
Island Trading | 10315 |
Island Trading | 10318 |
Island Trading | 10321 |
Königlich Essen | 10323 |
Königlich Essen | 10325 |
LILA-Supermercado | 10283 |
LILA-Supermercado | 10296 |
LILA-Supermercado | 10330 |
LILA-Supermercado | 10357 |
LILA-Supermercado | 10381 |
LINO-Delicateses | 10405 |
La corne d’abondance | null |
La maison d’Asie | 10350 |
La maison d’Asie | 10358 |
La maison d’Asie | 10371 |
La maison d’Asie | 10413 |
La maison d’Asie | 10425 |
Laughing Bacchus Wine Cellars | null |
Lazy K Kountry Store | null |
Lehmanns Marktstand | 10279 |
Lehmanns Marktstand | 10284 |
Lehmanns Marktstand | 10343 |
Let’s Stop N Shop | null |
Lonesome Pine Restaurant | 10307 |
Lonesome Pine Restaurant | 10317 |
Magazzini Alimentari Riuniti | 10275 |
Magazzini Alimentari Riuniti | 10300 |
Magazzini Alimentari Riuniti | 10404 |
Maison Dewey | null |
Morgenstern Gesundkost | 10277 |
Mère Paillarde | 10332 |
Mère Paillarde | 10339 |
Mère Paillarde | 10376 |
Mère Paillarde | 10424 |
Mère Paillarde | 10439 |
North/South | null |
Océano Atlántico Ltda. | 10409 |
Old World Delicatessen | 10260 |
Old World Delicatessen | 10305 |
Old World Delicatessen | 10338 |
Old World Delicatessen | 10441 |
Ottilies Käseladen | 10407 |
Paris spécialités | null |
Pericles Comidas clásicas | 10322 |
Pericles Comidas clásicas | 10354 |
Piccolo und mehr | 10353 |
Piccolo und mehr | 10392 |
Piccolo und mehr | 10427 |
Princesa Isabel Vinhoss | 10336 |
Princesa Isabel Vinhoss | 10397 |
Princesa Isabel Vinhoss | 10433 |
QUICK-Stop | 10273 |
QUICK-Stop | 10285 |
QUICK-Stop | 10286 |
QUICK-Stop | 10313 |
QUICK-Stop | 10345 |
QUICK-Stop | 10361 |
QUICK-Stop | 10418 |
Que Delícia | 10261 |
Que Delícia | 10291 |
Que Delícia | 10379 |
Que Delícia | 10421 |
Queen Cozinha | 10372 |
Queen Cozinha | 10406 |
Rancho grande | null |
Rattlesnake Canyon Grocery | 10262 |
Rattlesnake Canyon Grocery | 10272 |
Rattlesnake Canyon Grocery | 10294 |
Rattlesnake Canyon Grocery | 10314 |
Rattlesnake Canyon Grocery | 10316 |
Rattlesnake Canyon Grocery | 10346 |
Rattlesnake Canyon Grocery | 10401 |
Reggiani Caseifici | 10288 |
Reggiani Caseifici | 10428 |
Reggiani Caseifici | 10443 |
Ricardo Adocicados | 10287 |
Ricardo Adocicados | 10299 |
Richter Supermarkt | 10255 |
Richter Supermarkt | 10419 |
Romero y tomillo | 10281 |
Romero y tomillo | 10282 |
Romero y tomillo | 10306 |
Santé Gourmet | 10387 |
Save-a-lot Markets | 10324 |
Save-a-lot Markets | 10393 |
Save-a-lot Markets | 10398 |
Save-a-lot Markets | 10440 |
Seven Seas Imports | 10359 |
Seven Seas Imports | 10377 |
Seven Seas Imports | 10388 |
Simons bistro | 10341 |
Simons bistro | 10417 |
Split Rail Beer & Ale | 10271 |
Split Rail Beer & Ale | 10329 |
Split Rail Beer & Ale | 10349 |
Split Rail Beer & Ale | 10369 |
Split Rail Beer & Ale | 10385 |
Split Rail Beer & Ale | 10432 |
Spécialités du monde | null |
Suprêmes délices | 10252 |
Suprêmes délices | 10302 |
The Big Cheese | 10310 |
The Cracker Box | null |
Toms Spezialitäten | 10438 |
Tortuga Restaurante | 10276 |
Tortuga Restaurante | 10293 |
Tortuga Restaurante | 10304 |
Tortuga Restaurante | 10319 |
Tradição Hipermercados | 10249 |
Tradição Hipermercados | 10292 |
Trail’s Head Gourmet Provisioners | null |
Vaffeljernet | 10367 |
Vaffeljernet | 10399 |
Victuailles en stock | 10251 |
Victuailles en stock | 10334 |
Vins et alcools Chevalier | 10274 |
Vins et alcools Chevalier | 10295 |
Wartian Herkku | 10266 |
Wartian Herkku | 10270 |
Wartian Herkku | 10320 |
Wartian Herkku | 10333 |
Wartian Herkku | 10412 |
Wartian Herkku | 10416 |
Wartian Herkku | 10437 |
Wellington Importadora | 10256 |
Wellington Importadora | 10420 |
White Clover Markets | 10269 |
White Clover Markets | 10344 |
Wilman Kala | 10248 |
Wolski | 10374 |
참고
W3C School - SQL LEFT JOIN Keyword
W3C School - SQL Tutorial