Operators of SQL
Operators of SQL
An operator manipulates individual data items and returns a result. The data items are called operands or arguments. Operators are represented by special characters or by keywords. For example, the multiplication operator is represented by an asterisk (*) and the operator that tests for nulls is represented by the keywords IS NULL. There are two general classes of operators: unary and binary. Oracle Lite SQL also supports set operators.
Unary Operators
A unary operator uses only one operand. A unary operator typically appears with its operand in the following format:
operator operand
Binary Operators
A binary operator uses two operands. A binary operator appears with its operands in the following format:
operand1 operator operand2
Set Operators
Set operators combine sets of rows returned by queries, instead of individual data items. All set operators have equal precedence. Oracle Lite supports the following set operators:
The following lists the levels of precedence among the Oracle Lite SQL operators from high to low. Operators listed on the same line have the same level of precedence: Table of Levels of Precedence of the Oracle Lite SQL Operators
Precedence Level 1 SQL Operator Unary + - arithmetic operators, PRIOR operator
SQL Operator * / arithmetic operators Binary + - arithmetic operators, || character operators All comparison operators NOT logical operator AND logical operator OR logical operator
Other operators with special formats accept more than two operands. If an operator receives a null operator, the result is always null. The only operator that does not follow this rule is CONCAT.
Arithmetic Operators
Arithmetic operators manipulate numeric operands. The - operator is also used in date arithmetic. Table of Arithmetic Operators
Operator + (unary) - (unary) / * + Description Makes operand positive Negates operand Division (numbers and dates) Multiplication Addition (numbers and dates) Subtraction (numbers and dates) Example
SELECT +3 FROM DUAL; SELECT -4 FROM DUAL; SELECT SAL / 10 FROM EMP; SELECT SAL * 5 FROM EMP; SELECT SAL + 200 FROM EMP; SELECT SAL - 100 FROM EMP;
Character Operators
Character operators are used in expressions to manipulate character strings. Table of Character Operators
Operator Description || Example
Concatenates character SELECT 'The Name of the employee is: ' || ENAME FROM EMP; strings
With Oracle Lite, you can concatenate character strings with the following results:
Concatenating two character strings results in another character string. Oracle Lite preserves trailing blanks in character strings by concatenation, regardless of the strings' datatypes. Oracle Lite provides the CONCAT character function as an alternative to the vertical bar operator. For example:
SELECT ENAME|| ' is a '||job FROM EMP WHERE SAL > 2000;
This returns:
KING BLAKE CLARK JONES FORD SCOTT is is is is is is a a a a a a PRESIDENT MANAGER MANAGER MANAGER ANALYST ANALYST
6 rows selected.
Oracle Lite treats zero-length character strings as nulls. When you concatenate a zero-length character string with another operand the result is always the other operand. A null value can only result from the concatenation of two null strings.
Comparison Operators
Comparison operators are used in conditions that compare one expression with another. The result of a comparison can be TRUE, FALSE, or UNKNOWN. Table of Comparison Operators
Operator = Description Equality test. Example
SELECT ENAME "Employee" FROM EMP WHERE SAL = 1500; SELECT ENAME FROM EMP WHERE SAL ^= 5000; SELECT ENAME "Employee", JOB "Title" FROM EMP WHERE SAL > 3000; SELECT * FROM PRICE WHERE MINPRICE < 30; SELECT * FROM PRICE WHERE MINPRICE >= 20; SELECT ENAME FROM EMP WHERE SAL <= 1500; SELECT * FROM EMP WHERE ENAME IN ('SMITH', 'WARD'); SELECT * FROM DEPT WHERE LOC = SOME ('NEW YORK','DALLAS');
>
<
>=
<=
IN
ANY/ SOME Compares a value to each value in a list or returned by a query. Must be preceded by =, !=, >, <, <=, or >=. Evaluates to FALSE if the query returns no rows. NOT IN Equivalent to "!= ANY". Evaluates to FALSE if any member of the set is NULL. Compares a value with every value in a list
SELECT * FROM DEPT WHERE LOC NOT IN ('NEW YORK', 'DALLAS'); SELECT * FROM emp WHERE
ALL
Operator
Description
Example
or returned by a query. Must be preceded by sal >= ALL (1400, 3000); =, !=, >, <, <=, or >=. Evaluates to TRUE if the query returns no rows. [NOT] [Not] greater than or equal to x and less BETWEEN x than or equal to y. and y EXISTS
SELECT ENAME, JOB FROM EMP WHERE SAL BETWEEN 3000 AND 5000;
TRUE if a sub-query returns at least one row. SELECT * FROM EMP WHERE
EXISTS (SELECT ENAME FROM EMP WHERE MGR IS NULL);
TRUE if x does [not] match the pattern y. Within y, the character "%" matches any string of zero or more characters except null. The character "_" matches any single character. Any character following ESCAPE is interpretted litteraly, useful when y contains a percent (%) or underscore (_).
IS [NOT] NULL
Tests for nulls. This is the only operator that SELECT * FROM EMP WHERE COMM IS NOT NULL AND SAL should be used to test for nulls.
> 1500;
Logical Operators
Logical operators manipulate the results of conditions. Table of Logical Operators
Operator Description NOT Returns TRUE if the following condition is FALSE. Returns FALSE if it is TRUE. If it is UNKNOWN, it remains UNKNOWN. Example
SELECT * FROM EMP WHERE NOT (job IS NULL) SELECT * FROM EMP WHERE NOT (sal BETWEEN 1000 AND 2000) SELECT * FROM EMP WHERE
AND
Operator Description are TRUE. Returns FALSE if either is FALSE; otherwise returns UNKNOWN. OR Returns TRUE if either component condition is TRUE. Returns FALSE if both are FALSE. Otherwise, returns UNKNOWN.
Example
job='CLERK' AND deptno=10
Set Operators
Set operators combine the results of two queries into a single result. Table of Set Operators
Operator UNION Description Returns all distinct rows selected by either query. Example
SELECT * FROM (SELECT ENAME JOB = 'CLERK' UNION SELECT ENAME FROM JOB = 'ANALYST'); EMP WHERE FROM EMP WHERE
UNION ALL
SELECT * FROM (SELECT SAL FROM EMP WHERE JOB = 'CLERK' UNION SELECT SAL FROM EMP WHERE JOB = 'ANALYST');
MINUS
SELECT * FROM (SELECT SAL FROM Returns all distinct rows selected by the first query but EMP WHERE JOB = 'PRESIDENT'
Operator
Example
The syntax for INTERSECT ALL is supported, but it returns the same results as INTERSECT.
Other Operators:
Some other in-built operators include (+) which is used in outer join operations, PRIOR used in tree structure queries The following are the lists of other operators: Table of Other Operators:
Operator Description (+) Indicates that the preceding column is the outer join column in a join. Example
SELECT ENAME, DNAME FROM EMP, DEPT WHERE DEPT.DEPTNO = EMP.DEPTNO (+);
PRIOR
Evaluates the following expression for the parent SELECT EMPNO, ENAME, MGR FROM EMP CONNECT row of the current row in a hierarchical, or treeBY PRIOR EMPNO = MGR; structured query. In such a query, you must use this operator in the CONNECT BY clause to define the relationship between the parent and child rows.