Beginning SQL
Beginning SQL Beginning SQL
❑ 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
- Page 558: Now that you’ve confirmed that th
- Page 562: Summary Queries within Queries This
- Page 568: Chapter 9 264 Figure 9-1 The SQL ne
- Page 572: Chapter 9 266 INSERT INTO OrderItem
- Page 576: Chapter 9 268 VALUES (5,7, 2,’200
- Page 580: Chapter 9 Now that you’ve added t
- Page 584: Chapter 9 272 FirstName LastName Mo
- Page 588: Chapter 9 274 DVDPrice column is in
- Page 592: Chapter 9 SalesPersonId FirstName L
- Page 596: Chapter 9 278 SalesPersonId MemberI
- Page 600: Chapter 9 280 SalesPersonId MemberI
- Page 604: Chapter 9 282 MemberDetails.FirstNa
- Page 608: Chapter 9 284 The query would work
- Page 614: 10 Views Views, as the name implies
- Page 618: Try It Out Building a View 1. The p
- Page 622: FROM dbo.MemberDetails INNER JOIN d
- Page 626: For example, you might want a list
- Page 630: However, if you take the case of Me
- Page 634: However, someone writes a query tha
- Page 638: Summary As you learned, views are l
- Page 644: Chapter 11 The information that fol
- Page 648: Chapter 11 This SQL adds formats (d
- Page 652: Chapter 11 TRANSACTION statement is
- Page 656: Chapter 11 Additionally, if a DBMS
❑ 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