Beginning SQL

Beginning SQL Beginning SQL

marjan.fesb.hr
from marjan.fesb.hr More from this publisher
20.07.2013 Views

FirstName LastName DateOfBirth Jenny Jones 1953-08-25 Jack Johnson 1945-06-09 Seymour Botts 1956-10-21 Jamie Hills 1992-07-17 Stuart Dales 1956-08-07 William Doors 1994-05-28 Doris Night 1997-05-28 Catherine Hawthorn Depending on your database system, it might list DateOfBirth for Catherine as NULL or it may just show nothing at all, as in the preceding table. Generally speaking, you are better off avoiding the NULL data type and instead using some default value. For example, if you query for a numerical field, use a number that is never normally part of the results, such as –1 for an age field. For a text field, use an empty string, and so on. Chapter 5 revisits the NULL data type when looking at SQL math. Summary This chapter covered a lot of topics, some of which were quite challenging, but they all dealt with how to get answers out of a database. At the end of the day, that’s what SQL and databases are all about — getting answers. The key to extracting data with SQL is the SELECT query, which allows you to select which columns and from what tables to extract data. The chapter also discussed the following: Extracting Information ❑ How to filter results so that you get only the data you require. The WHERE clause allows you to specify any number of conditions in order to filter your results to suit your particular query. Only if your specific conditions are met does a record appear in the final results set. ❑ The logical operators AND, OR, NOT, BETWEEN, IN, and LIKE. Coverage of the AND and OR operators was a rehash from the previous chapter, but the rest were introduced in this chapter. NOT allows you to reverse a condition. The BETWEEN operator allows you to specify a range of values and proves a condition true when a column value is within the specified range. When you have a list of potential values, the IN operator comes in handy. It proves a condition true when the column has a value that is in the list of given values. Finally, you learned how to use the LIKE operator with text. The LIKE operator allows the use of wildcard characters. ❑ After you learned how to get the results set you want, you learned how to use the ORDER BY clause, which allows you to list the order of results in ascending or descending order, based on one or more columns. 115

FirstName LastName DateOfBirth<br />

Jenny Jones 1953-08-25<br />

Jack Johnson 1945-06-09<br />

Seymour Botts 1956-10-21<br />

Jamie Hills 1992-07-17<br />

Stuart Dales 1956-08-07<br />

William Doors 1994-05-28<br />

Doris Night 1997-05-28<br />

Catherine Hawthorn<br />

Depending on your database system, it might list DateOfBirth for Catherine as NULL or it may just show<br />

nothing at all, as in the preceding table.<br />

Generally speaking, you are better off avoiding the NULL data type and instead using some default<br />

value. For example, if you query for a numerical field, use a number that is never normally part of the<br />

results, such as –1 for an age field. For a text field, use an empty string, and so on. Chapter 5 revisits the<br />

NULL data type when looking at <strong>SQL</strong> math.<br />

Summary<br />

This chapter covered a lot of topics, some of which were quite challenging, but they all dealt with how to<br />

get answers out of a database. At the end of the day, that’s what <strong>SQL</strong> and databases are all about — getting<br />

answers. The key to extracting data with <strong>SQL</strong> is the SELECT query, which allows you to select which<br />

columns and from what tables to extract data.<br />

The chapter also discussed the following:<br />

Extracting Information<br />

❑ How to filter results so that you get only the data you require. The WHERE clause allows you to<br />

specify any number of conditions in order to filter your results to suit your particular query.<br />

Only if your specific conditions are met does a record appear in the final results set.<br />

❑ The logical operators AND, OR, NOT, BETWEEN, IN, and LIKE. Coverage of the AND and OR operators<br />

was a rehash from the previous chapter, but the rest were introduced in this chapter. NOT<br />

allows you to reverse a condition. The BETWEEN operator allows you to specify a range of values<br />

and proves a condition true when a column value is within the specified range. When you have<br />

a list of potential values, the IN operator comes in handy. It proves a condition true when the<br />

column has a value that is in the list of given values. Finally, you learned how to use the LIKE<br />

operator with text. The LIKE operator allows the use of wildcard characters.<br />

❑ After you learned how to get the results set you want, you learned how to use the ORDER BY<br />

clause, which allows you to list the order of results in ascending or descending order, based on<br />

one or more columns.<br />

115

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

Saved successfully!

Ooh no, something went wrong!