0% found this document useful (0 votes)
32 views23 pages

Built in Functions

The document provides an overview of SQL functions in Oracle, detailing single row functions, character functions, number functions, date functions, conversion functions, and grouping functions. It explains the syntax and usage of various functions, including examples for clarity. Additionally, it discusses the GROUP BY and HAVING clauses for organizing and filtering grouped data.

Uploaded by

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

Built in Functions

The document provides an overview of SQL functions in Oracle, detailing single row functions, character functions, number functions, date functions, conversion functions, and grouping functions. It explains the syntax and usage of various functions, including examples for clarity. Additionally, it discusses the GROUP BY and HAVING clauses for organizing and filtering grouped data.

Uploaded by

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

Working with tables:

Functions and grouping:


Built-in function:
 The built-in function provide a powerful tool for the
enhancement of a basic query.
 Functions are used for performing calculations on
data, converting data, modifying individual data,
manipulating a group of rows and formatting
columns.
In Oracle’s SQL there are two types of functions:
1. Single row function
2. Group function or aggregate function
1. Single row function:
The single row functions take different type of
arguments, work on a data item from each row and return
one value for each row.
The arguments are in the form of a constant
value, variable name, column, and/or expression.
Syntax:
Function(column/expression[argument1,
argument2,…,])
Various type of single row function:-
1. Character function
2. Number function
3. Data function
4. Conversion function
5. Grouping function or General function
1.Character function:
The character function perform case conversion or
character manipulation. The case conversion character
functions change a string or character type column data’s
case.
There are two types of character function:
1.Case-Manipulative Function(LOWER, UPPER,
INITCAP)

2. Character-Manipulative Function(CONCAT,
LENGTH, SUBSTR, INSTR, LPAD, RPAD, TRIM, and

REPLACE).
1.Case-Manipulative Function
• LOWER:-
• Syntax: LOWER(SQL course)
• Query: SELECT LOWER (‘FUNCTION’) FROM DUAL;
• Output: function
• Query: SELECT LOWER (‘DATABASE@456’)FROM DUAL;
• Output: database@456
• UPPER:-
• Query: SELECT UPPER (‘single’) FROM DUAL;
• Output: SINGLE
• INITCAP:-
• Query: SELECT INITCAP (‘java coding’) FROM DUAL;
• Output: Java Coding
2. Character-Manipulative Function
• CONCAT:-
• Syntax: CONCAT(‘String1’, ‘String2’)
• Query: SELECT CONCAT(‘computer’, ‘science’)FROM DUAL;
• Output: computerscience
• Query: SELECT CONCAT(‘null’, ‘Android’)FROM DUAL;
• Output: Android
• LENGTH:-
• Syntax: LENGTH(Column | Expression)
• Query: SELECT LENGTH(‘Pen’) FROM DUAL;
• Output: 3
• INSTR: This function returns numeric position of a character or a
string in a given string.
• Syntax: INSTR(column | Expression, ‘String’,[,m], [n])
• Query: SELECT INSTR(‘Sun light’, ‘lig’, 1, 2)FROM DUAL;
• Output: 5
• LPAD and RPAD: These function return the string padded the left
or right as per the use.
• Syntax: LPAD(Column | Expression, n, ‘String’)
• Query: SELECT LPAD(‘100’, 5, ‘*’) FROM DUAL;
• Output: **100
• Query: SELECT RPAD(‘5000’, 7, ‘*’) FROM DUAL;
• Output: 5000***
• SUBSTR: This function returns a portion of a string from a given
start point to an end point.
• Syntax: (‘String’, start-index, length_of_extracted_string)
• Query: SELECT SUBSTR (‘Database Management System’, 9)
FROM DUAL;
• Output: Management System
• Query: SELECT SUBSTR(‘Database Management System’, 9, 7)
FROM DUAL;
• Output: Managem
• TRIM:
• Syntax: TRIM(leading | Trailing | Both, trim_character FROM
trim_source)
• Query: SELECT TRIM(‘G’ FROM ‘GEEKS’) FROM DUAL;
• Output: EEKS
• REPLACE:
• Syntax: REPLACE(Text, search_string, replacement_string)
• Query: SELECT REPLACE(‘Data Management’, ‘Data’,
‘Database’)FROM DUAL;
• Output: Database Management
Number function (or) numeric:-
1. ABS
Returns the absolute positive value of an expression.
• Syntax: ABS(expression)
• Query: SELECT ABS(-1.0) Output: 10
2. CEIL
Return the smallest integer less than or equal to the specified
numeric expression
• Query: SELECT CELING(223.45)
• Output: 224
3. GREATEST(): It returns the greatest value in a list of expressions.
• Query: SELECT GREATEST(30, 2, 36, 81, 125);Output: 125
4. LEAST(): It returns the smallest value in a list of expressions.
• Query: SELECT LEAST(30, 2, 36, 81, 125);Output: 2
5. MOD(): It returns the remainder of n divided by m.
• Query: SELECT MOD(18, 4);Output: 2
6. POWER(): It returns m raised to the nth power.
Query: SELECT POWER(4, 2);Output: 16
7. ROUND(): It returns a number rounded to a certain number of
decimal places.
• Syntax: SELECT ROUND(5.553);Output: 6
8. SIGN(): It returns a value indicating the sign of a number.
• Syntax: SELECT SIGN(255.5);Output: 1
Date function:
• NOW(): Returns the current date and time.
• Example:SELECT NOW(); Output:2017-01-13 08:03:52
• CURDATE(): Returns the current date.
• Example:SELECT CURDATE(); Output:2017-01-13
• CURTIME(): Returns the current time.
• Example:SELECT CURTIME(); Output:08:05:15
• DATE(): Extracts the date part of a date or date/time expression.
• Example: Test
‘Id Name BirthTime
4120 Pratik 1996-09-26 16:44:15.581
• SELECT Name, DATE(BirthTime) AS BirthDate FROM Test;
• Output: Name BirthDate
Pratik 1996-09-26
• EXTRACT(): Returns a single part of a date/time.
• Syntax:EXTRACT(unit FROM date);
• Query: SELECT Name, Extract(DAY FROM BirthTime) AS
BirthDay FROM Test;
• Output: Name BirthDay
Pratik 26
• DATE_ADD() : Adds a specified time interval to a date
Syntax:DATE_ADD(date, INTERVAL expr type);
• Query: SELECT Name, DATE_ADD(BirthTime, INTERVAL 1
YEAR) AS BirthTimeModified FROM Test;
• Output: Name BirthTimeModified
Pratik 1997-09-26 16:44:15.581
Conversion function:
• In some cases, the Server uses data of one type where it expects
data of a different data type.
• This can happen when the Server can automatically convert the
data to the expected data type.
• This data type conversion can be done implicitly by the Server, or
explicitly by the user.

From To

VARCHAR2 or CHAR NUMBER

VARCHAR2 or CHAR DATE

DATE VARCHAR2

NUMBER VARCHAR2
Nested function:
• Single row functions can be nested within each other.
• In nested functions the innermost function is evaluated
first and then evaluation moves outward.
• The outmost function is evaluated last.

Id name Subject class_id Salary


1 Elisabeth History 3 2,500
2 Robert Language Null 2000
3 John Science 1 2350
4 Sara Maths 2 3000
Query:
Select subject, max(salary _by _ subject. Avg _ salary) As max
salary from(select subject, avg (monthly_ salary) As avg _ salary
From teachers GROUP BY subject) salary _ by_ subject;

Output:
Subject Max _ salary
Maths 3000
Group function:
• The GROUP BY Statement in SQL is used to arrange identical
data into groups with the help of some functions.
• GROUP BY clause is used with the SELECT statement.
• In the query, GROUP BY clause is placed after the WHERE
clause.
• In the query, GROUP BY clause is placed before ORDER BY
clause if used any.
• In the query , Group BY clause is placed before Having clause .
• Place condition in the having clause
Grouping functions are:
SUM
AVG
MAX
MIN
COUNT
Syntax:
SELECT column1, function_name(column2) from table_name
where condition GROUP BY column1,column2 order by column1,
column2;
• Function_name: Name of the function used for example sum,avg.
• Table_name: Name of the table.
• Condition: Condition used.
SELECT NAME, SUM(SALARY) FROM Employee GROUP BY NAME;
HAVING Clause:
• We know that WHERE clause is used to place conditions on
columns but what if we want to place conditions on groups? This
is where HAVING clause comes into use.
• We can use HAVING clause to place conditions to decide which
group will be the part of final result-set. Also we can not use the
aggregate functions like SUM(), COUNT() etc.
• with WHERE clause. So we have to use HAVING clause if we
want to use any of these functions in the conditions.
Syntax:
Select column1,function_name(column2) from table_name
where condition GROUP BY column1, column2 HAVING condition
order by column1,column2;
Query:
Select name, sum(salary) from employee Group by name
Having sum(salary)>3000;

Output:
Harsh 5500

You might also like