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.

3<br />

Extracting Information<br />

So far you’ve learned how to set up a database and how to insert data into it, so now you can learn<br />

how to extract data from your database. Arguably, <strong>SQL</strong>’s most powerful feature is its ability to<br />

extract data, and extracting data can be as simple or complex as you require. You can simply<br />

extract data in the same form in which it was entered into the database, or you could query the<br />

database and obtain answers to questions that are not obvious from the basic data. In your example<br />

database, you can use <strong>SQL</strong> to find out which meeting locations are most popular, or you could<br />

find out which meeting locations are most popular for which film category. It might turn out that<br />

Windy Village has a particularly large number of sci-fi fans. If the film club decides to show a film<br />

at that location, you would be aware that a sci-fi film is likely to be popular. The ability to ask the<br />

database questions and get answers via <strong>SQL</strong> queries makes <strong>SQL</strong> so popular and useful.<br />

The key to getting data out is the SELECT statement, which in its basic form is very simple and<br />

easy to use. However, as you go through this chapter and then the advanced chapters, you see lots<br />

of extra options that make the SELECT statement very powerful. Before getting too complicated,<br />

however, you need to familiarize yourself with the SELECT statement.<br />

The SELECT Statement<br />

At its simplest, the SELECT requires you to tell it which columns and from what table you want to<br />

obtain data. The basic syntax is as follows:<br />

SELECT column1, column2,.....columnx FROM table_name<br />

Using the basic syntax, the <strong>SQL</strong> required to select the MemberId and FirstName columns from all<br />

records in the MemberDetails table is<br />

SELECT MemberId, FirstName FROM MemberDetails;<br />

The order in which you list the columns in the SELECT statement determines the order in which<br />

they are returned in the results. The preceding query returns this order:

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

Saved successfully!

Ooh no, something went wrong!