0% found this document useful (0 votes)
4 views39 pages

SQL Operators

The document provides an overview of SQL operators, including arithmetic, logical, concatenation, and set operators like UNION, INTERSECT, and MINUS. It explains the use of SELECT statements, WHERE clauses, and various operators such as LIKE, IN, BETWEEN, and NULL handling. Additionally, it covers the ORDER BY clause for sorting results and introduces the DUAL table in Oracle databases.

Uploaded by

Mohit Sidana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views39 pages

SQL Operators

The document provides an overview of SQL operators, including arithmetic, logical, concatenation, and set operators like UNION, INTERSECT, and MINUS. It explains the use of SELECT statements, WHERE clauses, and various operators such as LIKE, IN, BETWEEN, and NULL handling. Additionally, it covers the ORDER BY clause for sorting results and introduces the DUAL table in Oracle databases.

Uploaded by

Mohit Sidana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

SQL Operators

Sanjeev Gumber
DAV College, Abohar
Arithmetic Operators
• The arithmetic operators are used in an
expression to add, subtract, multiply, divide
numeric values. The result of the operation is
also a numeric value. The Plus(+) and minus(-)
operators are also used in date arithmetic.
Logical operators
• The logical operators are used to combine the
individual logical expressions into more
complex conditions that are either TRUE or
FALSE.
Concatenation Operator (||)
• The concatenation operator is used to
concatenate two or more strings.

• Select ‘My name is ‘|| ename as name from


emp where deptno=10;
SET operators
UNION[ALL]
• UNION set operator returns a table consisting
of all rows either appearing in the result of
<SELECT Statement 1> or in the result of
<SELECT Statement 2>.
• Duplicates are automatically eliminated unless
the clause ALL is used.
• If we use UNION ALL set operator, then
duplicate rows are not filtered out from the
list.
• Select Empno, Ename from Emp
Where Ename LIKE ‘A%’ or Ename Like ‘S%’
UNION
Select Empno, Ename from Emp
Where Ename LIKE ‘A%’ or Ename Like ‘J%’
INTERSECT
• The INTERSECT set operator returns all those
distinct rows that exist in both the <select
statement1> and <select statement2>.
• Select Empno, Ename from Emp
Where Ename LIKE ‘A%’ or Ename Like ‘S%’
INTERSECT
Select Empno, Ename from Emp
Where Ename LIKE ‘A%’ or Ename Like ‘J%’
MINUS
• The MINUS set operator returns all those rows
that appear in the result of <select statement1>
but not present in the result of <select
statement2>.
• Select Empno, Ename from Emp
Where Ename LIKE ‘A%’ or Ename Like ‘S%’
MINUS
Select Empno, Ename from Emp
Where Ename LIKE ‘A%’ or Ename Like ‘J%’
Select Statement
• Select <select_list>
• From <table_list>
• [WHERE <condition(s)>]
• [GROUP BY<column1>,<column2>,----
<columnN>]
• [HAVING <condition>]
• [ORDER BY<expression>];
• # to retrieve all Columns
• Select * from emp;
• # to retrieve the selected columns
• Select Sal, deptno from emp;
• # to retrieve the Distinct rows
• Select DISTINCT Sal, deptno from emp;
Restricting rows using where clause
• # to retrieve the employee’s name , sal, job
title whose empno is 7369
• Select ename,sal,job from emp where
empno=7369;
• #to retrieve the ename,sal,job from the emp
table where sal is greater than equal to 2975
and deptno=20;
• Select ename, sal, job from emp
Where sal>=2975 and deptno=20;
• # to retrieve the empno, ename, job from emp
table where job is equal to that of
‘MANAGER’ or the deptno in which he works
is 20.
• Select empno, ename, job from emp where
job=‘MANAGER’ OR deptno=20;
Using expression In the Select
statement
To retrieve the rows consisting of employee’s name,
total salary(SAL+COMM) of the employee whose
job’s title is ‘SALESMAN’.
Select ename, Sal+NVL(Comm,0) from emp where
job=‘SALESMAN’;
# referencing columns with an alias
Select ename, Sal+NVL(Comm,0) as Total_sal from
emp where job=‘SALESMAN’;
Select ename, Sal+NVL(Comm,0) “Total_sal” from
emp where job=‘SALESMAN’;
Use of LIKE operator
• 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
Retrieve ename, sal, job from emp
table where ename of the employee
begins with Letter ‘A’.
• Select ename, sal, job from emp where ename
LIKE ‘A%’ ;
Find the Ename, Sal of an employee from
Emp table whose name consists of five
characters with the second character to be
‘L’ and fourth character is ‘E’.

• Select Ename, Sal from Emp where Ename


LIKE ‘_L_E_’ ;
To retrieve all the employee’s name,
salary from Emp table whose name
does not start with character ‘A’.

• Select Ename, Sal from Emp where Ename


NOT LIKE ‘A%’ ;
• # List all matching patterns ending with ‘TH’
• LIKE ‘%TH’
• # List all matching patterns containing two L in it
• LIKE ‘%L%L%’
• #list all matching patterns containing U at the
second position in the string
• LIKe ‘_U%’
• List all matching pattern having four characters
such that last character is U.
• LIKE ‘_ _ _ U’
USE of IN Operator
• The IN operator allows you to specify multiple
values in a WHERE clause.
• The IN operator is a shorthand for multiple OR
conditions.
• SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
To retrieve the Ename, Sal and Deptno
of an employee from an Emp table
whose Deptno is either 10 or 20
• Select Ename , Sal, Deptno from Emp
Where deptno IN (10,20) ;
To retrieve the Empno, Ename and Job
of the employee’s from the Emp table
where job of an employee is either
‘CLERK’ or ‘MANAGER’
• Select Empno, Ename, Job from emp where
Job IN(‘CLERK’, ‘MANAGER’);
To retrieve the Ename, Sal, Deptno
of all employee from EMP table
having Deptno neither 10 nor 30
• Select Ename, Sal, Deptno from emp
Where deptno NOT IN(10,30);
BETWEEN Operator
• 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.
• SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND v
alue2;
To retrieve the Ename, Sal, Deptno of
an employee from an EMP table
whose deptno is between 10 and 30

• Select Ename, Sal, Deptno from Emp


Where deptno BETWEEN 10 AND 30;
To retrieve the ename, sal and deptno
of an employee from an EMP table
whose Ename range from character
‘AM’ to ‘F’

• Select Ename, Sal, Deptno from Emp


Where Ename Between ‘AM’ AND ‘F’ ;
To retrieve the ename, sal and deptno
of an employee from an EMP table
whose salary is not between 1000 and
4000.
• Select Ename, Sal, Deptno from Emp
Where Sal NOT BETWEEN 1000 and 4000;
NULL and NOT NULL
• NULL is a term used to describe something that is
undefined.
• A NULL column means that there is no value assigned to a
column.
• NULL does not mean 0 or blank.
• Since NULL means no value or nothing or unknown, it
cannot be compared using comparison or logical operators.
• So NULL can not equal to NULL.
• A NULL value is used when the actual value is not known.
• To test whether a value is Null or not Oracle provides two
SQL operators IS Null and IS NOT NULL.
To retrieve Empno, Ename of an
employee whose commission is NULL

• Select Empno, Ename from Emp


Where COMM=NULL;
This is wrong
• Select Empno, Ename from Emp
Where COMM IS NULL;
To retrieve Empno, Ename of an
employee whose commission is
not NULL
• Select Empno, Ename from emp where
COMM IS NOT NULL;
Order By Clause
• The ORDER BY keyword is used to sort the
result-set in ascending or descending order.
• The ORDER BY keyword sorts the records in
ascending order by default. To sort the records
in descending order, use the DESC keyword.
• ORDER BY Syntax
• SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
To retrieve Ename, Job title, Sal of an
employee from EMP table who works in
DEPTNO=20 and the names of the
employees are shown in ascending order

• Select Ename, Job, Sal from Emp


Where deptno=20 ORDER BY Ename;
To retrieve Ename, Job title, Sal of an
employee from EMP table who works
in DEPTNO=20 and the names of the
employees are shown in Decending
order
• Select Ename, Job, Sal from Emp
Where deptno=20 ORDER BY Ename DESC;
To retrieve Ename, Job title, Sal of an
employee from EMP table who works in
DEPTNO=20 and the order of the retrieved
records is such that sort by Job in
ascending order , then it will sort by Ename
in Descending order

• Select Ename, Job, Sal from Emp


Where deptno=20 ORDER BY Job ASC, Ename
DESC;
Dual Table
• The DUAL is special one row, one column table
present by default in all Oracle databases.
• The owner of DUAL is SYS but DUAL can be
accessed by every user.
• The table has a single VARCHAR2(1) column
called DUMMY that has a value of 'X'.
• Eg Select 2*2 from dual;
• Select sysdate from dual;

You might also like