SELECT STATEMENT REVISITED
The SELECT statement — along with INSERT, UPDATE, and DELETE — are the four pillars of SQL, and you need to master them to be considered a fluent, if not a native speaker. We discussed this statement briefly in Chapter 1; now let's take a closer look.
Selecting Literals, Functions, and Calculated Columns
There are many things to select from in a database, and tables are not the only game in town. In the examples on implicit data conversion, you've already seen how we can perform arithmetic and strings concatenation using SELECT and literal values (Microsoft SQL Server syntax):
SELECT 1+2 AS SumOfTwo, ‘one’ + ‘two’ AS TwoStrings; SumOfTwo TwoStrings ----------- ------------ 3 onetwo
Note that because we do not refer to a table (well, Oracle would ask for selecting FROM dual, and IBM insists that such expressions were selected FROM sysibm.sysdummy1); no FROM statement is required.
This also can be expanded to include calculated columns and functions (SQL functions are covered in Chapter 4). If, for instance, you'd need to see how much your books would cost in Japanese Yen, all you have to do is to multiply its price value by the exchange rate:
SELECT bk_price * 80.6829 AS PriceInYen FROM books; PriceInYen ----------- 3226.50917100
Adding SQL functions to the mix is just as easy, and you get to alias the resulting columns! Using Microsoft SQL Server's built-in GetDate() function (more about functions in Chapter 4), we can get the today's date as result ...
Get Discovering SQL: A Hands-On Guide for Beginners now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.