Beginning SQL
Beginning SQL Beginning SQL
The query provides the following results: FirstName LastName YEAR(DateOfBirth) Steve Gee 1967 Susie Simons 1937 Jamie Hills 1992 What you didn’t learn in Chapter 3, however, is that you can also use the IN operator with subqueries. Instead of providing a list of literal values, a SELECT query provides the list of values. For example, if you want to know which members were born in the same year that a film in the Films table was released, you’d use the following SQL query. Again, these examples won’t work in Oracle because it doesn’t support the YEAR() function: SELECT FirstName, LastName, YEAR(DateOfBirth) FROM MemberDetails WHERE YEAR(DateOfBirth) IN (SELECT YearReleased FROM Films); Executing this query gives the following results: FirstName LastName YEAR(DateOfBirth) Katie Smith 1977 Steve Gee 1967 Doris Night 1997 The subquery (SELECT YearReleased FROM Films) returns a list of years from the Films table. If a member’s year of birth matches one of the items in that list, then the WHERE clause is true and the record is included in the final results. You may have spotted that this is not the only way to get the result. You could have used an INNER JOIN coupled with a GROUP BY statement instead, as shown in the following SQL: SELECT FirstName, LastName, YEAR(DateOfBirth) FROM MemberDetails JOIN Films ON YEAR(DateOfBirth) = YearReleased GROUP BY FirstName, LastName, YEAR(DateOfBirth); Queries within Queries Running this query gives the same results as the previous query. So which is best? Unfortunately, there’s no definitive answer; very much depends on the circumstances, the data involved, and the database system involved. A lot of SQL coders prefer a join to a subquery and believe that to be the most efficient. However, if you compare the speed of the two using MS SQL Server 2000, in this case, on that system, the subquery is faster by roughly 15 percent. Given how few rows there are in the database, the difference was negligible in this example, but it might be significant with a lot of records. Which way should you go? You should go with the way that you find easiest, and fine-tune your SQL code only if problems occur during testing. If you find on a test system with a million records that your SQL runs like an arthritic snail with heavy shopping, then you should go back and see whether you can improve your query. 243
- Page 476: Chapter 7 SELECT MD1.MemberId, MD1.
- Page 480: Chapter 7 220 SELECT Category, Film
- Page 484: Chapter 7 222 Location.Street Membe
- Page 488: Chapter 7 224 Category MemberId War
- Page 492: Chapter 7 Look at the results that
- Page 496: Chapter 7 228 The Maltese Poodle 8
- Page 500: Chapter 7 230 9 10 11 12 13 14 15 1
- Page 504: Chapter 7 232 On Golden Puddle 1967
- Page 510: 8 Queries within Queries This chapt
- Page 514: There are no films with a value in
- Page 518: The error wording depends on the da
- Page 522: Now execute the query. You receive
- Page 528: Chapter 8 244 There is one area in
- Page 532: Chapter 8 246 SELECT FirstName, Las
- Page 536: Chapter 8 248 The subquery (SELECT
- Page 540: Chapter 8 250 Notice the use of the
- Page 544: Chapter 8 4. You should receive the
- Page 548: Chapter 8 254 This inner query retu
- Page 552: Chapter 8 It’s quite important no
- Page 556: Chapter 8 258 To tackle this query,
- Page 560: Chapter 8 260 Now you need to find
- Page 566: 9 Advanced Queries In database prog
- Page 570: FOREIGN KEY (OrderId) REFERENCES Or
- Page 574: INSERT INTO Orders(OrderId, MemberI
The query provides the following results:<br />
FirstName LastName YEAR(DateOfBirth)<br />
Steve Gee 1967<br />
Susie Simons 1937<br />
Jamie Hills 1992<br />
What you didn’t learn in Chapter 3, however, is that you can also use the IN operator with subqueries.<br />
Instead of providing a list of literal values, a SELECT query provides the list of values. For example, if<br />
you want to know which members were born in the same year that a film in the Films table was<br />
released, you’d use the following <strong>SQL</strong> query. Again, these examples won’t work in Oracle because it<br />
doesn’t support the YEAR() function:<br />
SELECT FirstName, LastName, YEAR(DateOfBirth)<br />
FROM MemberDetails<br />
WHERE YEAR(DateOfBirth) IN (SELECT YearReleased FROM Films);<br />
Executing this query gives the following results:<br />
FirstName LastName YEAR(DateOfBirth)<br />
Katie Smith 1977<br />
Steve Gee 1967<br />
Doris Night 1997<br />
The subquery (SELECT YearReleased FROM Films) returns a list of years from the Films table. If a<br />
member’s year of birth matches one of the items in that list, then the WHERE clause is true and the record<br />
is included in the final results.<br />
You may have spotted that this is not the only way to get the result. You could have used an INNER<br />
JOIN coupled with a GROUP BY statement instead, as shown in the following <strong>SQL</strong>:<br />
SELECT FirstName, LastName, YEAR(DateOfBirth)<br />
FROM MemberDetails JOIN Films ON YEAR(DateOfBirth) = YearReleased<br />
GROUP BY FirstName, LastName, YEAR(DateOfBirth);<br />
Queries within Queries<br />
Running this query gives the same results as the previous query. So which is best? Unfortunately, there’s<br />
no definitive answer; very much depends on the circumstances, the data involved, and the database system<br />
involved. A lot of <strong>SQL</strong> coders prefer a join to a subquery and believe that to be the most efficient.<br />
However, if you compare the speed of the two using MS <strong>SQL</strong> Server 2000, in this case, on that system, the<br />
subquery is faster by roughly 15 percent. Given how few rows there are in the database, the difference<br />
was negligible in this example, but it might be significant with a lot of records. Which way should you go?<br />
You should go with the way that you find easiest, and fine-tune your <strong>SQL</strong> code only if problems occur<br />
during testing. If you find on a test system with a million records that your <strong>SQL</strong> runs like an arthritic snail<br />
with heavy shopping, then you should go back and see whether you can improve your query.<br />
243