You are on page 1of 17

A function is a special type of

Predefined Command Set that


performs some operation and
return a single value. The functions
operate on a value provided to
them. These values that are
provided to functions are called
PARAMETERS OR ARGUMENTS.
1. Single Row Functions:
A Single Row Function works with a
single row at a time. It return a
result for every row of a queried
table.
2. Multiple Row function:
Multiple row functions work with
data of multiple row at a time and
return aggregated value.
• Single row character functions accepts a
character input and return both character and
numeric values.
1. LOWER(): It Converts the given character-
expression in to lower case.
Eg. SELECT lower(name) from STUREC;
2. CONCAT(): This function concatenates two
strings.
Syntax : CONCAT(CHAR1, CHAR2);
MySql> Select Concat(Roll_no,Name) “Rollno and
Name” from STUREC where Roll_no<=105;
3. UPPER(): It Converts the given character-
expression in to upper case.
Eg. SELECT upper(name) from STUREC;

4. REPLICATE : Repeats a input string for a


specified number of times.
Syntax: REPLICATE (string, integer)

MySql > SELECT REPLICATE('FUNCTION', 3)


returns FUNCTIONFUNCTIONFUNCTION
SUBSTR : Returns part of a given string.
SUBSTR function retrieves a portion of the given string starting
at the specified character(M) to the number of characters
specified(N).
Syntax: SUBSTRING(CHAR, M, N)
EXAMPLE: SELECT SUBSTR( ‘HELLO’, 2,3) FROM DUAL;
OUTPUT WILL BE  ELL
LEN : Returns the length of the character string.
Syntax: LEN(string)
SELECT LEN('STRING FUNCTION') returns 15

REPLACE : Replaces all occurrences of the second


string(string2) in the first string(string1) with a third
string(string3).
Syntax: REPLACE('string1','string2','string3')
SELECT REPLACE('STRING FUNCTION','STRING','SQL')
returns SQL FUNCTION
Returns NULL if any one of the arguments is NULL.
LEFT : Returns left part of a string with the specified number of characters
counting from left.LEFT function is used to retrieve portions of the string.
Syntax: LEFT(string,integer)
SELECT LEFT('STRING FUNCTION', 5) returns STRIN

RIGHT : Returns right part of a string with the specified number of


characters counting from right.RIGHT function is used to retrieve portions of
the string.
Syntax: RIGHT(string,integer)
SELECT RIGHT('STRING FUNCTION‘,4) returns TION

LTRIM : Returns a string after removing leading blanks on Left


side.(Remove left side space or blanks)
Syntax: LTRIM(string)
SELECT LTRIM(' STRING FUNCTION') returns STRING FUNCTION

RTRIM : Returns a string after removing leading blanks on Right


side.(Remove right side space or blanks)
Syntax: RTRIM( string )
SELECT RTRIM('STRING FUNCTION ') returns STRING FUNCTION
REVERSE : Returns reverse of a input string.
Syntax: REVERSE(string)

SELECT REVERSE('STRING FUNCTION') returns


NOITCNUF GNIRTS

SPACE : Returns a string of repeated spaces.


The SPACE function is an equivalent of using REPLICATE
function to repeat spaces.
Syntax: SPACE ( integer)
(If integer is negative, a null string is returned.)

SELECT ('STRING') + SPACE(1) + ('FUNCTION') returns

STRING FUNCTION
Some of the commonly used mathematical functions are
sum() avg(), count(), min(), max() etc.

e.g. SELECT sum(marks) FROM student;


displays the sum of all the marks in the table student.

e.g. SELECT min(Roll_no), max(marks) FROM student;


displays smallest Roll_no and highest marks in the table
student.
e.g. SELECT avg(marks) FROM student;
displays the average of all the marks in the table student.

e.g. SELECT count(*) FROM student;


displays the total no. of records(rows) table student.
GETDATE() : This function returns the current system date.
SELECT GATEDATE() FROM DUAL;

ADD_MONTHS(D,N):
SELECT ADD_MONTHS(first date, 2) FROM dual;
This function adds number of months to a given date and
returns a date after those many months.

MONTHS_BETWEEN(): It finds no. of months between two


given dates.
SELECT MONTHS_BETWEEN(‘22-10-2010’,15-11-2011’)

NEXT_DAY() : It returns the date of specified coming day in


the next week.
SELECT NEXT_DAY(‘15-11-2011’,’TUESDAY’);
It find out the date of next Tuesday after 15-11-11: 22-11-11
TRUNC() : This function returns a truncated date.
Syntax: TRUNC(SYSDATE,’YEAR’)
SELECT TRUNC(‘15-11-2011’, ’2011’) FROM DUAL;
Output : 01-01-2011 -> Beginning date of the SAME year.

ROUND() : This function returns a date that is rounded off.


Syntax: ROUND(SYSDATE,’YEAR’)
SELECT TRUNC(‘15-11-2011’, ’2011’) FROM DUAL;
Output : 01-01-2012 -> Beginning date of the NEXT year.

Syntax: ROUND(SYSDATE,’MONTH’)
SELECT TRUNC(‘03-05-2011’, ’05’) FROM DUAL;
Output : 01-05-2011 -> Nearest date of the same month.
DELETE Command
To delete the record fro a table SQL provides a
delete statement.
General syntax is:-

DELETE FROM <table_name> [WHERE <condition>];

> DELETE FROM student WHERE city = ‘Jammu’;

This command deletes all those records whose city


is Jammu.
UPDATE Command: To update the data stored
in the data base, UPDATE command is used.
Syntax:
UPDATE Tablename SET column=newvalue
WHERE column=oldvalue;

> UPDATE student SET marks = marks + 100;


Increase marks of all the students by 100.

> UPDATE student SET City = ‘Udhampur’


WHERE city = ‘Jammu’;
changes the city of those students to Udhampur
whose city is Jammu.
We can also update multiple columns with
update command, like

UPDATE student set marks = marks + 20,


city = ‘Jalandhar’ WHERE city NOT IN
(‘Jammu’,’Udhampur’);
ALTER TABLE Command
In SQL if we ever need to change the structure of
the table then ALTER TABLE command is used. By
using this command we can add a column in the
existing table, delete a column from a table or
modify columns in a table.
Adding a column
The syntax to add a column is:-
ALTER TABLE table_name ADD column_name datatype;

ALTER TABLE student ADD(Address varchar(30));

The above command add a column Address to the


table atudent.
Removing a column
ALTER TABLE table_name DROP COLUMN column_name;
e.g ALTER TABLE Student DROP COLUMN Address;
The column Address will be removed from the table student.

DROP TABLE Command


DROP TABLE command is used to delete / drop a table
permanently but we can not drop a table if it contains records.

That is first all the rows of the table have to be deleted and
only then the table can be dropped.

The general syntax of this command is:-


DROP TABLE <table_name>;
e.g DROP TABLE student;
This command will remove the table student
CREATE VIEW Command
A View is not a part of the database's physical
representation. It is created on a table or another
view. It is precompiled, so that data retrieval behaves
faster, and also provides a secure accessibility
mechanism. So it is called a VIRTUAL TABLE.
In SQL we can create a view of the already existing table that
contains specific attributes of the table.

CREATE VIEW vstudent AS SELECT Roll_no, Name,


Class FROM student;
The above command create a virtual table (view)
named vstudent that has three attributes as
mentioned and all the rows under those attributes as
in student table.
We can also create a view from an existing
table based on some specific conditions, like

CREATE VIEW vstudent AS SELECT


Roll_no, Name, Class FROM student WHERE
City <>’Jammu’;

You might also like