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.

Chapter 3<br />

2. Add a WHERE clause to the preceding <strong>SQL</strong> to filter the list so that it includes only those members<br />

who joined in 2005.<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 />

3. Finally, query the database with the following <strong>SQL</strong> to produce a list of members’ names and<br />

dates of birth sorted in descending order by date of birth.<br />

SELECT LastName, FirstName, DateOfBirth<br />

FROM MemberDetails<br />

ORDER BY DateOfBirth DESC;<br />

How It Works<br />

80<br />

Write the SELECT statement to select the LastName, FirstName, and DateOfBirth fields from the<br />

MemberDetails table:<br />

SELECT LastName, FirstName, DateOfBirth<br />

FROM MemberDetails ;<br />

To return the results in the order you want, an ORDER BY clause is required. Because the ORDER BY clause<br />

determines priority of ordering, be sure to list the columns in the order LastName, DateOfBirth, FirstName:<br />

SELECT LastName, FirstName, DateOfBirth<br />

FROM MemberDetails<br />

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

When you execute the preceding <strong>SQL</strong>, you get the results you want:<br />

LastName FirstName DateOfBirth<br />

Botts Seymour 1956-10-21<br />

Dales Stuart 1956-08-07<br />

Doors William 1994-05-28<br />

Gee Steve 1967-10-05<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

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

Saved successfully!

Ooh no, something went wrong!