1 views

Uploaded by amrit thind

- SQL Queries
- Query-3G Daily KPI Report_Rollout_Cluster_Hourly
- Oracle tuning Session Longops
- Database Size
- Tr Locker
- Portable Settings
- Mahal Aga
- Solaris_Container_LAB
- codigo
- Query Create Table
- 8_High-Level Language Programming With SCL and SIMATIC S7-1200-En
- Experiment 1
- Test File
- Copy of Module 1_Quiz Data_Pareto
- XML Report Details Supplier Cost
- COPA Report Troubleshoot
- s Databaseetyweryt
- Notifications From Oracle Workflows
- PowerSource Forums - Student Duplicates
- Leepit Whitepaper Plan

You are on page 1of 62

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

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)

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

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)

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)

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.

COUNT(EMPNO)

------------

16

List the number of different names in the emp table.

COUNT(DISTINCTENAME)

--------------------

16

List the number of departments in the employee table .

COUNT(DISTINCTDEPTNO)

---------------------

3

SUM(x)

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.

The output is:

SUM(SAL)

---------

29025

AVG(x)

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.

Example:

emp table .

SQL>select avg(sal) ,count(sal) from emp ;

MIN(x)

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 .

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 .

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

emp ;

Exercise :

•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:

[WHERE condition]

[GROUP BY group_by_expression]

[ORDER BY column];

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:

deptno;

DEPTNO SUM(SAL)

10 2916.6667

20 2175

30 1566.6667

Here is how this SELECT statement, containing a GROUP BY clause,

is evaluated:

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 maximum salary for each dept.

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;

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.

• 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:

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

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.

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

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

emp

WHERE deptno=20

GROUP by job

HAVING AVG(sal)>1000;

The output is:

SQL> SELECT job, SUM(sal) PAYROLL

FROM emp

WHERE job NOT LIKE 'SALE%'

GROUP BY job

HAVING SUM(sal)>5000

ORDER BY SUM(sal);

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

Only display those where supplied qty is greater than100

Display info in descending order of Qty

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

IN(SELECT PNO FROM P WHERE COLOR=‘RED’) GROUP

BY PNO HAVING SUM(QTY)>1000 ORDER BY PNO;

- SQL QueriesUploaded byramprakash_kota
- Query-3G Daily KPI Report_Rollout_Cluster_HourlyUploaded byabuzarshazli
- Oracle tuning Session LongopsUploaded byshubhrobhattacharya
- Database SizeUploaded byStl Server
- Tr LockerUploaded byYanyz Aquariuslove
- Mahal AgaUploaded byrandall_razon10
- Solaris_Container_LABUploaded bytee.mara2907
- 8_High-Level Language Programming With SCL and SIMATIC S7-1200-EnUploaded byThong Nguyen
- Portable SettingsUploaded bySebastián Sánchez Mendoza
- codigoUploaded byMarx UT
- Experiment 1Uploaded byadamwaiz
- XML Report Details Supplier CostUploaded byNisheeth Ranjan
- Query Create TableUploaded byAdec Pratama Putra
- Copy of Module 1_Quiz Data_ParetoUploaded bybattlewar111
- COPA Report TroubleshootUploaded byshastryc
- PowerSource Forums - Student DuplicatesUploaded byMuskegon ISD PowerSchool Support Consortium
- Test FileUploaded byChristine Droesser
- s DatabaseetywerytUploaded byGeorgios Puliarekos
- Notifications From Oracle WorkflowsUploaded bynagarajuvcc123
- Leepit Whitepaper PlanUploaded bySimon Lapscher
- Cst900.CblUploaded byRogério Moraes de Aguiar
- Informatica_filter and Router TransformationUploaded bynagaaytha
- Zfi r002 TopUploaded by3vilcat
- Muse LogUploaded byRizky Maulana S
- scribenotes-week1-SectionA.pdfUploaded bySri Mukhesh Chowdary
- Subhash ProjectUploaded bysubhashbaraiya
- OBIEE-Essbase-The-Truth-about-Integration.pdfUploaded byMohamed Zaki
- Vishnu SrinivasUploaded bySasidhar Chowduru
- BoyaPeng CVUploaded byEmmaBYP
- Advanced Forms of Insert.Uploaded byAbhi_pops

- How to BindUploaded byAmir Wagdarikar
- Chapter 3 ExamUploaded byMilton Neves
- Mobile-IP goals,entities, packet delivery, encapsulations, tunnelling and DHCP (Unit-3)Uploaded byMukesh
- First Order Linear Homogeneous Fuzzy Ordinary Differential Equation Based on Lagrange Multiplier MethodUploaded bysankar_mondal02
- Swirl Test With GitUploaded bySeyed Mehdi Hoseiny
- ecvUploaded byFrancesco Lombardi
- VMware10.0.1-1379776Uploaded byNiklas Tokbollen Riddarlo
- VarunBiala ResumeUploaded byVarun Biala
- TM210TRE.40-EnG_Working With Automation Studio_V4200Uploaded byVladan Milojević
- IJETTCS-2014-04-24-121Uploaded byAnonymous vQrJlEN
- Railway Reservation SystemUploaded bysb_rameshbabu
- NetAnalysis-UsingEtherealUploaded by~kdm
- DSettlement User ManualUploaded byBluebelgian
- Evaluation of Scalar Value Estimation Techniques for 3D Medical ImagingUploaded byعلي علي
- Automatic Real Time Auditorium Power Supply Control.Uploaded byKishor Mhaske
- 1 - Proton Compiler ManualUploaded byAlexis26rg
- EWI USB Software Quickstart ManualUploaded bydfang72
- 2016 UK ECommerce Fraud ReportUploaded byashok
- 11g_app_devUploaded bykaushik4end
- WinterCorp Report Big Data What Does It Really CostUploaded byJames Felton Keith
- Sap Sd Course lineUploaded bynitish07singh
- xxxxxUploaded byDuddukuri Srikar
- Preps_5-1-1_RelNotesUploaded byIvan Ilic
- A ZEOS Basics Tutorial Not Only for FirebirdUploaded byRolly Ar
- Upgrade to Skype for Business 2015 Step by Step eBookUploaded byzawmoetun
- NII - IT SDLC - 1.0.T.01 Business Requirements SDLC Template_TEMPORALUploaded bykrishtal25
- LarCalc10_ch01_sec2Uploaded byZachary
- smcc2011Uploaded bybassalemi
- Optimal location and sizing of DG units to improve the voltage stability in the distribution system using Particle Swarm Optimization Algorithm with Time Varying Acceleration CoefficientsUploaded byGRD Journals
- Stateflow.pdfUploaded byjan.sakowicz9777