Database Management System
By
Mr. Parag R. Sali
Lecturer
Department of Computer Technology
SNJB’s Shri. Hiralal Hastimal ( Jain Brothers)
Polytechnic, Chandwad
Program Name: Computer Engineering Group
Program Code : CO/CM/CW
Semester : Third
Course Title : Database Management System
Course Code : 22319
SQL Operators
SQL OPERATORS
Arithmetic Operators
Comparison Operators
Logical Operators
Set Operators
Range Searching Operators – Between
Pattern Matching Operators – Like
ARITHMETIC OPERATORS
Consider : a=10 , b=20
Operator Description Example
Adds values on either side of the operator.
+ (Addition) a + b will give 30
Subtracts right hand operand from left
- (Subtraction) hand operand. a - b will give -10
Multiplies values on either side of the
* (Multiplication) operator. a * b will give 200
Divides left hand operand by right hand
/ (Division) operand. b / a will give 2
Divides left hand operand by right hand
% (Modulus) operand and returns remainder. b % a will give 0
COMPARISON OPERATORS
Operator Description Example a=10 b=20
Checks if the values of two operands are equal or not, if yes then condition
= (a = b) is not true.
becomes true.
Checks if the values of two operands are equal or not, if values are not equal then
!= condition becomes true. (a != b) is true.
Checks if the values of two operands are equal or not, if values are not equal then
<> condition becomes true. (a <> b) is true.
Checks if the value of left operand is greater than the value of right operand, if
> yes then condition becomes true. (a > b) is not true.
Checks if the value of left operand is less than the value of right operand, if yes
< then condition becomes true. (a < b) is true.
Checks if the value of left operand is greater than or equal to the value of right
>= operand, if yes then condition becomes true. (a >= b) is not true.
Checks if the value of left operand is less than or equal to the value of right
<= operand, if yes then condition becomes true. (a <= b) is true.
Checks if the value of left operand is not less than the value of right operand, if
!< yes then condition becomes true. (a !< b) is false.
Checks if the value of left operand is not greater than the value of right operand,
!> if yes then condition becomes true. (a !> b) is true.
Roll_no Name City Marks
1 Jay Delhi 87
2 Sai Pune 60
3 Deep Nasik 75
4 Riya Gujrat 93
5 Diya Jalgaon 88
Select * from stud where name = ‘Deep’;
Roll_no Name City Marks
3 Deep Nasik 75
Select * from stud where Marks < 70;
Roll_no Name City Marks
2 Sai Pune 60
Select * from stud where Marks >90;
Roll_no Name City Marks
4 Riya Gujrat 93
LOGICAL OPERATORS
Sr.No. Description
AND: The AND operator allows the existence of multiple
1 conditions in an SQL statement's WHERE clause.
OR: The OR operator is used to combine multiple conditions
2 in an SQL statement's WHERE clause.
NOT: The NOT operator reverses the meaning of the logical
operator with which it is used.
3 Eg: NOT EXISTS, NOT BETWEEN, NOT IN, etc. This is a negate operator.
Roll_no Name City Marks
1 Jay Nasik 87
2 Sai Pune 60
3 Deep Nasik 75
4 Riya Gujrat 93
5 Diya Jalgaon 88
Select * from stud where City=‘Nasik’ AND Marks >=80;
Roll_no Name City Marks
1 Jay Nasik 87
Select * from stud where City=‘Nasik’ OR Marks >=80;
Roll_no Name City Marks
1 Jay Nasik 87
3 Deep Nasik 75
4 Riya Gujrat 93
5 Diya Jalgaon 88
SET OPERATORS
SQL supports few Set operations which can be
performed on the table data. These are used to get
meaningful results from data stored in the table, under
different special conditions.
Set operators are used to join the results of multiple
SELECT statements.
In this tutorial, we will cover 4 different types of SET
operations, along with example:
UNION
UNION ALL
INTERSECT
MINUS
Consider following tables to perform SET operators
Table : Emp
Emp_name Dept_no City
Jay 101 Mumbai
Rahul 102 Pune
Sai 103 Nashik
om 102 Jalgaon
siya 101 Nagar
Table : Dept
Dept_no Dept_name
101 Comp
102 Mech
103 Civil
104 E&TC
UNION OPERATOR
UNION is used to combine the results of two or
more SELECT statements. However it will eliminate duplicate
rows from its result set.
Syntax :
Select column_name from table1 UNION Select column_name from table2;
Example :
Select Dept_no from Emp UNION Select Dept_no from Dept;
Output :
Dept_no
101
102
103
104
UNION ALL OPERATOR
This operation is similar to Union. But it also shows the
duplicate rows.
Syntax :
Select column_name from table1 UNION ALL Select column_name from table2;
Example : Dept_no
Select Dept_no from Emp UNION ALL Select Dept_no from Dept; 101
102
103
102
101
101
102
103
104
INTERSECT OPERATOR
Intersect operation is used to combine two SELECT statements,
but it only returns the records which are common from
both SELECT statements.
Syntax :
Select column_name from table1 INTERSECT Select column_name from table2;
Example :
Select Dept_no from Emp INTERSECT Select Dept_no from Dept;
Dept_no
101
102
103
MINUS OPERATOR
The Minus operation combines results of two SELECT statements and return only those in the final result, which
belongs to the first set of the result.
Syntax :
Select column_name from table1 MINUS Select column_name from table2;
Example :
Select Dept_no from Emp MINUS Select Dept_no from Dept;
Dept_no
104
RANGE SEARCHING OPERATORS – BETWEEN
The BETWEEN operator selects values within a given
range. The values can be numbers, text, or dates.
The BETWEEN operator is inclusive: begin and end values
are included.
Syntax :
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
Example :
SELECT * FROM Dept WHERE Dept_no BETWEEN 101 AND 103;
Select * from Emp where Dept_no BETWEEN 101 AND 103;
Select city from Emp where Dept_no BETWEEN 101 and 103;
Select emp_name from Emp where Dept_no BETWEEN 101 and 103
Output: Dept_no Dept_name
101 Comp
102 Mech
103 Civil
Emp_name Dept_no City
Jay 101 Mumbai
Rahul 102 Pune
Sai 103 Nashik
om 102 Jalgaon
siya 101 Nagar
PATTERN MATCHING OPERATOR – LIKE
The LIKE operator is used in a WHERE clause to search for
a specified pattern in a column.
There are two wildcards often used in conjunction with the
LIKE operator:
% - The percent sign represents zero, one, or multiple characters
_ - The underscore represents a single character
The percent sign and the underscore can also be used in combinations
Syntax :
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
some examples showing different LIKE operators with
'%' and '_' wildcards:
LIKE Operator Description
WHERE CustomerName LIKE 'a%' Finds any values that start with "a"
WHERE CustomerName LIKE '%a' Finds any values that end with "a"
WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any position
WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second position
WHERE CustomerName LIKE 'a_%' Finds any values that start with "a" and are at least 2
characters in length
WHERE CustomerName LIKE 'a__%' Finds any values that start with "a" and are at least 3
characters in length
WHERE ContactName LIKE 'a%o' Finds any values that start with "a" and ends with "o"
Example :
SELECT * FROM Emp
WHERE Emp_nameLIKE ‘s%';
Output :
Emp_name Dept_no City
Sai 103 Nashik
siya 101 Nagar