SELECT [DISTINCT] {* | column_expression, ...}[ GROUP BY {column_expression, ...} ][ ORDER BY {sort_expression [DESC | ASC]}, ... ][ FOR UPDATE [OF {column_expression, ...}] ]Follow Select with a list of column expressions you want to retrieve or an asterisk (*) to retrieve all fields.column_expression can 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_alias can 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 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:Table 11-1 lists valid aggregate functions.
Table 11-1. Aggregate Functions The total of the values in a numeric field expression. For example, SUM(SALARY) returns the sum of all salary field values. The average of the values in a numeric field expression. For example, AVG(SALARY) returns the average of all salary field values. The number of values in any field expression. For example, COUNT(NAME) returns the number of name values. When using COUNT with a field name, COUNT returns the number of non-NULL field values. A special example is COUNT(*), which returns the number of rows in the set, including rows with NULL values. The maximum value in any field expression. For example, MAX(SALARY) returns the maximum salary field value. The minimum value in any field expression. For example, MIN(SALARY) returns the minimum salary field value.The From clause indicates the tables to be used in the Select statement. The format of the From clause is:table_names can be one or more simple table names in the current working directory or complete path names.table_alias is 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:If you are joining more than one table, you can use LEFT OUTER JOIN, which includes non-matching rows in the first table you name. For example:The Where clause specifies the conditions that rows must meet to be retrieved. The Where clause contains conditions in the form:expr1 and expr2 can be field names, constant values, or expressions.rel_operator is 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.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:GROUP BY column_expressionscolumn_expressions must 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.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.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:expr1 and expr2 can be field names, constant values, or expressions. These expressions do not have to match a column expression in the Select clause.rel_operator is 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: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:SELECT statementWhen 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 {sort_expression [DESC | ASC]}, ...sort_expression can be field names, expressions, or the positioned number of the column expression to use.For example, to sort by last_name and then by first_name, you could use either of the following Select statements:In the second example, last_name is the second column expression following Select, so Order By 2 sorts by last_name.The For Update clause locks the rows of the database table selected by the Select statement. The form is:FOR UPDATE OF column_expressionscolumn_expressions is 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.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.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 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.
The following table shows examples of character expressions. In the examples, last_name is 'JONES ' and first_name is 'ROBERT '.
'ROBERT JONES ' 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:
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}.
Table 11-2. Relational Operators Compares a value to each value returned by a subquery. Any must be prefaced by =, <>, >, >=, <, or <=. Compares a value to each value returned by a subquery. All must be prefaced by =, <>, >, >=, <, or <=.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:As expressions become more complex, the order in which the expressions are evaluated becomes important. Table 11-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 11-3. Operator Precedence =, <>, <, <=, >, >=, LIKE, NOT LIKE, IS NULL, IS NOT NULL, BETWEEN, IN, EXISTS, ANY, ALL 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.The flat-file drivers support a number of functions that you may use in expressions. In Table 11-4 through Table 11-6, the functions are grouped according to the type of result they return.
Table 11-4. Functions that Return Character Strings CHR(67) returns C. UPPER('Allen') returns ALLEN. LOWER('Allen') returns allen. LEFT('Mattson',3) returns Mat. RIGHT('Mattson',4) returns tson. Returns a substring of a string. Parameters are the string, the first character to extract, and the number of characters to extract (optional).SUBSTR('Conrad',2,3) returns onr.SUBSTR('Conrad',2) returns onrad. SPACE(5) returns ' '. Converts a date to a character string. An optional second parameter determines the format of the result:An optional third parameter specifies the date separator character. If not specified, a slash (/) is used.DTOC({01/30/1997}) returns 01/30/97.DTOC({01/30/1997}, 0) returns 01/30/97.DTOC({01/30/1997}, 1) returns 30/01/97.DTOC({01/30/1997}, 2,'-') returns 97-01-30. DTOS({01/23/1990}) returns 19900123. Returns one of two values, true or false. Parameters are a logical expression, the true value, and the false value. If the logical expression evaluates to true, the function returns the true value. Otherwise, it returns the false value.IIF(salary>20000,'BIG','SMALL') returns BIG if salary is greater than 20000. If not, it returns SMALL. Converts a number to a character string. Parameters are the number, the total number of output characters (including the decimal point), and optionally the number of digits to the right of the decimal point.STR(12.34567,4) returns 12.STR(12.34567,4,1) returns 12.3.STR(12.34567,6,3) returns 12.346. STRVAL('Woltman') returns Woltman.STRVAL({12/25/1953}) returns 12/25/1953.STRVAL (5 * 3) returns 15.STRVAL (4 = 5) returns 'False'. NOTE: This function applies only to flat-file drivers that support SQL_TIMESTAMP: the Btrieve driver and the dBASE (access to FoxPro 3.0) driver.Converts a timestamp to a character string. An optional second parameter determines the format of the result:When set to 0 or none (the default), MM/DD/YY HH:MM:SS AM is returned.When set to 1, YYYYMMDDHHMMSS is returned, which is a suitable format for indexing.TTOC({1992-04-02 03:27:41}) returns 04/02/92 03:27:41 AM.TTOC({1992-04-02 03:27:41, 1}) returns 19920402032741 For Btrieve, the logon ID specified at connect time is returned. For all other flat-file drivers, an empty string is returned.
Table 11-5. Functions that Return Numbers MOD(10,3) returns 1. LEN('ABC') returns 3. MONTH({01/30/1989}) returns 1. DAY({01/30/1989}) returns 30. YEAR({01/30/1989}) returns 1989. MAX(66,89) returns 89. DAYOFWEEK({05/01/1995}) returns 5. MIN(66,89) returns 66. POW(7,2) returns 49. INT(6.4321) returns 6. ROUND(123.456, 0) returns 123.ROUND(123.456, 2) returns 123.46.ROUND(123.456, –2) returns 100. Converts a character string to a number. If the character string is not a valid number, a zero (0) is returned.NUMVAL('123') returns the number 123. Converts a character string to a number. If the character string is not a valid number, a zero (0) is returned.VAL('123') returns the number 123.
Table 11-6. Functions that Return Dates DATEVAL('01/30/1989') returns {01/30/1989}. Converts a character string to a date. An optional second parameter specifies the format of the character string: 0 (the default) returns MM/DD/YY, 1 returns
DD/MM/YY, and 2 returns YY/MM/DD.CTOD('01/30/1989') returns {01/30/1989}.CTOD('01/30/1989',1) returns {30/01/1989}.