Beginning SQL

Beginning SQL Beginning SQL

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

❑ Use IN with your subqueries. When you write a query similar to the following, the database system has to get all the results from the subquery to make sure that it returns only one value: SELECT FirstName, LastName FROM MemberDetails WHERE MemberId = (SELECT MemberId FROM Orders WHERE OrderId = 2); If you rewrite the query using the IN operator, the database system only needs to get results until there’s a match with the values returned by the subquery; it doesn’t necessarily have to get all the values: SELECT FirstName, LastName FROM MemberDetails WHERE MemberId IN (SELECT MemberId FROM Orders WHERE OrderId = 2); ❑ Avoid using SELECT * FROM. Specifying which columns you need has a few advantages, not all of them about efficiency. First, it makes clear which columns you’re actually using. If you use SELECT * and actually use only two out of seven of the columns, it’s hard to guess from the SQL alone which ones you’re using. If you say SELECT FirstName, LastName...then it’s quite obvious which columns you’re using. From an efficiency standpoint, specifying columns reduces the amount of data that has to pass between the database and the application connecting to the database. This is especially important where the database is connected over a network. ❑ Search on integer columns. If you have a choice, and often you don’t, search on integer columns. For example, if you are looking for the member whose name is William Doors and whose MemberId is 13, then it makes sense to search via the MemberId because it’s much faster. In the Film Club database, it’s also the primary key column, so there’s even more of a speed boost. Summary This chapter looked at how to tackle difficult query writing. You examined some fairly involved queries and learned how to break them down into manageable chunks, and then you saw a few tips on how to increase the efficiency of your queries. Specifically, this chapter covered the following topics: ❑ Working out what the question is and what it actually involves ❑ How to create complex queries by following a basic process: ❑ Creating the SELECT list first ❑ Looking at the FROM clause and how you need to obtain your data ❑ Tackling difficult queries ❑ Using tips to write more efficient queries Advanced Queries By now, you should be familiar enough with SQL to design databases; populate tables and columns in those databases; add, insert, retrieve, and delete data from your databases; and write complex queries that obtain exactly the information you want. The chapters that follow cover topics more tangentially related to SQL, such as views, transactions, security, and methods for tuning your database so that it performs optimally. The next chapter covers views and discusses how to use them to pull specific data for specific users and create coherent data sets that are of interest to a particular user or application. 285

❑ Use IN with your subqueries. When you write a query similar to the following, the database<br />

system has to get all the results from the subquery to make sure that it returns only one value:<br />

SELECT FirstName, LastName<br />

FROM MemberDetails<br />

WHERE MemberId = (SELECT MemberId FROM Orders WHERE OrderId = 2);<br />

If you rewrite the query using the IN operator, the database system only needs to get results<br />

until there’s a match with the values returned by the subquery; it doesn’t necessarily have to get<br />

all the values:<br />

SELECT FirstName, LastName<br />

FROM MemberDetails<br />

WHERE MemberId IN (SELECT MemberId FROM Orders WHERE OrderId = 2);<br />

❑ Avoid using SELECT * FROM. Specifying which columns you need has a few advantages, not all<br />

of them about efficiency. First, it makes clear which columns you’re actually using. If you use<br />

SELECT * and actually use only two out of seven of the columns, it’s hard to guess from the <strong>SQL</strong><br />

alone which ones you’re using. If you say SELECT FirstName, LastName...then it’s quite<br />

obvious which columns you’re using. From an efficiency standpoint, specifying columns reduces<br />

the amount of data that has to pass between the database and the application connecting to the<br />

database. This is especially important where the database is connected over a network.<br />

❑ Search on integer columns. If you have a choice, and often you don’t, search on integer columns.<br />

For example, if you are looking for the member whose name is William Doors and whose<br />

MemberId is 13, then it makes sense to search via the MemberId because it’s much faster. In the<br />

Film Club database, it’s also the primary key column, so there’s even more of a speed boost.<br />

Summary<br />

This chapter looked at how to tackle difficult query writing. You examined some fairly involved queries<br />

and learned how to break them down into manageable chunks, and then you saw a few tips on how to<br />

increase the efficiency of your queries. Specifically, this chapter covered the following topics:<br />

❑ Working out what the question is and what it actually involves<br />

❑ How to create complex queries by following a basic process:<br />

❑ Creating the SELECT list first<br />

❑ Looking at the FROM clause and how you need to obtain your data<br />

❑ Tackling difficult queries<br />

❑ Using tips to write more efficient queries<br />

Advanced Queries<br />

By now, you should be familiar enough with <strong>SQL</strong> to design databases; populate tables and columns in<br />

those databases; add, insert, retrieve, and delete data from your databases; and write complex queries<br />

that obtain exactly the information you want. The chapters that follow cover topics more tangentially<br />

related to <strong>SQL</strong>, such as views, transactions, security, and methods for tuning your database so that it performs<br />

optimally. The next chapter covers views and discusses how to use them to pull specific data for<br />

specific users and create coherent data sets that are of interest to a particular user or application.<br />

285

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

Saved successfully!

Ooh no, something went wrong!