25.03.2013 Views

Cracking the Coding Interview - Fooo

Cracking the Coding Interview - Fooo

Cracking the Coding Interview - Fooo

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Solutions to Chapter 15 | Databases<br />

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

types are better in certain situations<br />

SOLUTION<br />

CareerCup com<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, and ano<strong>the</strong>r lists <strong>the</strong><br />

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

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

Regular Beverages:<br />

Name Code<br />

Budweiser BUDWEISER<br />

Coca-Cola COCACOLA<br />

Pepsi PEPSI<br />

Calorie-Free Beverages:<br />

Code Name<br />

COCACOLA Diet Coca-Cola<br />

FRESCA Fresca<br />

PEPSI Diet Pepsi<br />

PEPSI Pepsi Light<br />

Water 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, and [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 and 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 />

2 3 2

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

Saved successfully!

Ooh no, something went wrong!