20.07.2013 Views

Beginning SQL

Beginning SQL

Beginning SQL

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

LastName FirstName DateOfBirth<br />

Night Doris 1997-05-28<br />

Simons Susie 1937-01-20<br />

Smith Katie 1977-01-09<br />

To filter the results to include only members who joined in 2005, simply add a WHERE clause before the<br />

ORDER BY clause. Using a BETWEEN clause is the easiest way to include only dates in 2005. Currently the<br />

database contains only dates in 2004 and 2005, so it might be tempting to filter all dates with a >= 2005<br />

operator, but if you add data later that includes 2006 dates and beyond, the <strong>SQL</strong> would no longer return<br />

valid results. With the WHERE clause added, the <strong>SQL</strong> is now as follows:<br />

SELECT LastName, FirstName, DateOfBirth<br />

FROM MemberDetails<br />

WHERE DateOfJoining BETWEEN ‘2005-01-01’ AND ‘2005-12-31’<br />

ORDER BY LastName, DateOfBirth, FirstName;<br />

Remember that Oracle’s date format is day-month-year. Also remember that MS Access needs the hash<br />

sign (#) rather than single quotes around the dates.<br />

When executed, the preceding <strong>SQL</strong> provides these results:<br />

LastName FirstName DateOfBirth<br />

Botts Seymour 1956-10-21<br />

Dales Stuart 1956-08-07<br />

Doors William 1994-05-28<br />

Hills Jamie 1992-07-17<br />

Jackson John 1974-05-27<br />

Johnson Jack 1945-06-09<br />

Jones John 1952-10-05<br />

Jones Jenny 1953-08-25<br />

Night Doris 1997-05-28<br />

Simons Susie 1937-01-20<br />

Previously, results have appeared in ascending order, which is the ORDER BY clause’s default order. This<br />

time, however, you want results to appear in descending order, so you must add DESC at the end of the<br />

ORDER BY clause’s list of columns:<br />

SELECT LastName, FirstName, DateOfBirth<br />

FROM MemberDetails<br />

ORDER BY DateOfBirth DESC;<br />

Extracting Information<br />

81

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

Saved successfully!

Ooh no, something went wrong!