OPERATORS AND FUNCTIONS
OPERATORS:-
• Operators are symbols that operates on a values/operand.
• Operators are used to manipulate data using single
command.
Arithmetic
Logical
Relational
Special
String
ARITHMETIC OPERATORS:-
LOGICAL OPERATORS:-
• AND :- Check both the conditions should be true
select * from emp where id = 100 AND sal
>=30000;
• OR :- Check any of two conditions are true
select name from emp where id = 15 OR id =40;
• NOT :- Reverse the result/
select * from emp where NOT sal >=30000;
RELATIONAL OPERATORS:-
SPECIAL OPERATORS:-
STRING OPERATORS:-
• %
• To list employees whose names begin with ‘m’
Select * from emp where name like ‘m%’;
• _
• To list employees who name start with ‘m’ and ends with ‘i’
Select * from emp where name like ‘m_ _ _ _i’;
• ||
• To concatenate
• Select name||id from emp;
SET OPERATOR
• It is used to combine the result of two queries.
• here are certain rules which must be followed to perform operations
using SET operators in SQL. Rules are as follows:
i - The number and order of columns must be the same.
ii-Data types must be compatible.
• UNION
• UNION ALL
• INTERSECT
• MINUS
union
• UNION will be used to combine the result of two select
statements.
• Duplicate rows will be eliminated without ignoring the
NULL values from the results obtained after performing
the UNION operation.
• SELECT *FROM empss1 UNION SELECT *FROM empss2;
OUTPUT
Union all
• This operator combines all the records from both the
queries.
• Duplicate rows will be not be eliminated from the results
obtained after performing the UNION ALL operation.
• SELECT *FROM empss1 UNION
ALL SELECT *FROM empss2;
OUTPUT
INTERSECT
• It is used to combine two SELECT statements, but it
only returns the records which are common from
both SELECT statements.
• SELECT *FROM empss1 INTERSECT SELECT *FROM e
mpss2;
OUTPUT
MINUS
• It displays the rows which are present in the first
query but absent in the second query with no
duplicates.
• SELECT *FROM empss1 MINUS SELECT *FROM emps
s2;
OUTPUT
FUNCTION :-
• Functions are methods used to perform data operations.
•Function is used to perform some particular task and it
returns zero or more values as a result.
• SQL functions are categorized into the following two
categories:
Aggregate Functions
Scalar Functions
Aggregate Functions
• The Aggregate Functions in SQL perform calculations on a
group of values and then return a single value.
• SUM()
• COUNT()
• AVG()
• MIN()
• MAX()
• FIRST()
• LAST()
SUM()
• Used to return a total sum of numeric column which you
choose.
• Write a query to retrieve the sum of marks of all students
from the Students table.
• SELECT SUM(Total Marks) FROM Student;
• OUTPUT ?
COUNT()
• Returns the number of rows present in the table either based on
some condition or without any condition.
• Write a query to count the number of students from the Students
table.
• SELECT COUNT(ROLL_NO) FROM Student;
• OUTPUT ?
• SELECT COUNT(ROLL_NO) FROM Student WHERE Marks >90;
• OUTPUT ?
AVG()
• This function is used to return the average value of a
numeric column.
• Write a query to calculate the average marks of all
students from the Students table.
• SELECT AVG(Total Marks) FROM Student;
• Output ?
MIN()
• Used to return the minimum value of a numeric
column.
• Write a query to retrieve the minimum marks out of all
students from the Students table.
• SELECT MIN(Total Marks) FROM Student;
• Output ?
MAX()
• Used to return the maximum value of a numeric
column.
• Write a query to retrieve the maximum marks out of all
students from the Students table.
• SELECT MAX(Total Marks) FROM Student;
• Output ?
LAST()
• The LAST() function returns the last value of the
selected column. It can be used only in MS ACCESS.
Syntax:
SELECT LAST(column_name) FROM table_name;
Q.Fetching marks of last student from the Students table.
A) SELECT LAST(MARKS) AS MarksLast FROM Students;
FIRST()
• The FIRST() function returns the first value of the
selected column.
Syntax:
SELECT FIRST(columnname) FROM table_name;
Q.Fetching marks of first student from the Students table.
A) SELECT FIRST(MARKS) AS MarksFirst FROM Students;
Scalar Functions
• The Scalar Functions are used to return a single value from the given input
value.
• The scalar functions are also known as single row functions.
• These functions are based on user input, these too return single value.
I. UPPER()
II. LOWER()
III. LEN()
IV. ROUND()
V. NOW()
UPPER():
It converts the value of a field to uppercase.
Syntax:
Select UCASE(Columnname) From table_name;
Q. Converting names of the students from the table Students to uppercase.
A) Select UCASE(NAME) from Students;
LOWER():
It converts the value of a field to lowercase.
Syntax:
SELECT LCASE(column_name) FROM table_name;
Q. Converting names of students from the table Students to lowercase.
A) SELECT LCASE(NAME) FROM Students;
LEN():
The LEN() function returns the length of the value in a text field.
Syntax:
SELECT LENGTH(column_name) FROM table_name;
Q.Fetching length of names of students from Students table.
A)SELECT LENGTH(NAME) FROM Students;
ROUND():
The ROUND() function is used to round a numeric field to the number
of decimals specified.
Syntax:
SELECT ROUND(column_name , decimals) FROM table_name;
Q.Fetching maximum marks among students from the Students table.
A) SELECT ROUND(MARKS,0) FROM table_name;
NOW():
The NOW() function returns the current system date and time.
Syntax:
SELECT NOW() FROM table_name;
Q.Fetching current system time.
A) SELECT NAME, NOW() AS DateTime FROM Students;