O'Reilly logo

Discovering SQL: A Hands-On Guide for Beginners by Alex Kriegel

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required