Select Statement
The form of the Select statement supported by the flat-file drivers is:
SELECT [DISTINCT] {* |column_expression, ...} FROMtable_names[table_alias] ... [ WHEREexpr1rel_operatorexpr2] [ GROUP BY {column_expression, ...} ] [ HAVINGexpr1rel_operatorexpr2] [ UNION [ALL] (SELECT...) ] [ ORDER BY {sort_expression[DESC | ASC]}, ... ] [ FOR UPDATE [OF {column_expression, ...}] ]Select Clause
Follow Select with a list of column expressions you want to retrieve or an asterisk (*) to retrieve all fields.
column_expressioncan be simply a field name (for example, LAST_NAME). More complex expressions may include mathematical operations or string manipulation (for example, SALARY * 1.05). See "SQL Expressions" for details.
column_aliascan be used to give the column a descriptive name. For example, to assign the alias DEPARTMENT to the column DEP:Separate multiple column expressions with commas (for example, LAST_NAME, FIRST_NAME, HIRE_DATE).
Field names can be prefixed with the table name or alias. For example, EMP.LAST_NAME or E.LAST_NAME, where E is the alias for the table EMP.
The Distinct operator can precede the first column expression. This operator eliminates duplicate rows from the result of a query. For example:
Aggregate Functions
Aggregate functions can also be a part of a Select clause. Aggregate functions return a single value from a set of rows. An aggregate can be used with a field name (for example, AVG(SALARY)) or in combination with a more complex column expression (for example, AVG(SALARY * 1.07)). The column expression can be preceded by the Distinct operator. The Distinct operator eliminates duplicate values from an aggregate expression. For example:
In this example, only distinct last name values are counted.
Table 4-1 lists valid aggregate functions.
From Clause
The From clause indicates the tables to be used in the Select statement. The format of the From clause is:
table_namescan be one or more simple table names in the current working directory or complete path names.
table_aliasis a name used to refer to a table in the rest of the Select statement. Database field names may be prefixed by the table alias. Given the table specification:you may refer to the LAST_NAME field as E.LAST_NAME. Table aliases must be used if the Select statement joins a table to itself. For example:
The equal sign (=) includes only matching rows in the results.
If you are joining more than one table, you can use LEFT OUTER JOIN, which includes nonmatching rows in the first table you name. For example:
Where Clause
The Where clause specifies the conditions that rows must meet to be retrieved. The Where clause contains conditions in the form:
expr1andexpr2can be field names, constant values, or expressions.
rel_operatoris the relational operator that links the two expressions. See "SQL Expressions" for details.For example, the following Select statement retrieves the names of employees that make at least $20,000.
Group By Clause
The Group By clause specifies the names of one or more fields by which the returned values should be grouped. This clause is used to return a set of aggregate values. It has the following form:
column_expressionsmust match the column expression used in the Select clause. A column expression can be one or more field names of the database table, separated by a comma (,) or one or more expressions, separated by a comma (,). See "SQL Expressions" for details.The following example sums the salaries in each department:
This statement returns one row for each distinct department ID. Each row contains the department ID and the sum of the salaries of the employees in the department.
Having Clause
The Having clause enables you to specify conditions for groups of rows (for example, display only the departments that have salaries totaling more than $200,000). This clause is valid only if you have already defined a Group By clause. It has the following form:
expr1andexpr2can be field names, constant values, or expressions. These expressions do not have to match a column expression in the Select clause.
rel_operatoris the relational operator that links the two expressions. See "SQL Expressions" for details.The following example returns only the departments whose sums of salaries are greater than $200,000:
Union Operator
The Union operator combines the results of two Select statements into a single result. The single result is all the returned rows from both Select statements. By default, duplicate rows are not returned. To return duplicate rows, use the All keyword (UNION ALL). The form is:
When using the Union operator, the Select lists for each Select statement must have the same number of column expressions with the same data types, and must be specified in the same order. For example:
This example has the same number of column expressions, and each column expression, in order, has the same data type.
The following example is not valid because the data types of the column expressions are different (salary from emp has a different data type than last_name from raises). This example does have the same number of column expressions in each Select statement but the expressions are not in the same order by data type.
Order By Clause
The Order By clause indicates how the rows are to be sorted. The form is:
sort_expressioncan be field names, expressions, or the positioned number of the column expression to use.The default is to perform an ascending (ASC) sort.
For example, to sort by last_name and then by first_name, you could use either of the following Select statements:
or
In the second example, last_name is the second column expression following Select, so Order By 2 sorts by last_name.
For Update Clause
The For Update clause locks the rows of the database table selected by the Select statement. The form is:
column_expressionsis a list of field names in the database table that you intend to update, separated by a comma (,).The following example returns all rows in the employee database that have a salary field value of more than $20,000. When each record is fetched, it is locked. If the record is updated or deleted, the lock is held until you commit the change. Otherwise, the lock is released when you fetch the next record.
SQL Expressions
Expressions are used in the Where clauses, Having clauses, and Order By clauses of SQL Select statements.
Expressions enable you to use mathematical operations as well as character string and date manipulation operators to form complex database queries.
The most common expression is a simple field name. You can combine a field name with other expression elements.
Valid expression elements are as follows:
Constants
Constants are values that do not change. For example, in the expression PRICE * 1.05, the value 1.05 is a constant.
You must enclose character constants in pairs of single (') or double (") quotation marks. To include a single quotation mark in a character constant enclosed by single quotation marks, use two single quotation marks together (for example, 'Don''t'). Similarly, if the constant is enclosed by double quotation marks, use two double quotation marks to include one.
You must enclose date and time constants in braces ({}), for example, {01/30/89} and {12:35:10}. The form for date constants is MM/DD/YY or MM/DD/YYYY. The form for time constants is HH:MM:SS.
The logical constants are .T. and 1 for True and .F. and 0 for False. For portability, use 1 and 0.
Exponential Notation
You can include exponential notation in expression elements. For example:
Numeric Operators
You can include the following operators in numeric expressions:
Operator Meaning + Addition - Subtraction * Multiplication / Division ** Exponentiation ^ Exponentiation
The following table shows examples of numeric expressions. For these examples, assume salary is 20000.
You can precede numeric expressions with a unary plus (+) or minus (-). For example,
-(salary * 1.1)is -22000.Character Operators
Character expressions can include the following operators:
Operator Meaning + Concatenation, keeping trailing blanks. - Concatenation, moving trailing blanks to the end.
The following table shows examples of character expressions. In the examples, last_name is
'JONES 'and first_name is'ROBERT '.
Example Resulting Valuefirst_name + last_name'ROBERT JONES 'first_name - last_name'ROBERTJONES '
NOTE: Some flat-file drivers return character data with trailing blanks as shown in the table; however, you cannot rely on the driver to return blanks. If you want an expression that works regardless of whether the drivers return trailing blanks, use the TRIM function before concatenating strings to make the expression portable. For example:
Date Operators
You can include the following operators in date expressions:
Operator Meaning + Add a number of days to a date to produce a new date. - The number of days between two dates, or subtract a number of days from a date to produce a new date.
The following table shows examples of date expressions. In these examples, hire_date is {01/30/1990}.
Example Resulting Valuehire_date + 5{02/04/1990}hire_date - {01/01/1990}29hire_date - 10{01/20/1990}
Relational Operators
Relational operators separating any two expressions can be any one of those listed in Table 4-2.
The following list shows some examples of relational operators:
salary <= 40000 dept = 'D101' hire_date > {01/30/1989} salary + commission >= 50000 last_name LIKE 'Jo%' salary IS NULL salary BETWEEN 10000 AND 20000 WHERE salary = ANY (SELECT salary FROM emp WHERE dept = 'D101') WHERE salary > ALL (SELECT salary FROM emp WHERE dept = 'D101')Logical Operators
Two or more conditions may be combined to form more complex criteria. When two or more conditions are present, they must be related by AND or OR. For example:
The logical NOT operator is used to reverse the meaning. For example:
Operator Precedence
As expressions become more complex, the order in which the expressions are evaluated becomes important. Table 4-3 shows the order in which the operators are evaluated. The operators in the first line are evaluated first, then those in the second line, and so on. Operators in the same line are evaluated left to right in the expression.
Table 4-3. Operator Precedence Precedence Operator 1 Unary -, Unary + 2 ** 3 *, / 4 +, - 5 =, <>, <, <=, >, >=, LIKE, NOT LIKE, IS NULL, IS NOT NULL, BETWEEN, IN, EXISTS, ANY, ALL 6 NOT 7 AND 8 OR
The following example shows the importance of precedence:
Because AND is evaluated first, this query retrieves employees in department D101 hired after January 30, 1989, as well as every employee making more than $40,000, no matter what department or hire date.
To force the clause to be evaluated in a different order, use parentheses to enclose the conditions to be evaluated first. For example:
retrieves employees in department D101 that either make more than $40,000 or were hired after January 30, 1989.
Functions
The flat-file drivers support a number of functions that you may use in expressions. In Table 4-4 through Table 4-6, the functions are grouped according to the type of result they return.
The following examples use some of the number and date functions.
Retrieve all employees that have been with the company at least 90 days:
Retrieve all employees hired in January of this year or last year: