SlideShare ist ein Scribd-Unternehmen logo
1 von 62
Downloaden Sie, um offline zu lesen
Introduction to Oracle 
Functions and Group By 
Clause
Introduction to Oracle 
Functions 
Functions make the result of the query 
easier and are used to manipulate the data 
values. Functions can accept any number of 
constant values or variables. These 
variables or constant are called as 
arguments. SQL functions can perform 
different types of operations such as 
modify individual data items, convert 
column data types, format dates and 
numbers etc.
Categories of Functions 
Oracle functions are categorized into two categories: 
• Single Row/Scalar Functions 
• Group/Aggregate Functions 
Functions, which operate on single rows and return one 
value per row, are called as Scalar functions or Single 
Row Functions. 
On the other hand functions, which operate on set of 
values to give one result, are called as Group Functions 
or Aggregate Functions.
Single-Row Functions (Scalar functions) 
These functions act on each row of the table and 
return a single value for each row selected. These 
functions may accept one or more arguments and can 
return a value of different data type than that it 
has accepted.
Classification of Single Row Functions 
Single Row Functions can be classified into the following 
categories: 
(i) Character 
(ii) Number 
(iii) Date 
(iv) Conversion 
(v) General
Character Functions 
•
• length(x) 
It returns the length of the string x. 
Example: 
SQL> Select LENGTH ('Ipso Facto') ergo FROM dual;
• ltrim(string[,char(s)]) 
It removes all the blank spaces from the left side of the 
string if no char is specified. If we give a char, then it 
removes the leading occurrences of that character from the 
string.
• Translate(char,find,new) 
This function is used to find a char and replace it with new 
character. All occurrences of find are replaced by the 
character in new.
• floor(x) 
Where x is a number. This function returns the largest integer that is less than or equal to n. FLOOR round 
down to a whole number.
• round(x[,y]) 
It rounds off x to the decimal precision of y. If y is negative, rounds to the precision of y places to the left of the 
decimal point.
• sqrt(x) 
This function returns the square root of the given number x. If the given number x is negative or NULL then the result is 
NULL. 
Example: 
SQL>select sqrt(36) as square_root from dual;
Date Functions
SQL> SELECT TO_CHAR(SYSDATE,'HH') HOUR, 
TO_CHAR(SYSDATE,'MI') MIN,TO_CHAR(SYSDATE,'SS') 
SEC FROM DUAL; 
The output is: 
HO MI SE 
-- -- -- 
03 01 16 
SQL> SELECT TO_DATE('15-MAR-1999','DD-MON-YYYY') 
FROM DUAL; 
SQL>SELECT TO_NUMBER('49583') FROM DUAL;
General functions 
SQL>Select greatest(-2 ,10,’7’) from dual ; 
SQL>Select least(‘ABCD’,‘abcd’,’xyz’)from dual ;
SQL>Select user from dual;
Aggregate Functions (Group 
Functions) 
These functions are used to produce summarized 
results. They are applied on set of rows to give you 
single value as a result. A group function allows you to 
perform a data operation on several values in a column 
of data as though the column was one collective group 
of data. These functions are called group functions 
also, because they are often used in a special clause of 
select statements called a group by clause.
COUNT (x) 
This function returns the number of rows or non-null values 
for column x. When we use * in place of x, it returns the 
total number of rows in the table. 
Syntax: 
count([distinct|all]column name) 
Example: 
1. Count the number of employees in the emp table. 
SQL>Select count(empno) from emp; 
The Output is: 
COUNT(EMPNO) 
------------ 
16
List the number of different names in the emp table. 
SQL>Select count (distinct ename) from emp; 
The output is: 
COUNT(DISTINCTENAME) 
-------------------- 
16 
List the number of departments in the employee table . 
SQL>Select count( distinct deptno) from emp; 
The output is: 
COUNT(DISTINCTDEPTNO) 
--------------------- 
3
SUM(x) 
This function returns the sum of values for the 
column x. This function is applied on columns having numeric 
datatype and it returns the numeric value. 
syntax : sum([distinct|all]column name) 
Example: 
List the total salary paid to the employees in the emp table. 
SQL>select sum(sal) from emp ; 
The output is: 
SUM(SAL) 
--------- 
29025
AVG(x) 
This function returns the average of values for 
the column x. This function is applied on columns having 
numeric datatype and it returns the numeric value. It ignores 
the null values in the column x. 
syntax : avg([distinct|all]column name) 
Example: 
List the average salary and the number of employees in the 
emp table . 
SQL>select avg(sal) ,count(sal) from emp ;
MIN(x) 
This function returns the minimum of values for the column 
x for all the rows .this function can be applied on any 
datatype . 
syntax : min([distinct|all]column name) 
Example: 
List the minimum salary in the emp table . 
SQL>select min(sal) from emp ; 
The output is: 
MIN(SAL) 
-------- 
800
MAX(x) 
This function returns the maximum of values for 
the column x for all the rows .this function can be applied on 
any datatype. 
syntax : max([distinct|all]column name) 
Example: 
List the maximum salary and commission in the emp table . 
SQL>select max(sal) ,max(comm) from emp ;
Note : The avg() and sum() functions will always be applied 
on numeric datatype while min() and max() functions can be 
applied on any datatype. 
Example 
SQL>select avg(sal),sum(sal),min(ename),max(ename) from 
emp ;
Exercise : 
•list the names of the employees earning minimum salary . 
•list the names of the employees earning second highest 
salary . 
•list the details of the employees who earn salary greater 
than the average salary . also count their number . 
•count the number of employees whose salary is equal to 
the highest salary . 
•list the number of employees ,their average salary 
,minimum salary and maximum salary in the employee 
table.
Grouping Data with GROUP 
BY 
GROUP BY clause is used to group or categorize the data. In other 
words it divide rows in a table into smaller groups. We can then 
use the group functions to return summary information for each 
group. 
If no GROUP BY clause is specified, then the default grouping is 
the entire result set. When the query executes and the data is 
fetched, it is grouped based on the GROUP BY clause and the 
group function is applied.
Syntax: 
SELECT column,group_function(column) FROM table 
[WHERE condition] 
[GROUP BY group_by_expression] 
[ORDER BY column]; 
Here, group_by_expression specifies columns whose values 
determine the basis for grouping rows.
For example, If we have to find the total salary of each department 
manually, first we group the records on the basis of department 
number and then we apply the sum function on salary of each group 
to obtain the required result. Similarly in SQL we apply the GROUP 
BY clause on deptno and then calculate the total salary for each group 
by Sum(sal) function as shown below: 
SQL>SELECT deptno, Sum(sal) FROM emp GROUP BY 
deptno; 
The output is: 
DEPTNO SUM(SAL) 
10 2916.6667 
20 2175 
30 1566.6667
Here is how this SELECT statement, containing a GROUP BY clause, 
is evaluated: 
• The SELECT clause specifies the columns to be retrieved i.e 
Department number column in the EMP table, the sum of all the 
salaries in the group you specified in the GROUP By clause 
• The FROM clause specifies the tables that the database must 
access i.e EMP table. 
• The WHERE clause specifies the rows to be retrieved. Since 
there is no WHRE clause, by default all rows are retrieved.
The GROUP BY clause specifies how the rows should be grouped. 
Department number groups the rows, so the AVG function that is 
being applied to the salary column will calculate the average salary 
for each department. 
• List the average salary of each job in the emp table. 
SQL>SELECT JOB,AVG(SAL) FROM EMP GROUP BY JOB; 
• 
List the maximum salary for each dept. 
SQL>SELECT DEPTNO,MAX(SAL) FROM EMP GROUP BY 
DEPTNO;
Grouping by more than one 
column 
Sometimes there is a need to see results for groups within groups. 
For example if we have to find the total salary being paid to each 
job title, within each department. Then there is a need to having 
grouping on department number and within each department 
number grouping on the basis of job or in other words there is a 
need for grouping within a group. 
Thus, the EMP table is grouped first by department number, and 
within that grouping, it is grouped by job title. For example, the two 
clerks in department 20 are grouped together and a single result 
(total salary) is produced for all clerks people within that group.
SQL> SELECT deptno,job,sum(sal) FROM emp GROUP BY 
deptno, job; 
The output is:
By above example it is clear that we can return summary results for 
groups and subgroups by listing more than one GROUP BY column. 
We can determine the default sort order of the results by the order of 
the columns in the GROUP BY clause. 
The SELECT clause specifies the column to be retrieved: 
• Department number in the EMP table 
• Job title in the EMP table 
• The sum of all the salaries in the group that you specified in the 
GROUP BY clause 
• The FROM clause specifies the tables that the database must 
access the EMP table 
• The GROUP BY clause specifies how we must group the rows 
First, department number groups the rows. Second, within the 
department number groups, the rows are grouped by job title. So, the 
SUM function is being applied to the salary column for all job titles 
within each department number group.
Illegal Queries Using Group 
Functions 
Whenever you use a mixture of individual items (DEPTNO) and 
group functions (COUNT) in the same select statement, you must 
include a Group By clause that specifies the individual items (in 
this case, DEPTNO). If the GROUP By clause is missing, then the 
error message “not a single-group group function” appears and an 
asterisk (*) points to the offending column. You can correct the 
error by adding the GROUP BY clause.
For example, following is the illegal query: 
SQL> SELECT deptno, COUNT(ename) FROM emp; 
The Output will be: 
Column missing in the Group By clause 
Select deptno,count(ename) 
* 
ERROR at line 1: 
ORA-00937: not a single-group group function In above select 
statement individual items DEPTNO and group function COUNT 
appears in the same SELECT statement without GROUP BY clause 
which results error, it can be corrected by adding the GROUP BY 
clause as shown below: 
SQL> SELECT deptno,COUNT(ename) FROM emp Group By deptno;
DEPTNO COUNT(ENAME) 
10 3 
20 5 
30 6 
Note: 
Any column or expression in the SELECT list that is not an 
aggregate function must be in the GROUP By clause.
Restricting Group Results 
As we use the WHERE clause to restrict the rows that we select, 
we can use the HAVING clause to restrict groups. 
For example: To find the maximum salary of each department, 
but show only the departments that have a maximum salary of 
more than Rs.2900, we need to do the following. 
• Find the maximum salary for each department by grouping 
by department number. 
• Restrict the groups to those departments with a maximum 
salary greater the Rs.2900.
Syntax: 
SELECT column, group_function 
FROM table 
{WHERE condition] 
[GROUP BY group_by_expression] 
[HAVING group_condition] 
[ORDER BY Column];
Here we use the HAVING clause to specify which groups are to be 
displayed. Therefore, we further restrict the groups on the basis of 
aggregate information. 
In the syntax: 
HAVING clause restricts the groups of rows returned to those 
groups for which the specified condition is TRUE 
The Oracle Server performs the following steps when you use the 
HAVING clause:
• Rows are grouped 
• The group function is applied to the group. 
• The groups that match the criteria in the HAVING clause are 
displayed. 
The HAVING clause can precede the GROUP By clause, but it is 
recommended that you place the GROUP By clause first because it is 
more logical. Groups are formed and group functions are calculated 
before the HAVING clause is applied to the groups in the SELECT 
list.
For example: To find the maximum salary of each department, but 
show only the departments that have a maximum salary of more than 
Rs.2900 
SQL> SELECT deptno,max(sal) FROM emp 
GROUP BY deptno Having max(sal)>2900; 
The output is: 
DEPTNO MAX(SAL) 
10 5000 
20 3000
Use of WHERE clause with GROUP BY clause 
List the total salary, maximum and minimum salary and the average 
salary of employees job wise, for department number 20 and 
display only those rows having average salary greater than 1000 
SQL>SELECT job, SUM(sal), avg(sal), max(sal), min(sal) from 
emp 
WHERE deptno=20 
GROUP by job 
HAVING AVG(sal)>1000;
The output is: 
JOB SUM(SAL) AVG(SAL) MAX(SAL) MIN(SAL) 
ANALYST 6000 3000 3000 3000 
MANAGER 2975 2975 2975 2975
SQL> SELECT job, SUM(sal) PAYROLL 
FROM emp 
WHERE job NOT LIKE 'SALE%' 
GROUP BY job 
HAVING SUM(sal)>5000 
ORDER BY SUM(sal); 
The output is: 
JOB PAYROLL 
ANALYST 6000 
MANAGER 8275 
The above query displays the job title and total monthly salary for 
each job title with a total payroll exceeding Rs.5000. The example 
excludes salespeople and sorts the list by the total monthly salary.
Display total no of suppliers supplying red part 
Display total qty supplied by by each supplier 
Display total Qty supplied for each part excluding P3 
Only display those where supplied qty is greater than100 
Display info in descending order of Qty 
Select Pno, Sum(QTY) from P,SP WHERE PNO<>’P3’ 
GROUP BY PNO HAVING SUM(QTY)>1000 ORDER BY 
PNO;
Display total Qty supplied for each part excluding part having red 
color, Only display those where supplied qty is greater than100 
Select Pno, Sum(QTY) from P,SP WHERE PNO NOT 
IN(SELECT PNO FROM P WHERE COLOR=‘RED’) GROUP 
BY PNO HAVING SUM(QTY)>1000 ORDER BY PNO;

Weitere ähnliche Inhalte

Was ist angesagt? (20)

SQL commands
SQL commandsSQL commands
SQL commands
 
SQL Queries
SQL QueriesSQL Queries
SQL Queries
 
Introduction to sql
Introduction to sqlIntroduction to sql
Introduction to sql
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 
Basic SQL and History
 Basic SQL and History Basic SQL and History
Basic SQL and History
 
Sql subquery
Sql  subquerySql  subquery
Sql subquery
 
Single-Row Functions in orcale Data base
Single-Row Functions in orcale Data baseSingle-Row Functions in orcale Data base
Single-Row Functions in orcale Data base
 
Database Objects
Database ObjectsDatabase Objects
Database Objects
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Packages in PL/SQL
Packages in PL/SQLPackages in PL/SQL
Packages in PL/SQL
 
SQL Overview
SQL OverviewSQL Overview
SQL Overview
 
MySql slides (ppt)
MySql slides (ppt)MySql slides (ppt)
MySql slides (ppt)
 
SQL subquery
SQL subquerySQL subquery
SQL subquery
 
PL/SQL Fundamentals I
PL/SQL Fundamentals IPL/SQL Fundamentals I
PL/SQL Fundamentals I
 
PL/SQL TRIGGERS
PL/SQL TRIGGERSPL/SQL TRIGGERS
PL/SQL TRIGGERS
 
SQL Basics
SQL BasicsSQL Basics
SQL Basics
 
SQL
SQLSQL
SQL
 
Stored procedure
Stored procedureStored procedure
Stored procedure
 
Trigger
TriggerTrigger
Trigger
 
4. plsql
4. plsql4. plsql
4. plsql
 

Andere mochten auch

Oracle SQL Functions
Oracle SQL FunctionsOracle SQL Functions
Oracle SQL FunctionsA Data Guru
 
Functions oracle (pl/sql)
Functions oracle (pl/sql)Functions oracle (pl/sql)
Functions oracle (pl/sql)harman kaur
 
Introduction To Oracle Sql
Introduction To Oracle SqlIntroduction To Oracle Sql
Introduction To Oracle SqlAhmed Yaseen
 
SQL practice questions set - 2
SQL practice questions set - 2SQL practice questions set - 2
SQL practice questions set - 2Mohd Tousif
 
Sql queries with answers
Sql queries with answersSql queries with answers
Sql queries with answersvijaybusu
 

Andere mochten auch (6)

Oracle: Basic SQL
Oracle: Basic SQLOracle: Basic SQL
Oracle: Basic SQL
 
Oracle SQL Functions
Oracle SQL FunctionsOracle SQL Functions
Oracle SQL Functions
 
Functions oracle (pl/sql)
Functions oracle (pl/sql)Functions oracle (pl/sql)
Functions oracle (pl/sql)
 
Introduction To Oracle Sql
Introduction To Oracle SqlIntroduction To Oracle Sql
Introduction To Oracle Sql
 
SQL practice questions set - 2
SQL practice questions set - 2SQL practice questions set - 2
SQL practice questions set - 2
 
Sql queries with answers
Sql queries with answersSql queries with answers
Sql queries with answers
 

Ähnlich wie Introduction to oracle functions

MySQL-commands.pdf
MySQL-commands.pdfMySQL-commands.pdf
MySQL-commands.pdfssuserc5aa74
 
Database Query Using SQL_ip.docx
Database Query Using SQL_ip.docxDatabase Query Using SQL_ip.docx
Database Query Using SQL_ip.docxVandanaGoyal21
 
Group by clause mod
Group by clause modGroup by clause mod
Group by clause modNitesh Singh
 
Oracle sql functions
Oracle sql functionsOracle sql functions
Oracle sql functionsVivek Singh
 
Oracle_Analytical_function.pdf
Oracle_Analytical_function.pdfOracle_Analytical_function.pdf
Oracle_Analytical_function.pdfKalyankumarVenkat1
 
Advanced functions in PL SQL
Advanced functions in PL SQLAdvanced functions in PL SQL
Advanced functions in PL SQLHosein Zare
 
Introduction Oracle Database 11g Release 2 for developers
Introduction Oracle Database 11g Release 2 for developersIntroduction Oracle Database 11g Release 2 for developers
Introduction Oracle Database 11g Release 2 for developersLucas Jellema
 
Oracle tips and tricks
Oracle tips and tricksOracle tips and tricks
Oracle tips and tricksYanli Liu
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Trainingbixxman
 
Mysqlppt
MysqlpptMysqlppt
MysqlpptReka
 
Analytic & Windowing functions in oracle
Analytic & Windowing functions in oracleAnalytic & Windowing functions in oracle
Analytic & Windowing functions in oracleLogan Palanisamy
 
SQL select statement and functions
SQL select statement and functionsSQL select statement and functions
SQL select statement and functionsVikas Gupta
 

Ähnlich wie Introduction to oracle functions (20)

Introduction to Oracle Functions--(SQL)--Abhishek Sharma
Introduction to Oracle Functions--(SQL)--Abhishek SharmaIntroduction to Oracle Functions--(SQL)--Abhishek Sharma
Introduction to Oracle Functions--(SQL)--Abhishek Sharma
 
5. Group Functions
5. Group Functions5. Group Functions
5. Group Functions
 
MySQL-commands.pdf
MySQL-commands.pdfMySQL-commands.pdf
MySQL-commands.pdf
 
Database Query Using SQL_ip.docx
Database Query Using SQL_ip.docxDatabase Query Using SQL_ip.docx
Database Query Using SQL_ip.docx
 
Sql query [select, sub] 4
Sql query [select, sub] 4Sql query [select, sub] 4
Sql query [select, sub] 4
 
Group by clause mod
Group by clause modGroup by clause mod
Group by clause mod
 
Module03
Module03Module03
Module03
 
SQL.pptx
SQL.pptxSQL.pptx
SQL.pptx
 
Lab3 aggregating data
Lab3   aggregating dataLab3   aggregating data
Lab3 aggregating data
 
Oracle sql functions
Oracle sql functionsOracle sql functions
Oracle sql functions
 
Mysql1
Mysql1Mysql1
Mysql1
 
Oracle_Analytical_function.pdf
Oracle_Analytical_function.pdfOracle_Analytical_function.pdf
Oracle_Analytical_function.pdf
 
Advanced functions in PL SQL
Advanced functions in PL SQLAdvanced functions in PL SQL
Advanced functions in PL SQL
 
Introduction Oracle Database 11g Release 2 for developers
Introduction Oracle Database 11g Release 2 for developersIntroduction Oracle Database 11g Release 2 for developers
Introduction Oracle Database 11g Release 2 for developers
 
Oracle tips and tricks
Oracle tips and tricksOracle tips and tricks
Oracle tips and tricks
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
 
Analytic & Windowing functions in oracle
Analytic & Windowing functions in oracleAnalytic & Windowing functions in oracle
Analytic & Windowing functions in oracle
 
SQL select statement and functions
SQL select statement and functionsSQL select statement and functions
SQL select statement and functions
 
Advance excel
Advance excelAdvance excel
Advance excel
 

Mehr von Nitesh Singh

Mehr von Nitesh Singh (20)

Risk taking and emotions
Risk taking and emotionsRisk taking and emotions
Risk taking and emotions
 
Project report RAILWAY TICKET RESERVATION SYSTEM SAD
Project report RAILWAY TICKET RESERVATION SYSTEM SADProject report RAILWAY TICKET RESERVATION SYSTEM SAD
Project report RAILWAY TICKET RESERVATION SYSTEM SAD
 
The real comedy behind comedy
The real comedy behind comedyThe real comedy behind comedy
The real comedy behind comedy
 
Project report Rs Dry celaners
Project report Rs Dry celaners Project report Rs Dry celaners
Project report Rs Dry celaners
 
BIG DATA ANALYSIS
BIG DATA ANALYSISBIG DATA ANALYSIS
BIG DATA ANALYSIS
 
Udp vs-tcp
Udp vs-tcpUdp vs-tcp
Udp vs-tcp
 
Routing protocols-network-layer
Routing protocols-network-layerRouting protocols-network-layer
Routing protocols-network-layer
 
Routers vs-switch
Routers vs-switchRouters vs-switch
Routers vs-switch
 
New udp
New udpNew udp
New udp
 
I pv4 format
I pv4 formatI pv4 format
I pv4 format
 
I pv4 addressing
I pv4 addressingI pv4 addressing
I pv4 addressing
 
Hub vs-switch
Hub vs-switchHub vs-switch
Hub vs-switch
 
Ftp
FtpFtp
Ftp
 
Email ftp
Email ftpEmail ftp
Email ftp
 
Www and http
Www and httpWww and http
Www and http
 
Transmission main
Transmission mainTransmission main
Transmission main
 
Ta 104-topology
Ta 104-topologyTa 104-topology
Ta 104-topology
 
Ta 104-topology (1)
Ta 104-topology (1)Ta 104-topology (1)
Ta 104-topology (1)
 
Ta 104-tcp
Ta 104-tcpTa 104-tcp
Ta 104-tcp
 
Ta 104-media-3
Ta 104-media-3Ta 104-media-3
Ta 104-media-3
 

Kürzlich hochgeladen

Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Christo Ananth
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapRishantSharmaFr
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueBhangaleSonal
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756dollysharma2066
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdfSuman Jyoti
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...Call Girls in Nagpur High Profile
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 

Kürzlich hochgeladen (20)

Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 

Introduction to oracle functions

  • 1. Introduction to Oracle Functions and Group By Clause
  • 2. Introduction to Oracle Functions Functions make the result of the query easier and are used to manipulate the data values. Functions can accept any number of constant values or variables. These variables or constant are called as arguments. SQL functions can perform different types of operations such as modify individual data items, convert column data types, format dates and numbers etc.
  • 3. Categories of Functions Oracle functions are categorized into two categories: • Single Row/Scalar Functions • Group/Aggregate Functions Functions, which operate on single rows and return one value per row, are called as Scalar functions or Single Row Functions. On the other hand functions, which operate on set of values to give one result, are called as Group Functions or Aggregate Functions.
  • 4. Single-Row Functions (Scalar functions) These functions act on each row of the table and return a single value for each row selected. These functions may accept one or more arguments and can return a value of different data type than that it has accepted.
  • 5. Classification of Single Row Functions Single Row Functions can be classified into the following categories: (i) Character (ii) Number (iii) Date (iv) Conversion (v) General
  • 7.
  • 8. • length(x) It returns the length of the string x. Example: SQL> Select LENGTH ('Ipso Facto') ergo FROM dual;
  • 9. • ltrim(string[,char(s)]) It removes all the blank spaces from the left side of the string if no char is specified. If we give a char, then it removes the leading occurrences of that character from the string.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15. • Translate(char,find,new) This function is used to find a char and replace it with new character. All occurrences of find are replaced by the character in new.
  • 16.
  • 17.
  • 18.
  • 19. • floor(x) Where x is a number. This function returns the largest integer that is less than or equal to n. FLOOR round down to a whole number.
  • 20. • round(x[,y]) It rounds off x to the decimal precision of y. If y is negative, rounds to the precision of y places to the left of the decimal point.
  • 21. • sqrt(x) This function returns the square root of the given number x. If the given number x is negative or NULL then the result is NULL. Example: SQL>select sqrt(36) as square_root from dual;
  • 23.
  • 24.
  • 25.
  • 26.
  • 27. SQL> SELECT TO_CHAR(SYSDATE,'HH') HOUR, TO_CHAR(SYSDATE,'MI') MIN,TO_CHAR(SYSDATE,'SS') SEC FROM DUAL; The output is: HO MI SE -- -- -- 03 01 16 SQL> SELECT TO_DATE('15-MAR-1999','DD-MON-YYYY') FROM DUAL; SQL>SELECT TO_NUMBER('49583') FROM DUAL;
  • 28. General functions SQL>Select greatest(-2 ,10,’7’) from dual ; SQL>Select least(‘ABCD’,‘abcd’,’xyz’)from dual ;
  • 29.
  • 31. Aggregate Functions (Group Functions) These functions are used to produce summarized results. They are applied on set of rows to give you single value as a result. A group function allows you to perform a data operation on several values in a column of data as though the column was one collective group of data. These functions are called group functions also, because they are often used in a special clause of select statements called a group by clause.
  • 32. COUNT (x) This function returns the number of rows or non-null values for column x. When we use * in place of x, it returns the total number of rows in the table. Syntax: count([distinct|all]column name) Example: 1. Count the number of employees in the emp table. SQL>Select count(empno) from emp; The Output is: COUNT(EMPNO) ------------ 16
  • 33. List the number of different names in the emp table. SQL>Select count (distinct ename) from emp; The output is: COUNT(DISTINCTENAME) -------------------- 16 List the number of departments in the employee table . SQL>Select count( distinct deptno) from emp; The output is: COUNT(DISTINCTDEPTNO) --------------------- 3
  • 34. SUM(x) This function returns the sum of values for the column x. This function is applied on columns having numeric datatype and it returns the numeric value. syntax : sum([distinct|all]column name) Example: List the total salary paid to the employees in the emp table. SQL>select sum(sal) from emp ; The output is: SUM(SAL) --------- 29025
  • 35. AVG(x) This function returns the average of values for the column x. This function is applied on columns having numeric datatype and it returns the numeric value. It ignores the null values in the column x. syntax : avg([distinct|all]column name) Example: List the average salary and the number of employees in the emp table . SQL>select avg(sal) ,count(sal) from emp ;
  • 36. MIN(x) This function returns the minimum of values for the column x for all the rows .this function can be applied on any datatype . syntax : min([distinct|all]column name) Example: List the minimum salary in the emp table . SQL>select min(sal) from emp ; The output is: MIN(SAL) -------- 800
  • 37. MAX(x) This function returns the maximum of values for the column x for all the rows .this function can be applied on any datatype. syntax : max([distinct|all]column name) Example: List the maximum salary and commission in the emp table . SQL>select max(sal) ,max(comm) from emp ;
  • 38. Note : The avg() and sum() functions will always be applied on numeric datatype while min() and max() functions can be applied on any datatype. Example SQL>select avg(sal),sum(sal),min(ename),max(ename) from emp ;
  • 39. Exercise : •list the names of the employees earning minimum salary . •list the names of the employees earning second highest salary . •list the details of the employees who earn salary greater than the average salary . also count their number . •count the number of employees whose salary is equal to the highest salary . •list the number of employees ,their average salary ,minimum salary and maximum salary in the employee table.
  • 40. Grouping Data with GROUP BY GROUP BY clause is used to group or categorize the data. In other words it divide rows in a table into smaller groups. We can then use the group functions to return summary information for each group. If no GROUP BY clause is specified, then the default grouping is the entire result set. When the query executes and the data is fetched, it is grouped based on the GROUP BY clause and the group function is applied.
  • 41. Syntax: SELECT column,group_function(column) FROM table [WHERE condition] [GROUP BY group_by_expression] [ORDER BY column]; Here, group_by_expression specifies columns whose values determine the basis for grouping rows.
  • 42. For example, If we have to find the total salary of each department manually, first we group the records on the basis of department number and then we apply the sum function on salary of each group to obtain the required result. Similarly in SQL we apply the GROUP BY clause on deptno and then calculate the total salary for each group by Sum(sal) function as shown below: SQL>SELECT deptno, Sum(sal) FROM emp GROUP BY deptno; The output is: DEPTNO SUM(SAL) 10 2916.6667 20 2175 30 1566.6667
  • 43.
  • 44. Here is how this SELECT statement, containing a GROUP BY clause, is evaluated: • The SELECT clause specifies the columns to be retrieved i.e Department number column in the EMP table, the sum of all the salaries in the group you specified in the GROUP By clause • The FROM clause specifies the tables that the database must access i.e EMP table. • The WHERE clause specifies the rows to be retrieved. Since there is no WHRE clause, by default all rows are retrieved.
  • 45. The GROUP BY clause specifies how the rows should be grouped. Department number groups the rows, so the AVG function that is being applied to the salary column will calculate the average salary for each department. • List the average salary of each job in the emp table. SQL>SELECT JOB,AVG(SAL) FROM EMP GROUP BY JOB; • List the maximum salary for each dept. SQL>SELECT DEPTNO,MAX(SAL) FROM EMP GROUP BY DEPTNO;
  • 46. Grouping by more than one column Sometimes there is a need to see results for groups within groups. For example if we have to find the total salary being paid to each job title, within each department. Then there is a need to having grouping on department number and within each department number grouping on the basis of job or in other words there is a need for grouping within a group. Thus, the EMP table is grouped first by department number, and within that grouping, it is grouped by job title. For example, the two clerks in department 20 are grouped together and a single result (total salary) is produced for all clerks people within that group.
  • 47. SQL> SELECT deptno,job,sum(sal) FROM emp GROUP BY deptno, job; The output is:
  • 48.
  • 49. By above example it is clear that we can return summary results for groups and subgroups by listing more than one GROUP BY column. We can determine the default sort order of the results by the order of the columns in the GROUP BY clause. The SELECT clause specifies the column to be retrieved: • Department number in the EMP table • Job title in the EMP table • The sum of all the salaries in the group that you specified in the GROUP BY clause • The FROM clause specifies the tables that the database must access the EMP table • The GROUP BY clause specifies how we must group the rows First, department number groups the rows. Second, within the department number groups, the rows are grouped by job title. So, the SUM function is being applied to the salary column for all job titles within each department number group.
  • 50. Illegal Queries Using Group Functions Whenever you use a mixture of individual items (DEPTNO) and group functions (COUNT) in the same select statement, you must include a Group By clause that specifies the individual items (in this case, DEPTNO). If the GROUP By clause is missing, then the error message “not a single-group group function” appears and an asterisk (*) points to the offending column. You can correct the error by adding the GROUP BY clause.
  • 51. For example, following is the illegal query: SQL> SELECT deptno, COUNT(ename) FROM emp; The Output will be: Column missing in the Group By clause Select deptno,count(ename) * ERROR at line 1: ORA-00937: not a single-group group function In above select statement individual items DEPTNO and group function COUNT appears in the same SELECT statement without GROUP BY clause which results error, it can be corrected by adding the GROUP BY clause as shown below: SQL> SELECT deptno,COUNT(ename) FROM emp Group By deptno;
  • 52. DEPTNO COUNT(ENAME) 10 3 20 5 30 6 Note: Any column or expression in the SELECT list that is not an aggregate function must be in the GROUP By clause.
  • 53. Restricting Group Results As we use the WHERE clause to restrict the rows that we select, we can use the HAVING clause to restrict groups. For example: To find the maximum salary of each department, but show only the departments that have a maximum salary of more than Rs.2900, we need to do the following. • Find the maximum salary for each department by grouping by department number. • Restrict the groups to those departments with a maximum salary greater the Rs.2900.
  • 54. Syntax: SELECT column, group_function FROM table {WHERE condition] [GROUP BY group_by_expression] [HAVING group_condition] [ORDER BY Column];
  • 55. Here we use the HAVING clause to specify which groups are to be displayed. Therefore, we further restrict the groups on the basis of aggregate information. In the syntax: HAVING clause restricts the groups of rows returned to those groups for which the specified condition is TRUE The Oracle Server performs the following steps when you use the HAVING clause:
  • 56. • Rows are grouped • The group function is applied to the group. • The groups that match the criteria in the HAVING clause are displayed. The HAVING clause can precede the GROUP By clause, but it is recommended that you place the GROUP By clause first because it is more logical. Groups are formed and group functions are calculated before the HAVING clause is applied to the groups in the SELECT list.
  • 57. For example: To find the maximum salary of each department, but show only the departments that have a maximum salary of more than Rs.2900 SQL> SELECT deptno,max(sal) FROM emp GROUP BY deptno Having max(sal)>2900; The output is: DEPTNO MAX(SAL) 10 5000 20 3000
  • 58. Use of WHERE clause with GROUP BY clause List the total salary, maximum and minimum salary and the average salary of employees job wise, for department number 20 and display only those rows having average salary greater than 1000 SQL>SELECT job, SUM(sal), avg(sal), max(sal), min(sal) from emp WHERE deptno=20 GROUP by job HAVING AVG(sal)>1000;
  • 59. The output is: JOB SUM(SAL) AVG(SAL) MAX(SAL) MIN(SAL) ANALYST 6000 3000 3000 3000 MANAGER 2975 2975 2975 2975
  • 60. SQL> SELECT job, SUM(sal) PAYROLL FROM emp WHERE job NOT LIKE 'SALE%' GROUP BY job HAVING SUM(sal)>5000 ORDER BY SUM(sal); The output is: JOB PAYROLL ANALYST 6000 MANAGER 8275 The above query displays the job title and total monthly salary for each job title with a total payroll exceeding Rs.5000. The example excludes salespeople and sorts the list by the total monthly salary.
  • 61. Display total no of suppliers supplying red part Display total qty supplied by by each supplier Display total Qty supplied for each part excluding P3 Only display those where supplied qty is greater than100 Display info in descending order of Qty Select Pno, Sum(QTY) from P,SP WHERE PNO<>’P3’ GROUP BY PNO HAVING SUM(QTY)>1000 ORDER BY PNO;
  • 62. Display total Qty supplied for each part excluding part having red color, Only display those where supplied qty is greater than100 Select Pno, Sum(QTY) from P,SP WHERE PNO NOT IN(SELECT PNO FROM P WHERE COLOR=‘RED’) GROUP BY PNO HAVING SUM(QTY)>1000 ORDER BY PNO;