Beginning SQL
Beginning SQL Beginning SQL
function and the query still works, except you might find that YearReleased is converted from an integer into a date data type with some quite odd results. The YEAR() function is covered in Chapter 5, as is a discussion of data type conversion. Although normally CAST() can be used to convert data types, it’s not necessary here because YEAR() returns the correct data type for the results. Also notice that the ORDER BY clause comes at the end of the UNION statements and refers to the first SELECT query. Again, this doesn’t work on IBM’s DB2, which allows the ORDER BY statement to come last only if the name of the column being ordered appears in all the queries. Likewise, the ORDER BY syntax when using UNION is different in Oracle. Rather than specifying the name of the column to be ordered, you specify the position in which the column appears in the SELECT list. If you want to order by FilmName, which is the first column, the ORDER BY clause would look like the following: ORDER BY 1; If you want to order by the second column, change the 1 to a 2. If you want to order by the third column, then change the 1 to a 3, and so on. Also note that the query includes only the City column from the Location table and that NULL is substituted for the second column. This works on many database platforms but not IBM DB2. Change the value from NULL to an integer value that makes it clear that the value is not a genuine piece of data (-1, for example). Executing the query provides the following results: 15th Late Afternoon 1989 Big City NULL Botts 1956 Dales 1956 Doors 1994 Gee 1967 Gone with the Window Cleaner 1988 Hawthorn NULL Hills 1992 Jackson 1974 Johnson 1945 Jones 1952 Jones 1953 Night 1997 Nightmare on Oak Street, Part 23 1997 Selecting Data from Different Tables Table continued on following page 231
- Page 450: Executing the final SQL provides th
- Page 456: Chapter 7 The INNER JOIN links the
- Page 460: Chapter 7 FilmName FirstName LastNa
- Page 464: Chapter 7 212 As mentioned, that wo
- Page 468: Chapter 7 Category Street Romance T
- Page 472: Chapter 7 216 MD1.First MD1.Last MD
- 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 506: Summary Selecting Data from Differe
- Page 512: Chapter 8 A WHERE clause is added t
- Page 516: Chapter 8 238 FilmName PlotSummary
- Page 520: Chapter 8 If you think that your jo
- Page 524: Chapter 8 In the query, the Categor
- 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
function and the query still works, except you might find that YearReleased is converted from an integer<br />
into a date data type with some quite odd results. The YEAR() function is covered in Chapter 5, as is a discussion<br />
of data type conversion. Although normally CAST() can be used to convert data types, it’s not necessary<br />
here because YEAR() returns the correct data type for the results.<br />
Also notice that the ORDER BY clause comes at the end of the UNION statements and refers to the first<br />
SELECT query. Again, this doesn’t work on IBM’s DB2, which allows the ORDER BY statement to come<br />
last only if the name of the column being ordered appears in all the queries. Likewise, the ORDER BY<br />
syntax when using UNION is different in Oracle. Rather than specifying the name of the column to be<br />
ordered, you specify the position in which the column appears in the SELECT list. If you want to order<br />
by FilmName, which is the first column, the ORDER BY clause would look like the following:<br />
ORDER BY 1;<br />
If you want to order by the second column, change the 1 to a 2. If you want to order by the third column,<br />
then change the 1 to a 3, and so on.<br />
Also note that the query includes only the City column from the Location table and that NULL is substituted<br />
for the second column. This works on many database platforms but not IBM DB2. Change the<br />
value from NULL to an integer value that makes it clear that the value is not a genuine piece of data (-1,<br />
for example).<br />
Executing the query provides the following results:<br />
15th Late Afternoon 1989<br />
Big City NULL<br />
Botts 1956<br />
Dales 1956<br />
Doors 1994<br />
Gee 1967<br />
Gone with the Window Cleaner 1988<br />
Hawthorn NULL<br />
Hills 1992<br />
Jackson 1974<br />
Johnson 1945<br />
Jones 1952<br />
Jones 1953<br />
Night 1997<br />
Nightmare on Oak Street, Part 23 1997<br />
Selecting Data from Different Tables<br />
Table continued on following page<br />
231