Introduction to SELECT
Introduction to SQL Operators and Operands
An operation is an action performed on one or more values either to modify the value held by one or both of the variables or to produce a new value by combining values. Therefore, an operation is performed by using at least one symbol and one value. The symbol used in an operation is called an operator. A value involved in an operation is called an operand.
Like every language, SQL ships with some words used to carry its various operations. One of these words is PRINT. To display something in plain text as a result of a statement, type PRINT followed by what to display. Therefore, PRINT uses the following formula:
The item to display can be anything that is allowed and it is provided on the right side of PRINT. If it is a regular constant number, simply type it on the right side of PRINT. The item to display can also be an operation or the result of an operation. You can also display an expression as a combination of number(s) and sentences.
The SELECT operator can be used, among other things, to display a value. The SELECT keyword uses the following syntax:
Based on this, to use it, where it is needed, type SELECT followed by a number, a word, a string, or an expression. The item to display follows some of the same rules as PRINT. One of the differences between PRINT and SELECT is that:
As done for PRINT, to display a sentence using SELECT, type it in single-quotes on the right side of this operator. Here is an executed example:
When you create a SELECT statement, what is on the right side of SELECT must be a value. Here is an example:
Based on this definition, instead of just being a value, the thing on the right side of SELECT must be able to produce a value. As we will see in the next sections, you can create algebraic operations on the right side of SELECT.
As mentioned already, unlike PRINT, SELECT can be used to display more than one value. The values must be separated by commas. Here is an example:
SELECT N'Hourly Salary', 24.85
Because we mentioned that the thing on the right side must produce a result, you can as well use another SELECT statement that it itself evaluates to a result. To distinguish the SELECT sections, the second one should be included in parentheses. Here is an example:
SELECT (SELECT 448.25); GO
When one SELECT statement is created after another, the second is referred to as nested.
Just as you can nest one SELECT statement inside of another, you can also nest one statement in another statement that itself is nested. Here is an example:
SELECT (SELECT (SELECT 1350.75)); GO
In the above introductions, we used either PRINT or SELECT to display something in the query window. One of the characteristics of SELECT is that it can segment its result in different sections. SELECT represents each value in a section called a column. Each column is represented with a name also called a caption. By default, the caption displays as "(No column name)". If you want to use your own caption, on the right side of an expression, type the AS keyword followed by the desired caption. The item on the right side of the AS keyword must be considered as one word. Here is an example:
SELECT 24.85 AS HourlySalary;
This would produce:
You can also include the item on the right side of AS in single-quotes. Here is an example:
SELECT 24.85 AS 'HourlySalary';
If the item on the right side of AS is in different words, you should include it in single-quotes or put them in inside of an opening square bracket "[" and a closing square bracket "]". Here is an example:
SELECT 24.85 AS 'Hourly Salary';
If you create different sections, separated by a comma, you can follow each with AS and a caption. Here is an example:
SELECT N'James Knight' As FullName, 20.48 AS Salary;
This would produce:
The above statement could also be written as follows:
SELECT N'James Knight' As [Full Name], 20.48 AS [Hourly Salary];
Published on Monday 25 December 2007
|Home||Copyright © 2007 FunctionX, Inc.|