SQL Operators
What are SQL operators?
An operator performs on separate data items and returns a result. The data items are called operands or
arguments. Operators are mentioned by special characters or by keywords.
For example , the plus operator is represented by a plus (+) sign and the operator that checks for nulls are
represented by the keywords IS NULL or IS NOT NULL.
Types of SQL operators
SQL operators Description
SQL Arithmetic Arithmetic operators can perform arithmetical operations on numeric operands involved.
Operator Arithmetic operators are addition(+), subtraction(-), multiplication(*) and division(/). The +
and - operators can also be used in date arithmetic.
SQL A comparison (or relational) operator is a mathematical symbol which is used to compare
Comparison two values. The result of a comparison can be TRUE, FALSE, or UNKNOWN.
Operator
SQL Assignment In SQL the assignment operator ( = ) assigns a value to a variable or of a column or field of a
operator table. Also the keyword AS may be used as an operator for assigning table or column-
heading aliases.
SQL Bitwise The Bitwise operators perform bit manipulations between two integer expressions of the
Operator integer data type category.
The bitwise operators are & ( Bitwise AND ),
| ( Bitwise OR ) and
^ ( Bitwise Exclusive OR or XOR ).
The valid datatypes for bitwise operators are BINARY, BIT, INT, SMALLINT, TINYINT,
and VARBINARY.
SQL Logical The Logical operators are those that are true or false. They return a true or false value to
Operator combine one or more true or false values. The logical operators are
AND , OR, NOT, IN, BETWEEN, ANY, ALL, SOME, EXISTS and LIKE.
SQL Unary The SQL Unary operators perform such an operation which contain only one expression of
Operator any of the data types in the numeric data type category.
Unary operators may be used on any numeric data type, though the bitwise operator (~) may
be used only on integer data types.
The Unary operator ( + ) means the numeric value is positive,
the ( - ) means the numeric value is negative,
the ( ~ ) means a bitwise NOT; returns the complement of the number ( except in Oracle )
Arithmetic Operators
SQL plus (+) operator
The SQL plus (+) operator is used to add two or more expressions or numbers.
Example:
Sample table: customer
Ex-1) To get data of 'cust_name', 'opening_amt', 'receive_amt', ('opening_amt' + 'receive_amt') from the
'customer' table with the condition that sum of 'opening_amt' and 'receive_amt' is greater than 15000,the
following SQL statement can be used :
SELECT cust_name, opening_amt, receive_amt, (opening_amt + receive_amt)
FROM customer
WHERE (opening_amt + receive_amt)>15000;
SQL minus (-) operator
The SQL minus (-) operator is used to subtract one expression or number from another expression or
number.
Ex-2) To get data of 'cust_name', 'opening_amount', 'payment_amount' and 'oustanding_amount' from the
'customer' table with the condition that 'outstanding_amt' - 'payment_amt' is equal to the 'receive_amt'.
SELECT cust_name,opening_amt, payment_amt, outstanding_amt
FROM customer
WHERE(outstanding_amt-payment_amt)=receive_amt;
Logical Operators
The Logical operators are those that are true or false. They return a true or false values to combine one or
more true or false values.
The Logical operators are:
AND OR NOT IN BETWEEN ANY
ALL SOME EXISTS
IN operator
The IN operator checks a value within a set of values separated by commas and retrieve the rows from the
table which are matching.
The IN returns 1 when the search value present within the range otherwise returns 0.
Ex-) Here we look for all agents in the agents table of inventory database who have a working area of the
state of 'London', 'Mumbai' or 'Chennai'.
SQL Code:
SELECT *FROM agents
WHERE working_area IN('London','Mumbai','Chennai');
This statement can also be used like bellow:
SELECT *
FROM agents
WHERE working_area='London' OR working_area='Mumbai' OR working_area='Chennai';
Ex-)To get data of all columns from the 'agents' table with the condition that 'commission' for the agents
will be any of .13, .14 and .12, the following SQL statement can be used :
SQL Code:
SELECT * FROM agents
WHERE commission IN (.13,.14,.12);
SQL IN operator with boolean NOT
In the following example, we have discussed the usage of IN operator with the boolean operator NOT in a
select statement.
Ex: To get data of all columns from the 'agents' table with the condition that 'commission' for the agents
will be none of .13, .14, .12, the following SQL statement can be used:
Sample table: agents
SQL Code:
SELECT *
FROM agents
WHERE commission NOT IN (.13,.14,.12);
BETWEEN Operator
The SQL BETWEEN operator tests an expression against a range. The range consists of a beginning,
followed by an AND keyword and an end expression.
The operator returns TRUE when the search value present within the range otherwise returns FALSE.
The results are NULL if any of the range values are NULL.
Ex-) Find all agents in the agents table of inventory database whose commission should be within .12 to
.14.
Sample table: agents
SQL Code:
SELECT * FROM agents
WHERE commission BETWEEN .12 AND .14;
Ex-) To get data of all columns from the 'customer' table with following conditions -
1. 'agent_code' must be within 'A003' and 'A008',
2. but 'agent_code' 'A004', 'A007' and 'A005' should not appear, the following SQL statement can be used
SQL Code:
SELECT agent_code, cust_code, cust_name, cust_city
FROM customer
WHERE (agent_code BETWEEN 'A003' AND 'A008')
AND agent_code NOT IN ('A004','A007','A005');
Ex-) To get data of 'cust_code', 'cust_name', 'cust_city', 'working_area' columns from the 'customer' table
with the condition that 'cust_name' must begin with any of the letters between 'A' and 'I'.
SQL Code:
SELECT cust_code, cust_name, cust_city, working_area
FROM customer
WHERE cust_name BETWEEN 'A%' AND 'I%’;
SQL Between operator with boolean NOT
Sample table :customer
Ex-) To get data of 'cust_code', 'cust_name', 'cust_city', 'working_area' columns from the 'customer' table
with the condition that 'cust_name' must not begin with any of the letters between 'K' and 'Y' .
SELECT cust_code, cust_name, cust_city, working_area
FROM customer
WHERE cust_name NOT BETWEEN 'K' AND 'Y';
SQL Between operator with NOT on date value
Sample table: orders
Ex-)To get data of 'ord_num', 'ord_amount', 'advance_amount', 'ord_date', 'cust_code' and 'agent_code'
from the 'orders' table with the condition that 'ord_date' must be a date before '15-Feb-08' or after '30-Jul-
08', the following SQL statement can be used :
SQL Code:
SELECT ord_num,ord_amount,ord_date, cust_code,agent_code
FROM orders
WHERE ord_date NOT BETWEEN '15-Feb-08' AND '30-Jul-08';
ANY Operator
ANY compares a value to each value in a list or results from a query and evaluates to true if the result of
an inner query contains at least one row. ANY must be preceded by comparison operators.
Syntax:
SELECT [column_name... | expression1 ]
FROM [table_name]
WHERE expression2 comparison_operator {ALL | ANY | SOME}(
subquery )
comparison_operator Compares the expression to the subquery. The comparison must be a standard
comparison operator (=, <>, !=, >, >=, <, or <=).
a) value>ANY(minimum value from list of values)
means if value is not greater than minimum value then it cannot be greater than rest of the values
from list of values.
b) Value<ANY(maximum value from list of values)
means if value is not less than maximum value then it cannot be less than rest of the values from
list of values.
Ex-) To get 'agent_code', 'agent_name', 'working_area', 'commission' from 'agents' table the following
conditions that
1. 'agent_code' should be any 'agent_code' from 'customer' table, which satisfies the condition bellow :
a) 'cust_country' in the 'customer' table must be 'UK',
SELECT agent_code,agent_name,working_area,commission
FROM agents
WHERE agent_code=ANY(SELECT agent_code FROM customer
WHERE cust_country='UK');
Ex-)To get 'agent_code', 'agent_name', 'working_area', 'commission' from 'agents' table with following
conditions -
1. 'agent_code' should be any 'agent_code' from 'customer' table, which satisfies the condition bellow :
a) 'agent_code' should be any 'agent_code' from 'orders' table, which satisfies the condition bellow :
i) 'advance_amount' of 'orders' table must be more than 600,
SELECT agent_code,agent_name,working_area,commission
FROM agents
WHERE agent_code=ANY(SELECT agent_code FROM customer
WHERE agent_code =ANY(SELECT agent_code FROM orders
WHERE advance_amount>600));
ALL Operator
ALL is used to select all records of a SELECT STATEMENT. It compares a value to every value in a list
or results from a query.
comparison_operator Compares the expression to the subquery. The comparison must be a standard
comparison operator (=, <>, !=, >, >=, <, or <=.
SOME Operator
SOME compare a value to each value in a list or results from a query and evaluate to true if the result of
an inner query contains at least one row.
SOME must match at least one row in the subquery and must be preceded by comparison operators.
comparison_operator Compares the expression to the subquery. The comparison must be a standard
comparison operator (=, <>, !=, >, >=, <, or <=).
Ex-) To get 'agent_code', 'agent_name', 'working_area', 'commission' from 'agents' table with following
conditions -
1. 'agent_code' should be within some 'agent_code' from 'customer' table, which satisfies the condition
bellow:
a) 'cust_country' in the 'customer' table must be 'UK',
SQL Code:
SELECT agent_code,agent_name,working_area,commission
FROM agents
WHERE agent_code=SOME(
SELECT agent_code FROM customer
WHERE cust_country='UK');
EXISTS Operator
The EXISTS checks the existence of a result of a Subquery. The EXISTS subquery tests whether a
subquery fetches at least one row.
When no data is returned then this operator returns 'FALSE'.
A valid EXISTS subquery must contain an outer reference and it must be a correlated Subquery.
The select list in the EXISTS subquery is not actually used in evaluating the EXISTS so it can contain
any valid select list.
Syntax:
SELECT [column_name... | expression1 ]
FROM [table_name]
WHERE [NOT] EXISTS (subquery)
WHERE [NOT] Tests the subquery for the existence of one or more rows. If a single row satisfies the
EXISTS subquery clause, it returns Boolean TRUE.
When the subquery returns no matching rows the optional NOT keyword returns a
Boolean TRUE.
Ex-) To get 'agent_code','agent_name','working_area' and 'commission' from the 'agents', with following
conditions-
1. 'grade' in 'customer' table must be 3,
2. 'agent_code' in 'customer' and 'agents' table must match,
3. 'commission' of 'agents' should arrange in ascending order,
4. the above condition (1) and (2) should match at least one row,
SELECT agent_code,agent_name,working_area,commission
FROM agents WHERE exists (SELECT * FROM customer WHERE grade=3
AND agents.agent_code=customer.agent_code)
ORDER BY commission;
NOTE: IT can be used with SELECT, INSERT, UPDATE and DELETE command.
SQL NOT Exists can be used also.
Ex-) Write a query to display the department name which does not have any employee.
SQL Code:
SELECT Department_name from department
where department_id
NOTEXISTS (SELECT 1 from employees
where employee.department_id=department.department_id
SQL Wildcards & Like operators
LIKE Operator
LIKE is the ANSI/ISO standard operator for comparing a column value to another column value, or to a
quoted string. Returns either 1 (TRUE) or 0 (FALSE).
The SQL LIKE operator is only applied on a field of types CHAR or VARCHAR to match a
pattern.
To match a pattern from a word, special characters, and wildcards characters may have used with LIKE
operator.
The LIKE operator can be used within any valid SQL statement, such as SELECT, INSERT
INTO, UPDATE or DELETE.
SQL Wildcards:
The SQL wildcards can be used to search data within a table.SQL wildcards are used with SQL LIKE
operator.
The boolean NOT operator in the select statement can be used as wildcard NOT LIKE operator.
In SQL, the wildcards are:
Wildcards Description
% The percent sign character (%) represent a sequence of 0 (zero) or more characters.
Underscore ( _ ) The underscore character ( _ ) represents a single character.
[charlist] It represents any single character within a charlist
[^charlist] or [!charlist] It represents any single character other than the charlist
SQL wildcards percentage (%)
Sample table: customer
Ex-) To get the 'cust_code', 'cust_name', 'cust_city' and 'cust_country' from the table 'customer' where
the 'cust_name' must begin with the letter 'S',
SELECT cust_code,cust_name,cust_city,cust_country
FROM customer WHERE cust_name LIKE 'S%';
SQL wildcards percentage (%) with boolean NOT
Sample table: agents
Ex-) To get all the columns from the table 'agents' with the condition that the 'agent_name' not initiated
with the letter 'M',
SELECT *
FROM agents WHERE agent_name NOT LIKE 'M%';
Example : SQL wildcards underscore ( _ )
Sample table : customer
Ex-) To get 'cust_code', 'cust_name', 'cust_city' and 'cust_country' from the table 'customer' with
following conditions -
1. the 'cust_name' must initiate with the letter 'R',
2. the third letter of 'cust_name' must be 'm',
3. and the second letter of 'cust_name' may be any,
SELECT cust_code,cust_name,cust_city,cust_country
FROM customer WHERE cust_name LIKE 'R_m%';
SQL wildcards underscore ( _ ) multiple characters
Sample table : customer
Ex-) To get 'cust_code', 'cust_name', 'cust_city' and 'cust_country' from the table 'customer' with
following conditions -
1. the first three letters of 'cust_name' may be any letter
2. the forth letter of 'cust_name' must be 'l'
3. and the rest may be any
SELECT cust_code,cust_name,cust_city,cust_country
FROM customer WHERE cust_name LIKE '___l%';
SQL wildcards underscore ( _ ) for specific length
Sample table: customer
Ex-) To get 'cust_code', 'cust_name', 'cust_city' and 'cust_country' from the table 'customer' with
following conditions -
1. the first three letters of 'cust_name' may be any letter
2. the forth letter of 'cust_name' must be 'l'
3. and the string must be a length of 4 letters
SELECT cust_code,cust_name,cust_city,cust_country
FROM customer WHERE cust_name LIKE ‘___l';
Example: SQL [charlist] wildcards
Sample table : agents
Ex-) To get all rows from the table 'agents' with the condition that the 'agent_name' must begin with the
letter 'a' or 'b' or 'i'
SELECT *
FROM agents WHERE agent_name LIKE '[abi]%';
Example: SQL [^charlist] or SQL [!charlist] wildcards
Sample table: agents
Ex-) To get all rows from the table 'agents' with the condition that the 'agent_name' must not begin with
the letter 'a' or 'b' or 'i',
SELECT *
FROM agents WHERE agent_name LIKE '[^abi]%';