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.

Chapter 3<br />

2. Using the following <strong>SQL</strong>, query the database to produce a list of all the films released in the<br />

1980s that have a rating between 2 and 4 and are available on DVD. The answer should detail<br />

the film name, date of release, rating, and availability on DVD.<br />

SELECT FilmName, YearReleased, Rating, AvailableOnDVD<br />

FROM Films<br />

WHERE ( YearReleased BETWEEN 1980 AND 1989 )<br />

AND<br />

( Rating BETWEEN 2 AND 4 )<br />

AND<br />

( AvailableOnDVD = ‘Y’ );<br />

3. Query the database to produce a list of films of any decade except the 1960s whose names are<br />

between P and T. The answer should detail the film names. Use the following <strong>SQL</strong> to execute<br />

the query:<br />

SELECT FilmName<br />

FROM Films<br />

WHERE ( YearReleased NOT BETWEEN 1960 AND 1969 )<br />

AND<br />

( FilmName BETWEEN ‘P’ AND ‘T’ );<br />

How It Works<br />

68<br />

You begin by inserting five new films into the Films table using INSERT INTO statements. Remember to<br />

commit the data if you’re using Oracle using the COMMIT command, unless you’ve already set the autocommit<br />

to on.<br />

The SELECT clause of the first query is fairly straightforward. It simply asks the database to select<br />

FilmName, YearReleased, Rating, and AvailableOnDVD from the Films database.<br />

SELECT FilmName, YearReleased, Rating, AvailableOnDVD<br />

FROM Films<br />

Next comes the query’s WHERE clause. First, it requires that the film be from the 1980s, and therefore it<br />

requires a range between 1980 and 1989. The BETWEEN operator is ideal for such a requirement:<br />

WHERE YearReleased BETWEEN 1980 AND 1989<br />

The next requirement is that the film must have a rating between 2 and 4. Again, a BETWEEN operator is<br />

the obvious choice. Because both conditions (YearReleased and Rating) must be true, an AND operator is<br />

required to link the two:<br />

WHERE ( YearReleased BETWEEN 1980 AND 1989 )<br />

AND<br />

( Rating BETWEEN 2 AND 4 )<br />

Notice that each condition appears inside brackets. Using brackets is not strictly necessary, but it does<br />

make things more readable: Brackets make it clear that an AND operator joins two unrelated conditions.

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

Saved successfully!

Ooh no, something went wrong!