20.07.2013 Views

Beginning SQL

Beginning SQL

Beginning SQL

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Executing the preceding <strong>SQL</strong> creates the following results set:<br />

SalesPersonId FirstName LastName MemberId FirstName LastName Repeat Orders<br />

1 Sandra Hugson 4 Steve Gee 3<br />

1 Sandra Hugson 6 Jenny Jones 3<br />

1 Sandra Hugson 7 John Jackson 1<br />

1 Sandra Hugson 9 Seymour Botts 6<br />

2 Frasier Crane 4 Steve Gee 3<br />

2 Frasier Crane 7 John Jackson 3<br />

2 Frasier Crane 10 Susie Simons 1<br />

As you would expect, these results are identical to the previous <strong>SQL</strong>’s results. So which query is best?<br />

First, it depends on which query you find easiest to use. Programmer time is often more expensive than<br />

computer time, so if one way of writing a query is easier for you, then stick with it unless there’s a good<br />

reason to do it another way.<br />

The other side of things is efficiency — which query runs faster? In the two different ways shown, the<br />

first is slightly more efficient. It has one less join than the second way, because the second way has to<br />

have an INNER JOIN of the Orders table to itself. The first way gets the same results but filters out<br />

nonduplicate orders using a HAVING clause.<br />

You may even be able to think of a third or fourth (or fifth or sixth) way of writing the same query, one<br />

that might be even faster. In the real world, however, most programmers don’t have the time to write the<br />

same code in five different ways in a bid to save fractions of a second. Unless, of course, efficiency is<br />

absolutely paramount — for example, where there are millions and millions of records and your initial<br />

<strong>SQL</strong> runs with all the speed of a tortoise through molasses.<br />

To help you speed up your queries, the next section covers some tips on writing efficient queries.<br />

Top Tips for Efficient Queries<br />

❑ When using AND, put the condition least likely to be true first. The database system evaluates<br />

conditions from left to right, subject to operator precedence. If you have two or more AND operators<br />

in a condition, the one to the left is evaluated first, and if and only if it’s true is the next condition<br />

evaluated. Finally, if that condition is true, then the third condition is evaluated. You can<br />

save the database system work, and hence increase speed, by putting the least likely condition<br />

first. For example, if you were looking for all members living in New State and born before<br />

January 1, 1940, you could write the following query:<br />

SELECT FirstName, LastName<br />

FROM MemberDetails<br />

WHERE State = ‘New State’ AND DateOfBirth < ‘1940-01-01’;<br />

Advanced Queries<br />

283

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

Saved successfully!

Ooh no, something went wrong!