27.10.2014 Views

Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions

Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions

Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Solutions</strong> to Chapter 15 | Databases<br />

15.2 What are <strong>the</strong> different types of joins? Please explain how <strong>the</strong>y differ <strong>and</strong> why certain<br />

types are better in certain situations.<br />

SOLUTION<br />

pg 80<br />

JOIN is used to combine <strong>the</strong> results of two tables. To perform a join, each of <strong>the</strong> tables must<br />

have at least one field which will be used to find matching records from <strong>the</strong> o<strong>the</strong>r table. The<br />

join type defines which records will go into <strong>the</strong> result set.<br />

Let’s take for example two tables: one table lists “regular” beverages, <strong>and</strong> ano<strong>the</strong>r lists <strong>the</strong><br />

calorie-free beverages. Each table has two fields: <strong>the</strong> beverage name <strong>and</strong> its product code.<br />

The “code” field will be used to perform <strong>the</strong> record matching.<br />

Regular Beverages:<br />

Name<br />

Budweiser<br />

Coca-Cola<br />

Pepsi<br />

Code<br />

BUDWEISER<br />

COCACOLA<br />

PEPSI<br />

Calorie-Free Beverages:<br />

Code<br />

COCACOLA<br />

FRESCA<br />

PEPSI<br />

PEPSI<br />

Water<br />

Name<br />

Diet Coca-Cola<br />

Fresca<br />

Diet Pepsi<br />

Pepsi Light<br />

Purified Water<br />

Let’s join this table by <strong>the</strong> code field. Whereas <strong>the</strong> order of <strong>the</strong> joined tables makes sense in<br />

some cases, we will consider <strong>the</strong> following statement:<br />

[Beverage] JOIN [Calorie-Free Beverage]<br />

i.e. [Beverage] is from <strong>the</strong> left of <strong>the</strong> join operator, <strong>and</strong> [Calorie-Free Beverage] is from <strong>the</strong><br />

right.<br />

1. INNER JOIN: Result set will contain only those data where <strong>the</strong> criteria match. In our example<br />

we will get 3 records: 1 with COCACOLA <strong>and</strong> 2 with PEPSI codes.<br />

2. OUTER JOIN: OUTER JOIN will always contain <strong>the</strong> results of INNER JOIN, however it can<br />

contain some records that have no matching record in o<strong>the</strong>r table. OUTER JOINs are divided<br />

to following subtypes:<br />

CareerCup.com<br />

2 3 2

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!