Beginning SQL
Beginning SQL Beginning SQL
You can raise a number to any power, from 1 to whatever the database system can handle; some huge numbers, however, will be too large for the database system to handle. For example, if you want to know what the results are when the MemberId is squared (raised to the power of 2), cubed (raised to the power of 3), and raised to the power of 7, you’d write the following SQL SELECT MemberId, POWER(MemberId,2), POWER(MemberId, 3) , POWER(MemberId, 7) FROM MemberDetails ORDER BY MemberId; Executing the statement gives the results in the following table: Manipulating Data MemberId POWER(MemberId,2) POWER(MemberId, 3) POWER(MemberId, 7) 1 1 1 1 4 16 64 16384 5 25 125 78125 6 36 216 279936 7 49 343 823543 8 64 512 2097152 9 81 729 4782969 10 100 1000 10000000 11 121 1331 19487171 12 144 1728 35831808 13 169 2197 62748517 14 196 2744 105413504 15 225 3375 170859375 For the first and second rows in the preceding table, the columns were calculated as shown in the following table: MemberId POWER(MemberId,2) POWER(MemberId, 3) POWER(MemberId, 7) 1 1 * 1 = 1 1 * 1 * 1 = 1 1 * 1 * 1 * 1 * 1 * 1 * 1 = 1 4 4 * 4 = 16 4 * 4 * 4 = 64 4 * 4 * 4 * 4 * 4 * 4 * 4 = 16384 Now that you’ve learned how to raise to the power of 2, you can put that in reverse and find the square root of a number. 161
- Page 312: Chapter 4 136 If you want to return
- Page 316: Chapter 4 If you’re using IBM DB2
- Page 320: Chapter 4 140 index helps speed up
- Page 324: Chapter 4 142 The results you get m
- Page 328: Chapter 4 144 The results of the qu
- Page 332: Chapter 4 146 Remember, IBM DB2 doe
- Page 336: Chapter 4 148 UPDATE Films SET Film
- Page 340: Chapter 4 150 Finally, you do the s
- Page 344: Chapter 4 152 SELECT * FROM MemberD
- Page 348: Chapter 4 ❑ Choose meaningful nam
- Page 354: 5 Manipulating Data In Chapter 3, y
- Page 358: The results of this query are as fo
- Page 364: Chapter 5 The SQRT() Function The S
- Page 368: Chapter 5 164 chairperson now also
- Page 372: Chapter 5 The FLOOR() Function The
- Page 376: Chapter 5 DVDPrice ROUND(DVDPrice,1
- Page 380: Chapter 5 Note that, as with all th
- Page 384: Chapter 5 The TRIM() Functions Trim
- Page 388: Chapter 5 174 In Oracle and IBM’s
- Page 392: Chapter 5 176 FirstName SOUNDEX(Fir
- Page 396: Chapter 5 Date Functions 178 Someti
- Page 400: Chapter 5 The syntax for the CAST()
- Page 404: Chapter 5 NULLs and Strings 182 NUL
- Page 408: Chapter 5 184 SELECT FirstName, Las
You can raise a number to any power, from 1 to whatever the database system can handle; some huge<br />
numbers, however, will be too large for the database system to handle. For example, if you want to<br />
know what the results are when the MemberId is squared (raised to the power of 2), cubed (raised to the<br />
power of 3), and raised to the power of 7, you’d write the following <strong>SQL</strong><br />
SELECT MemberId, POWER(MemberId,2), POWER(MemberId, 3) , POWER(MemberId, 7)<br />
FROM MemberDetails<br />
ORDER BY MemberId;<br />
Executing the statement gives the results in the following table:<br />
Manipulating Data<br />
MemberId POWER(MemberId,2) POWER(MemberId, 3) POWER(MemberId, 7)<br />
1 1 1 1<br />
4 16 64 16384<br />
5 25 125 78125<br />
6 36 216 279936<br />
7 49 343 823543<br />
8 64 512 2097152<br />
9 81 729 4782969<br />
10 100 1000 10000000<br />
11 121 1331 19487171<br />
12 144 1728 35831808<br />
13 169 2197 62748517<br />
14 196 2744 105413504<br />
15 225 3375 170859375<br />
For the first and second rows in the preceding table, the columns were calculated as shown in the following<br />
table:<br />
MemberId POWER(MemberId,2) POWER(MemberId, 3) POWER(MemberId, 7)<br />
1 1 * 1 = 1 1 * 1 * 1 = 1 1 * 1 * 1 * 1 * 1 * 1 * 1 = 1<br />
4 4 * 4 = 16 4 * 4 * 4 = 64 4 * 4 * 4 * 4 * 4 * 4 * 4 = 16384<br />
Now that you’ve learned how to raise to the power of 2, you can put that in reverse and find the square<br />
root of a number.<br />
161