Java DB

Apache Derby

Derby Reference Manual

Derby Getting Started
Derby Reference Manual
Derby Developer's Guide
Derby Performance Tuning
Derby Server and Admin Guide
Derby Tools and Utilities
Derby Reference Manual
-SQL language reference
-Capitalization and special characters
-SQL identifiers
-Rules for SQL92 identifiers
-SQL92Identifier
-column-Name
-correlation-Name
-new-table-Name
-schemaName
-Simple-column-Name
-synonym-Name
-table-Name
-view-Name
-index-Name
-constraint-Name
-cursor-Name
-TriggerName
-
-RoleName
-Interaction with the dependency system
-ALTER TABLE
-CALL (PROCEDURE)
-CREATE statements
-DECLARE GLOBAL TEMPORARY TABLE
-DELETE
-DROP statements
-GRANT
-INSERT
-LOCK TABLE
-RENAME statements
-REVOKE
-SET statements
-SELECT
-UPDATE
-SQL clauses
-SQL expressions
-JOIN operations
-SQL queries
-Built-in functions
-Standard built-in functions
-Aggregates (set functions)
-ABS or ABSVAL function
-ACOS function
-ASIN function
-ATAN function
-ATAN2 function
-AVG function
-BIGINT function
-CASE expressions
-CAST function
-CEIL or CEILING function
-CHAR function
-COALESCE function
-Concatenation operator
-COS function
-COSH function
-COT function
-COUNT function
-COUNT(*) function
-CURRENT DATE function
-CURRENT_DATE function
-CURRENT ISOLATION function
-CURRENT_ROLE function
-CURRENT SCHEMA function
-CURRENT TIME function
-CURRENT_TIME function
-CURRENT TIMESTAMP function
-CURRENT_TIMESTAMP function
-CURRENT_USER function
-DATE function
-DAY function
-DEGREES function
-DOUBLE function
-EXP function
-FLOOR function
-HOUR function
-IDENTITY_VAL_LOCAL function
-INTEGER function
-LCASE or LOWER function
-LENGTH function
-LN or LOG function
-LOG10 function
-LOCATE function
-LTRIM function
-MAX function
-MIN function
-MINUTE function
-MOD function
-MONTH function
-NULLIF expressions
-PI function
-RADIANS function
-RANDOM function
-RAND function
-ROW_NUMBER function
-RTRIM function
-SECOND function
-SESSION_USER function
-SIGN function
-SIN function
-SINH function
-SMALLINT function
-SQRT function
-SUBSTR function
-SUM function
-TAN function
-TANH function
-TIME function
-TIMESTAMP function
-TRIM function
-UCASE or UPPER function
-USER function
-VARCHAR function
-XMLEXISTS operator
-XMLPARSE operator
-XMLQUERY operator
-XMLSERIALIZE operator
-YEAR function
-Built-in system functions
-Built-in system procedures
-SYSCS_B ACKUP_D ATABASE
-SYSCS_BAC KUP_DATAB ASE_NOWAIT
-SYSCS_BACKUP_DAT ABASE_AND_ENABLE _LOG_ARCHIVE_MODE
-SYSCS_BACKUP_DATAB ASE_AND_ENABLE_LOG ARCHIVE_MODE_NOWAIT
-SYSCS_EMP TY_STATEM ENT_CACHE
-SYSCS_CH ECKPOINT _DATABASE
-SYSCS_COMPRESS_TABLE
-SYSCS_INP LACE_COMP RESS_TABLE
-SYSCS_DISA BLE_LOG_AR CHIVE_MODE
-SYSCS_EXPORT_TABLE
-SYSCS_EXPOR T_TABLE_LOB S_TO_EXTFILE
-SYSCS_EXPORT_QUERY
-SYSCS_EXPOR T_QUERY_LOB S_TO_EXTFILE
-SYSCS_IMPORT_DATA
-SYSCS_IMPOR T_DATA_LOBS FROM_EXTFILE
-SYSCS_IMPORT_TABLE
-SYSCS_IMPORT _TABLE_LOBS_ FROM_EXTFILE
-SYSCS_F REEZE_D ATABASE
-SYSCS_U NFREEZE DATABASE
-SYSCS_REL OAD_SECUR ITY_POLICY
-SYSCS_SET _DATABASE _PROPERTY
-SYSCS_SET _RUNTIMES TATISTICS
-SYSCS_SET _STATISTI CS_TIMING
-SYSCS_S ET_XPLA N_SCHEMA
-SYSCS_S ET_XPLA IN_MODE
-SYSCS_S ET_USER _ACCESS
-SYSCS_U PDATE_S ATISTICS
-SYSCS_DIAG diagnostic tables and functions
-Data types
-Argument matching
-SQL reserved words
-Derby support for SQL-92 features
-Derby system tables
-XPLAIN style tables
-Derby exception messages and SQL states
-JDBC reference
-java.sql.Driver interface
-java. sql. DriverManager. getConnection method
-java.sql.Connection interface
-java. sql. DatabaseMetaData interface
-java.sql.Statement interface
-java. sql. CallableStatement interface
-java. sql. PreparedStatement interface
-java.sql.ResultSet interface
-java. sql. ResultSetMetaData interface
-java.sql.SQLException class
-java.sql.SQLWarning class
-java.sql.Savepoint interface
-Mapping of java. sql. Types to SQL types
-JDBC Package for Connected Device Configuration/Foundation Profile (JSR 169)
-JDBC 4.0-only features
-Setting attributes for the database connection URL
-bootPassword= key attribute
-collation= collation attribute
-create= true attribute
-createFrom= path attribute
-databaseName= nameofDatabase attribute
-dataEncryption= true attribute
-drop= true attribute
-encryptionKey= key attribute
-encryptionProvider= providerName attribute
-encryptionAlgorithm= algorithm attribute
-failover= true attribute
-logDevice= logDirectoryPath attribute
-newEncryptionKey= key attribute
-newBootPassword= newPassword attribute
-password= userPassword attribute
-restoreFrom= path attribute
-rollForwardRecoveryFrom= path attribute
-securityMechanism= value attribute
-shutdown= true attribute
-slaveHost= hostname attribute
-slavePort= portValue attribute
-startMaster= true attribute
-startSlave= true attribute
-stopMaster= true attribute
-stopSlave= true attribute
-territory= ll_CC attribute
-traceDirectory= path attribute
-traceFile= path attribute
-traceFileAppend= true attribute
-traceLevel= value attribute
-upgrade= true attribute
-user= userName attribute
-ssl= sslMode attribute
-Creating a connection without specifying attributes
-Derby property reference
-Scope of Derby properties
-Dynamic and static properties
-Derby properties
-derby. authentication. builtin. algorithm
-derby. authentication. ldap. searchAuthDN
-derby. authentication. ldap. searchAuthPW
-derby. authentication. ldap. searchBase
-derby. authentication. ldap. searchFilter
-
-
-derby. connection. requireAuthentication
-derby. database. defaultConnectionMode
-derby. database. forceDatabaseLock
-
-
-
-derby. database. readOnlyAccessUsers
-derby. database. sqlAuthorization
-derby.infolog.append
-derby. jdbc. xaTransactionTimeout
-
-derby. language. logStatementText
-
-
-derby. locks. escalationThreshold
-derby.locks.monitor
-
-derby. replication. logBufferSize
-derby. replication. maxLogShippingInterval
-derby. replication. minLogShippingInterval
-
-
-derby. storage. minimumRecordSize
-
-derby. storage. pageReservedSpace
-
-
-
-
-
-
-derby. stream. error. logSeverityLevel
-derby.system.bootAll
-
-derby.system.home
-derby.user.UserName
-J2EE Compliance: Java Transaction API and javax. sql Interfaces
-Derby API
-Supported territories
-Derby limitations

 

SelectExpression

A SelectExpression is the basic SELECT-FROM-WHERE construct used to build a table value based on filtering and projecting values from other tables.

Syntax

SELECT [ DISTINCT | ALL ] SelectItem [ , SelectItem ]*
FROM clause
[ WHERE clause ]
[ GROUP BY clause ]
[ HAVING clause ]
[ ORDER BY clause ]
[ result offset clause ]
[ fetch first clause ]

SelectItem:

{
    * |
    { table-Name | correlation-Name } .* |
    Expression [AS Simple-column-Name ]
}

The SELECT clause contains a list of expressions and an optional quantifier that is applied to the results of the FROM clause and the WHERE clause. If DISTINCT is specified, only one copy of any row value is included in the result. Nulls are considered duplicates of one another for the purposes of DISTINCT. If no quantifier, or ALL, is specified, no rows are removed from the result in applying the SELECT clause (ALL is the default).

A SelectItem projects one or more result column values for a table result being constructed in a SelectExpression.

For queries that do not select a specific column from the tables involved in the SelectExpression (for example, queries that use COUNT(*)), the user must have at least one column-level SELECT privilege or table-level SELECT privilege. See GRANT statement for more information.

The result of the FROM clause is the cross product of the FROM items. The WHERE clause can further qualify this result.

The WHERE clause causes rows to be filtered from the result based on a boolean expression. Only rows for which the expression evaluates to TRUE are returned in the result.

The GROUP BY clause groups rows in the result into subsets that have matching values for one or more columns. GROUP BY clauses are typically used with aggregates.

If there is a GROUP BY clause, the SELECT clause must contain only aggregates or grouping columns. If you want to include a non-grouped column in the SELECT clause, include the column in an aggregate expression. For example:
-- List head count of each department, 
-- the department number (WORKDEPT), and the average departmental salary
-- (SALARY) for all departments in the EMPLOYEE table. 
-- Arrange the result table in ascending order by average departmental
-- salary.
SELECT COUNT(*),WORK_DEPT,AVG(SALARY)
     FROM EMPLOYEE
     GROUP BY WORK_DEPT
     ORDER BY 3

If there is no GROUP BY clause, but a SelectItem contains an aggregate not in a subquery, the query is implicitly grouped. The entire table is the single group.

The HAVING clause restricts a grouped table, specifying a search condition (much like a WHERE clause) that can refer only to grouping columns or aggregates from the current scope. The HAVING clause is applied to each group of the grouped table. If the HAVING clause evaluates to TRUE, the row is retained for further processing. If the HAVING clause evaluates to FALSE or NULL, the row is discarded. If there is a HAVING clause but no GROUP BY, the table is implicitly grouped into one group for the entire table.

The ORDER BY clause allows you to specify the order in which rows appear in the result set. In subqueries, the ORDER BY clause is meaningless unless it is accompanied by one or both of the result offset and fetch first clauses or in conjunction with the ROW_NUMBER function.

The result offset clause provides a way to skip the N first rows in a result set before starting to return any rows. The fetch first clause, which can be combined with the result offset clause if desired, limits the number of rows returned in the result set.

Derby processes a SelectExpression in the following order:
  • FROM clause
  • WHERE clause
  • GROUP BY (or implicit GROUP BY)
  • HAVING clause
  • ORDER BY clause
  • Result offset clause
  • Fetch first clause
  • SELECT clause

The result of a SelectExpression is always a table.

When a query does not have a FROM clause (when you are constructing a value, not getting data out of a table), you use a VALUES expression, not a SelectExpression. For example:
VALUES CURRENT_TIMESTAMP

See VALUES expression.

The * wildcard

* expands to all columns in the tables in the associated FROM clause.

table-Name.* and correlation-Name.* expand to all columns in the identified table. That table must be listed in the associated FROM clause.

Naming columns

You can name a SelectItem column using the AS clause. If a column of a SelectItem is not a simple ColumnReference expression or named with an AS clause, it is given a generated unique name.

These column names are useful in several cases:
  • They are made available on the JDBC ResultSetMetaData.
  • They are used as the names of the columns in the resulting table when the SelectExpression is used as a table subquery in a FROM clause.
  • They are used in the ORDER BY clause as the column names available for sorting.

Examples

-- This example shows SELECT-FROM-WHERE
-- with an ORDER BY clause
-- and correlation-Names for the tables.
SELECT CONSTRAINTNAME, COLUMNNAME 
    FROM SYS.SYSTABLES t, SYS.SYSCOLUMNS col,
    SYS.SYSCONSTRAINTS cons, SYS.SYSCHECKS checks 
  WHERE t.TABLENAME = 'FLIGHTS' 
    AND t.TABLEID = col.REFERENCEID 
    AND t.TABLEID = cons.TABLEID 
    AND cons.CONSTRAINTID = checks.CONSTRAINTID 
  ORDER BY CONSTRAINTNAME
-- This example shows the use of the DISTINCT clause
SELECT DISTINCT ACTNO
    FROM EMP_ACT
-- This example shows how to rename an expression
-- Using the EMPLOYEE table, list the department number (WORKDEPT) and 
-- maximum departmental salary (SALARY) renamed as BOSS 
-- for all departments whose maximum salary is less than the 
-- average salary in all other departments.
SELECT WORKDEPT AS DPT, MAX(SALARY) AS BOSS 
    FROM EMPLOYEE EMP_COR 
    GROUP BY WORKDEPT 
    HAVING MAX(SALARY) < (SELECT AVG(SALARY)
                          FROM EMPLOYEE
                          WHERE NOT WORKDEPT = EMP_COR.WORKDEPT) 
    ORDER BY BOSS
 

javadb@jdbcurl.com