interactive-select-statement >>-| fullselect |--+----------------------------------------------+-> | .-,-----------------------------. | | V .-ASC--. | | '-ORDER BY------+-column_name-+---+------+--+--' '-integer-----' '-DESC-' >----+---------------+----------------------------------------->< '-WITH--+-RR-+--' +-CS-+ '-UR-' fullselect |--+-subselect----+---------------------------------------------> '-(fullselect)-' .------------------------------------------. V | >--------+------------------------------------+--+--------------| '--+-UNION-----+---+-subselect----+--' '-UNION ALL-' '-(fullselect)-' subselect .-ALL------. |--SELECT----+----------+---+-*----------------------------+----> '-DISTINCT-' | .-,-----------------------. | | V | | '----+-expression---------+--+-' +-table_name.*-------+ +-view_name.*--------+ '-correlation_name.*-' .-,-----------------------------------------. V | >----FROM------+-table_name-+---+------------------+---+--------> '-view_name--' '-correlation_name-' >-----+--------------------------+------------------------------> '-WHERE--search_condition--' >-----+-------------------------------+-------------------------> | .-,--------------. | | V | | '-GROUP BY-----column_name---+--' >-----+---------------------------+-----------------------------| '-HAVING--search_condition--' |
The interactive-select statement retrieves data from a table or view. The data retrieved can only be viewed; you cannot change it. For more information about the terminology used in this diagram, refer to the DB2 Server for VSE & VM SQL Reference manual.
If a SELECT statement contains a placeholder in the WHERE, GROUP BY, ORDER BY, or HAVING clauses, FORMAT information is saved until the next SELECT statement is entered. Formatting information is saved permanently if the SELECT statement is stored. However, if the SELECT or FROM clauses contain placeholders or parameters, FORMAT information is not saved. In addition, FORMAT information is not saved if you change any data in the SELECT or FROM clauses by using the CHANGE command.
When you enter a SELECT statement from a terminal, the row length of the information you can see at one time is limited by the terminal screen size.
The number of bytes per row includes the bytes used as column separators.
select distinct deptno from project
You can also use it to eliminate duplicates from a column function:
select count (distinct deptno) from project
If you specify DISTINCT, the maximum number of columns you can put in the expression list is 16. In addition, the length of the encoded key derived from the expression list must not exceed 255 characters. That is, the sum of the lengths of the columns in the expression list, plus approximately 25% of the lengths of those columns that are of varying-length character type, must not exceed 255 EBCDIC or 127 DBCS characters.
Note: | If the table contains more than 45 columns, only the first 45 columns will be displayed. To see the other columns in the table, you can create a view. A view may contain no more than 140 columns. If more columns are needed, you can create additional views. |
You can specify a list of expressions, separating each with a comma, and the items are retrieved in the same left-to-right order as they appear in the list. The value of USER is interpreted as a CHAR(8) string whose value is the user ID of the user currently connected. The maximum number of columns that you can specify in the list is 45.
You can use the following operators to connect numeric data types:
If you use these operators for numeric data types, see the DB2 Server for VSE & VM SQL Reference manual for information about data conversion.
You can use the concatenation operator, CONCAT, to join two or more compatible operands to form a string. An operand may be a column, a name, a constant, or an expression.
An operand can be the result of an expression. For example, if the USER special register is used, it is treated as CHAR(8). If the CURRENT DATE, CURRENT TIME, CURRENT TIMESTAMP, or CURRENT TIMEZONE special registers are used, the values are treated as the character representation of the value in the format defined by the system. A datetime value can be concatenated with a character string because the datetime data types are compatible with character data types.
When varying-length operands are concatenated, only the actual length of the operand is concatenated.
The result of the expression is the concatenation of the operand expressions. The resulting data type is null if either operand is nullable. The resulting data type is character if both operands are CHARACTER. If both operands are GRAPHIC, the result data type is GRAPHIC. If both operands are fixed length, the resulting data type is fixed length. If either operand is varying length, the resulting data type is a varying length string. The associated defined length is the sum of the defined lengths not exceeding 254 bytes.
In the following example, the employee number and the first name of the employee are concatenated with a hyphen between them:
select empno concat '-' concat firstnme from employee
If a column function is used in an expression, all expressions in the list must contain a column function unless grouping is being performed. An example is:
select min(edlevel),avg(bonus) from employee
You can further qualify the table or view by specifying the owner of the table or view. You must separate the owner's name from the table name or view name with a period. The owner's name is unnecessary for tables or views that you own. You must have the SELECT privilege or DBA authority to select information from tables or views owned by other users.
AVG, MAX, MIN, and COUNT applied to groups of rows that have matching values in a column. Rows can also be grouped by matching values in more than one column. The definition of the groups is specified with a GROUP BY clause.
For example, the maximum, minimum, and average activity staff for each project in the PROJ_ACT table could be selected by the following query:
select projno,max(acstaff),min(acstaff),avg(acstaff) - from proj_act - group by projno
When a query uses the grouping feature, it returns only one result row for each group. Therefore, the items selected by such a query must be properties of the groups, not properties of individual rows. The expression list may contain columns that are also in the GROUP BY clause together with column functions on any columns. It may not contain any non-grouped column without a column function. If the column function COUNT(*) is used, it evaluates to the number of rows in the group.
If any rows have a null value in a grouped column, ISQL groups the null values in those columns together. The null values returned may be due either to unknown column values or to arithmetic exception errors.
A grouping query may have a standard WHERE clause that serves as a filter, keeping only those rows which satisfy the search_condition. The WHERE clause filters out the non-qualifying rows before the groups are formed and the column functions are computed.
The following example query finds the average and minimum activity staff for each project, considering only activities whose starting date is 1 January 1982:
select projno,avg(acstaff),min(acstaff) - from proj_act - where acstdate = '1982-01-01' - group by projno
The following example query lists the maximum and minimum activity staff for various projects in the PROJ_ACT table, considering only projects that have more than three activities:
select projno,max(acstaff),min(acstaff) - from proj_act - group by projno - having count(*) > 3
One of the functions in a HAVING clause may specify DISTINCT (for example, COUNT(DISTINCT PROJNO)). However, DISTINCT may be used only once in a query. It may not be used in both the expression list and the HAVING clause.
It is possible, though unusual, for a query to contain a HAVING clause but no GROUP BY clause. In this case, the entire table is treated as one group. If the HAVING condition is true for the table as a whole, the selected result, which must consist entirely of column functions, is returned.
select actno,projno,acstdate,acendate - from proj_act - order by actno,projno
select projno,acstdate,acendate,acstaff - from proj_act - order by 1,3
These items may be columns or more complex expressions such as BONUS+COMM.
order by 3,5 desc
because ASC is the default.
In contrast, to indicate descending order on item 3 and item 5, you would type:
order by 3 desc,5 desc
Blanks sort first in ascending order, last in descending order, and are ignored if the data type is VARCHAR or VARGRAPHIC.