3. OPERATORS IN SQL
- Operators are used to specify conditions in an SQL statement and to serve as conjunctions
for multiple conditions in a statement.
Types of Operators:
• Arithmetic operators
• Comparison operators
• Logical operators
• Operators used to negate conditions
- WHERE clause is used to perform operation(s), such as comparisons and arithmetic
operations in SQL statement.
6. EXAMPLES
• Increment the price of all stationery items with Rs. 10.
• Decrement the quantity of all stationery items with 6.
7. EXAMPLES
• Display 3 times of price for all items.
• Display the Quantity divided by 2.
8. EXAMPLES
• Display the Price added with 15 and quantity divided by 3.
• Display the total cost of all products.
9. ALIASES
• ALIASES can be used to create a temporary name for columns or
tables.
• Aliases are often used to make column names more readable.
• An alias only exists for the duration of the query
Syntax
column_name AS alias_name
Note
If the alias_name contains spaces, you must enclose the alias_name in
double quotes
12. DUAL
• The DUAL is special one row, one column table present by default in
all Oracle databases.
• The owner of DUAL is SYS (SYS owns the data dictionary, therefore
DUAL is part of the data dictionary) but DUAL can be accessed by
every user.
• The table has a single VARCHAR2(1) column called DUMMY that
has a value of 'X'.
18. COMPARISON OPERATORS
= Equality Operator
used to test for equality in a query. The = operator can only test equality
with values that are not NULL.
Example:
Get the details from Stationery relation whose supplier name is ‘Robin’.
19. COMPARISON OPERATORS
! = or <> Inequality Operator
It checks for inequality
Example:
Get the details from Stationery relation whose supplier name is not
‘Robin’.
20. COMPARISON OPERATORS
> Greater than Operator
Example:
Get the details from Stationery relation whose Price is greater than 10.
21. COMPARISON OPERATORS
< Less than Operator
Example
Get the details from Stationery relation whose Price is less than 10.
22. COMPARISON OPERATORS
>= Greater Than or Equal To Operator
Example
Get the details from Stationery relation whose quantity is greater than or
equal to 50.
23. COMPARISON OPERATORS
<= Less Than or Equal To Operator
Example
Get the details from Stationery relation whose quantity is less than or
equal to 90.
24. LOGICAL OPERATORS
AND OPERATOR
Should satisfy both the conditions.
Example:
Display the details of Stationery whose S_ID is ‘MT987’and Supplier is
‘Jessy’.
26. LOGICAL OPERATORS
Display the details of Stationery whose S_ID is ‘MT987’ and ‘RT987’.
Display the details of Stationery whose Name is ‘Eraser’or Quantity is
65.
27. LOGICAL OPERATORS
BETWEEN OPERATOR
Used to retrieve/get values from the table within a specific range.
Example:
Display the details of Stationery whose price ranges from 5 to 10.
28. LOGICAL OPERATORS
IN OPERATOR
It is used to help reduce the need to use multiple OR conditions.
Example:
Display the details of the Stationery whose S_ID areAS154, MT987
and DE589.
29. LOGICAL OPERATORS
LIKE OPERATOR
Used for pattern matching. It checks for the matching pattern using wild
card operators.
% (percentage) – matches for zero or more characters.
_ (Underscore) – matches for a single character.
Examples:
Display the details of the stationery whose name begins with ‘P’.
30. LOGICAL OPERATORS
Display the details of the stationery whose name begins with ‘Pen’.
Display the details of the stationery whose name ends with ‘r’.
31. LOGICAL OPERATORS
Display the details of the stationery whose name ends with ‘ser’.
Display the details of the stationery whose name contains the letter ‘e’.
32. LOGICAL OPERATORS
Display the details of the stationery whose name exactly contains 6
characters.
Display the details of the stationery whose name has ‘E’ as first
character and ‘a’as third character.
33. LOGICAL OPERATORS
IS NULLOperator
The IS NULL operator is used to display all the rows for columns that do not have a
value.
Example:
Display the details of the stationery for which the tax is not mentioned.
34. NOT OPERATOR
It is used to negate conditions.
Example:
Display the details of the stationery whose name is not ‘Pen’.
35. NOT OPERATOR
Not Operator with BETWEEN:
Display the details of the stationery whose quantity does not ranges
from 50 and 60.
(NOT can be given near column name or near BETWEEN operator)
36. NOT OPERATOR
Not Operator with IN:
Display the details of the Stationery whose S_ID are notAS154, MT987
and DE589.
(NOT can be given near column name or near IN operator)
37. NOT OPERATOR
Not Operator with LIKE:
Display the details of the stationery whose name does not starts with ‘P’.
(NOT can be given near column name or near LIKE operator)
38. NOT OPERATOR
Not Operator with LIKE:
Display the details of the stationery whose name does not contain 5
letters.
39. NOT OPERATOR
Not Operator with IS NULL:
Display the details of the Stationery which has tax.
(NOT can be given near column name or near NULL)
40. COCATENATION OPERATOR
It is used to join two string values or expressions in a SELECT query.
|| is the symbol used for concatenating or joining two strings.
Example:
Display S_ID and S_Name in a single column.
42. DISTINCT
The DISTINCT clause is used in a SELECT statement to filter duplicate
rows in the result set. It ensures that rows returned are unique for the
column or columns specified in the SELECT clause.
In Simple,
The DISTINCT clause is used to return only distinct (different) values.
Example:
Display the distinct values in Supplier Name Column of Stationery
Relation.