Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

Predicates:

Predicates, also referred to as logical predicates, are types of


conditional expressions. They specify a condition of a row or group that has
one of three possible states:
 TRUE
 FALSE
 NULL (or unknown)
Predicates can appear in the following clauses in SQL:
 WHERE, ON, or HAVING clause to qualify or disqualify rows in a SELECT
statement.
 WHEN clause search condition of a searched CASE expression.
 CASE_N function.
 IF, WHILE, REPEAT, and CASE statements in stored procedures.
Types of Logical Predicates
SQL provides the following logical predicates:
 Comparison operators
 [NOT] BETWEEN
 LIKE
 [NOT] IN
 [NOT] EXISTS
 OVERLAPS
 IS [NOT] NULL
Logical Operators that Operate on Predicates
 NOT
 AND
 OR
Predicate Quantifiers
 SOME
 ANY
 ALL
Examples of Predicates

Predicate Description

SELECT * Predicate in a WHERE clause specifying a condition for


FROM Employee selecting rows from the Employee table.
WHERE Salary <
40000;

SELECT
SUM(CASE
  WHEN part
BETWEEN 100 Predicate in a CASE expression specifying a condition
AND 199 that determines the value passed to the SUM function
  THEN 0 for a particular row in the Orders table.
  ELSE cost
  END)
FROM Orders;

SQL Operator:
Operators are the foundation of any programming language.
We can define operators as a reserved word or character or symbol which is
used primarily in a SQL WHERE clause to specify conditions in a SQL SELECT
UPDATE and DELETE statements by performing any kind of operations such
as logical, comparison and Arithmetic operations (query a database/tables.) on
databases/tables. Operators act as a connector between two or more
conditions (for various conditional statements). The operator manipulates the
data and gives the result based on the operator’s functionality.
https://minigranth.in/sql-tutorial/sql-operators
Types of SQL operators:
SQL provides three types of operations that can be
performed on the database/tables according to the need. These operators are
namely :
 Arithmetic Operators.
 Comparison Operators.
 Logical Operators.
Arithmetic operators: The SQL Arithmetic operator is used to perform
arithmetic computations and operations on the two operands or the numerical
data present in the tables of the database.
The Arithmetic operators are capable of performing all arithmetic operations
like addition, subtraction, multiplication, division, and modulus on the
operands of the operator.

Operator Description
+ Addition operator - it is used to perform addition operations on
the operands.
- Subtraction operator – he Subtraction operator is used to
perform subtraction operations on the operands (to get the
difference between operands).
* Multiplication operator – The Multiplication operator is used to
perform multiplication operations on the operands.
/ Division operator - The Division operator is used to perform
division operations on the operands.
% Modulus operator - The Modulus operator is used to perform
modulus operation on the operands (to get the remainder when
one operand is divided by the other).
Comparison operators: The SQL Comparison operator is used to compare the
two operands or the two data values present in the database tables. The
comparison operators are also capable of comparing one expression to the
other expression. Some of the comparison operators are:
Operator Description
Equals operator - The Equal operator is used to show data that
=
matches with the provided value in the query.
Greater Than operator - The Greater Than operator is used to
>
show data that are greater than the provided value in the query.
Less Than operator - The Lesser Than operator is used to show
<
data that are lesser than the provided value in the query.
Greater Than or Equal To operator - The Greater Than or

>= Equals To operator is used to show data that are greater than
and equal to the provided value in the query.
Less Than or Equal To operator - The Lesser Than or Equals

<= To operator is used to show data that are lesser than and equal
to the provided value in the query.
Not Equal To operator - The Not Equal operator is used to show
<> or !=
data that do not match with the provided value in the query.
!< Not Less Than operator - Compares two expressions and returns
TRUE if the left operand does not have a value lower than the right
operand; otherwise, the result is FALSE.
!> Not Greater Than operator - Compares two expressions and
returns TRUE if the left operand doesn’t have a greater value than
the right operand; otherwise, the result is FALSE.
Bit-wise Operators Bit-wise operators are used to perform bit-wise
operations (0 & 1) over the expressions which includes :

Logical Operators: The SQL Logical operator is used to perform Boolean


(TRUE or FALSE) operations on the operands or the two data values present in
the database tables. The Logical operators return true if both the operands
follow the logical condition. Logical SQL operators are used between queries
or used to join two or more conditions or used to extract some specific
values.
The common logical operators present in SQL:
Operator Description
The Logical AND operator is used to compare two operands, it
AND returns TRUE when both the operands follow the logical condition
provided in the SQL query.
The Logical OR operator is used to compare two operands, it
OR returns TRUE when any of the two operands follow the logical
condition provided in the SQL query.
The Logical NOT operator has been used to change the value
operand. If the value of the operand is True, it changes the value to
NOT
False. If the value of the operand is False, it changes the value to
True.
Apart from the AND, OR, and NOT logical operators, we have some special
logical operators. The special logical operators are used to select records from
the table. The special logical operators are commonly used inside
the WHERE clause or with the HAVING statement.
The special logical operators present in SQL:
Operator Description
The ALL operator compares the provided value to all the values
ALL of a column returned from the sub-query. It selects all the
records of an inner SELECT statement.
The ANY operator returns records, when any one of the values
ANY returned from the sub-query, satisfy the provided condition. It
must match with at least one record of the inner query.
The BETWEEN operator is used to retrieve records within the
provided range. BETWEEN operator can work with numbers,
BETWEEN
characters, dates, and times. The range is specified using
another logical operator AND.
The IN operator is used to retrieve records that match the set of
IN values separated by commas. We can use multiple OR
statements in the place of IN operator.
The EXISTS operator is used to check if the specified value exists
EXISTS in the result of the inner sub-query or not. It returns True or
False based on the results obtained.
The LIKE operator is used to retrieve those records from a table
that matches the provided pattern. There are two wild cards %
LIKE (percentage) and _ (underscore) that are often used with the LIKE
operator. The % sign represents 0 or more characters; _ sign
represents a single character.
Same as ANY. Returns TRUE when any of the subquery values
SOME
meet the condition.
In case of sub-queries, Operators like “ALL”, “ANY”, “EXISTS”
and “SOME” are used.
https://csveda.com/structured-query-language/sql-type-of-constraints-in-oracle/

SQL Statements
Like any programming language, SQL uses statements. A statement is a
specific instruction for the computer to do something.
For example: SQL contains the CREATE statement. As you can probably
guess, this statement is used to create a new database or a new table or a new
user.
There are many different kinds of SQL statements. In the following, we'll focus
on just one of them: the SELECT statement.

Consider the sample table ‘emp’

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7839 KING PRESIDENT – 17-NOV-81 5000 – 10

7698 BLAKE MANAGER 7839 01-MAY-81 2850 500 30

7782 CLARK MANAGER 7839 09-JUN-81 2450 500 10

7566 JONES MANAGER 7839 02-APR-81 2975 – 20

7788 SCOTT ANALYST 7566 19-APR-87 3000 – 20


7902 FORD ANALYST 7566 03-DEC-81 3000 – 20

7369 SMITH CLERK 7902 17-DEC-80 800 500 20

Example

select empno,ename,sal,sal*12 as annsal,sal/12 as comm


from emp;
O/P

7 rows selected.
EMPNO ENAME SAL ANNUAL COMM

7839 KING 5000 60000 416.66

7698 BLAKE 2850 34200 237.5

7782 CLARK 2450 29400 204.16

7566 JONES 2975 35700 247.91

7788 SCOTT 3000 36000 250

7902 FORD 3000 36000 250

7369 SMITH 800 9600 66.66

The above query performs multiplication and division operation on each and every value of the salary
column and displayed.

SQL Bitwise operators

Bitwise operators apply true false conditions on the individual binary bits of numbers

 Bitwise AND(&): Returns true you only are both input bits are true otherwise false
 Bitwise OR(|): Returns true if either of the input bits is true otherwise false
 Bitwise XOR(^): Returns true only if both the input bits are different

SQL comparison(Relational)  operators

Comparison operators are generally used along with where clause for filtering the data as per the
required condition specified
Assume variable a holds 10 and variable b holds 20 then

Operator Example

= (a = b) is not true.

!= (a != b) is true.

> (a > b) is not true.

< (a < b) is true.

>= (a >= b) is not true.

<= (a <= b) is true.

!< (a !< b) is false.

!> (a !> b) is true.

Example

select empno,ename,sal,job
from emp
where sal>2500;
O/P

5 rows selected.
EMPNO ENAME SAL JOB

7839 KING 5000 PRESIDENT

7698 BLAKE 2850 MANAGER

7566 JONES 2975 MANAGER

7788 SCOTT 3000 ANALYST

7902 FORD 3000 ANALYST

The above displays the details of employees whose salary is greater than 2500.

SQL Logical operators

AND operator

The AND results true only when all the conjunction of conditions specified after the where clause
are satisfied
Example

select * from emp


where job='MANAGER' AND sal>2500;

O/P

2 rows selected.
EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO

7698 BLAKE MANAGER 7839 01-MAY-81 2850 30

7566 JONES MANAGER 7839 02-APR-81 2975 20

Here two conditions first employee must be a manager at the same time he must be getting salary greater
than 2500 then only condition becomes true and the record gets displayed.

OR operator

Among multiple conditions specified in the where clause  the transaction is performed if any of the


condition becomes true

Example
select * from emp
where JOB='ANALYST' OR JOB='MANAGER';

O/P

5 rows selected 
EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO

7698 BLAKE MANAGER 7839 01-MAY-81 2850 30

7782 CLARK MANAGER 7839 09-JUN-81 2450 10

7566 JONES MANAGER 7839 02-APR-81 2975 20

7788 SCOTT ANALYST 7566 19-APR-87 3000 20

7902 FORD ANALYST 7566 03-DEC-81 3000 20

In the example above, the data is fetched if  the employee is an analyst or a manager i.e data is fetched
even if any one condition is satisfied.

 
SQL Special Operators

IN operator

It is used to specify a set of values and operation n is performed on all the values specified in the set
and if any of the value that is present in the list matches with the values present in a table then it
returns true and is operation is performed

Example
select empno,job,sal,hiredate from emp
where ename in('SCOTT','FORD','SMITH','JONES')
O/P

4 rows selected.
 EMPN
O JOB SAL HIREDATE

7566 MANAGER 2975 02-APR-81

7788 ANALYST 3000 19-APR-87

7902 ANALYST 3000 03-DEC-81

7369 CLERK 800 17-DEC-80

Records of all those employees that are specified in the list of in are displayed.

BETWEEN Operator

It is used to perform data comparison and manipulation over a range of values present in the
database table

Example

select empno,job,sal,hiredate
from emp
where sal between 800 and 2900;
O/P

3 rows selected.
EMPNO JOB SAL HIREDATE

7698 MANAGER 2850 01-MAY-81

7782 MANAGER 2450 09-JUN-81

7369 CLERK 800 17-DEC-80

 The details of those employees whose salary is present in the range between 800 to 2900
retrieved and it also considered specified values inclusive of the range
 In the case of between operator lower value is first specified and followed by the higher value 
& and operator in between this higher and lower values.

LIKE  operator

The like operator is a pattern matching operator and returns those records that match the specified
pattern

Example
select empno,ename,hiredate,sal,job
from emp
where ename like 'S%';
O/P

2 rows selected.
EMPNO ENAME HIREDATE SAL JOB

7788 SCOTT 19-APR-87 3000 ANALYST

7369 SMITH 17-DEC-80 800 CLERK

All the records of employees whose name starting with letter ‘S’ are displayed.

IS NULL operator

All operations upon null values present in the table must be done using this ‘is null’ operator .we
cannot compare null value using the assignment operator
Example
select * from emp
where comm is null
O/P

4 rows selected.
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7839 KING PRESIDENT – 17-NOV-81 5000 – 10

7566 JONES MANAGER 7839 02-APR-81 2975 – 20

7788 SCOTT ANALYST 7566 19-APR-87 3000 – 20

7902 FORD ANALYST 7566 03-DEC-81 3000 – 20

The details of those employees whose commission value is Null are displayed.

NOT  operator

Not operator is a negation operator which is used along with like between, is null, in operators, It
performs  reverse r action of all these operators.

IS NOT NULL
select * from emp
where comm is not null;
O/P

3 rows selected.
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7698 BLAKE MANAGER 7839 01-MAY-81 2850 500 30

7782 CLARK MANAGER 7839 09-JUN-81 2450 500 10

7369 SMITH CLERK 7902 17-DEC-80 800 500 20

Details of all those employees whose Commission value is not null value are displayed.

NOT BETWEEN 
select * from emp
where sal NOT between 800 and 2900
O/P

4 rows selected.
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7839 KING PRESIDENT – 17-NOV-81 5000 – 10

7566 JONES MANAGER 7839 02-APR-81 2975 – 20

7788 SCOTT ANALYST 7566 19-APR-87 3000 – 20

7902 FORD ANALYST 7566 03-DEC-81 3000 – 20

The details of those employees whose salary does not fall in the range between 800 to 2900 displayed.

NOT LIKE
select * from emp
where ename NOT like 'S%';
O/P

5 rows selected.
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7839 KING PRESIDENT – 17-NOV-81 5000 – 10

7698 BLAKE MANAGER 7839 01-MAY-81 2850 500 30

7782 CLARK MANAGER 7839 09-JUN-81 2450 500 10

7566 JONES MANAGER 7839 02-APR-81 2975 – 20

7902 FORD ANALYST 7566 03-DEC-81 3000 – 20

The details of all those employees whose name doesn’t start with letter ‘S’ are displayed.

NOT IN
select * from emp
where ename not in('SCOTT','FORD','SMITH','JONES');
O/P

5 rows selected
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7839 KING PRESIDENT – 17-NOV-81 5000 – 10

7698 BLAKE MANAGER 7839 01-MAY-81 2850 500 30


7782 CLARK MANAGER 7839 09-JUN-81 2450 500 10

7566 JONES MANAGER 7839 02-APR-81 2975 – 20

7902 FORD ANALYST 7566 03-DEC-81 3000 – 20

The details of all employees whose names are not among the list  are displayed

You might also like