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.

9<br />

Advanced Queries<br />

In database programming, you’ll find that roughly 95 percent of queries are fairly straightforward<br />

and are just a matter of working out what columns are required and including a simple WHERE<br />

clause to filter out the unwanted results. This chapter is all about how to tackle the other 5 percent,<br />

which are difficult and complex queries. This chapter also presents a number of questions and<br />

examines how to write the <strong>SQL</strong> to answer them. Specifically, this chapter covers the following:<br />

❑ Tackling complex queries<br />

❑ Formulating precise SELECT column lists and FROM clauses<br />

❑ Writing ruthlessly efficient queries<br />

Before getting into the specifics of the chapter, you need to begin by making some additions to the<br />

Film Club database.<br />

Updating the Database<br />

In order to give more scope for tricky queries and avoid repeating examples from previous chapters,<br />

this chapter extends the Film Club database and adds some new tables and data. Imagine that<br />

the film club chairperson wants to sell DVDs to members; the film club will employ salespeople to<br />

contact members and sell them DVDs. Therefore, you want to store details of the salespeople,<br />

details of orders taken, and details of what each order contains. In order to do this, you need to<br />

create three new tables (Orders, OrderItems, and SalesPerson), as shown in Figure 9-1:

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

Saved successfully!

Ooh no, something went wrong!