EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

Oracle Window Functions

By Priya PedamkarPriya Pedamkar

Home » Data Science » Data Science Tutorials » Oracle Tutorial » Oracle Window Functions

Oracle Window Functions

Introduction to Oracle Window Functions

Window Functions in Oracle performs aggregate function like operation but like in case of aggregate functions the result set groups query rows into a single row whereas if window function is applied the window function returns a result for each query row (query rows are rows on which function will be applied) meaning window operations do not collapse group of query rows into a single row but the result set returned by the window function contains result for each row which helps in performing data analysis calculations which gives it an edge over aggregate functions.

Oracle Window Functions

Below are the functions

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

OVER clause

The over clause is permitted for many aggregated functions. In the first example, we have a table named sales which consist of sales details based on the country and the year. If we use the aggregate function SUM (), it will return us a result set with a single row but we do not want the output to be of the single row instead we want it to have a result for each row. Let us look at the query for the same.

Code:

SELECT YEAR,
country,
profit,
SUM(profit) OVER() AS total_profit
FROM sales
ORDER BY country,
YEAR,
profit;

Output:

Over clause

Explanation: In the above query if you see we have used over clause which treats the query set as a single row meaning the query calculates the total sum but it does for each row as it will display the total sum with each row. As we can see in the output screenshot the window function produces a global sum but for each row.

PARTITION BY

This clause is used to split the result set into partitions on which window function is applied. In this example, we will use the PARTITION BY clause with the OVER BY clause. The aim of this example is to get the total profit from the sales table based on the country using a window function along with the aggregate function SUM(). We will use our previous table sales from the first example. Let us look at the query for the same.

Code:

SELECT year,
country,
profit,
SUM(profit) OVER(PARTITION BY COUNTRY) AS country_profit
FROM sales
ORDER BY country,
year,
profit;

Output:

Partition by claus

Explanation: If we see the above query, there is a slight difference from the one we used in the first example. Here we have used the PARTITION BY clause inside the OVER clause. This partition clause splits the rows by country and then producing the sum per country unlike in the previous case where it was producing a global sum for each row. The Partition BY clause allows the function to produce the sum for each partition row. As we can see in the output screenshot the window function produces the sum based on each country or for each partition row.

Row_Number ( )

This is a ranking type window function that assigns incrementing integers to each row or each row of the partition to which it is applied. We can use row_number function with non- aggregating functions also. In this example, we will use row_number function to find the rank/row number of profit based on the year and partitioned on the basis of country. So, each country is a partition. Let us look at the query.

Popular Course in this category
Sale
Oracle Training (14 Courses, 8+ Projects)14 Online Courses | 8 Hands-on Projects | 120+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (8,901 ratings)
Course Price

View Course

Related Courses
Oracle DBA Database Management System Training (2 Courses)All in One Financial Analyst Bundle - 250+ Courses, 40+ Projects

Code:

SELECT year,
country,
product,
profit,
ROW_NUMBER() OVER(PARTITION BY country ORDER BY YEAR DESC) AS row_num
FROM sales;

Output:

Oracle Window Functions3

Explanation: As we can see in the query the partition by clause is used to partition the row set based on the country and then we are using order_by clause to sort the partition rows based on the year in descending order. The row_number () function then generates a unique incremental rank number. As per the above output screenshot we can see that in the result set, the row number is displayed for each row of each partition.

RANK ( ) function

The rank ( ) function is also a type of ranking function also used as an analytical function. It calculates the rank of a value in a set of values. The rank is same for the same values. In this example, we are going to use the RANK ( ) function to calculate the rank based on the profit. For this we first partitioned the rows on the basis of the country so each country is one unique partition and then we use order by clause to sort it into descending order based on the profit column and then apply the rank () window function on that. Let us look at the query below

Code:

SELECT year,
country,
product,
profit,
RANK() OVER(PARTITION BY country ORDER BY PROFIT DESC) AS rnk
FROM sales;

Output:

Oracle Window Functions4

Explanation: As we can see in the above screenshot each row is provided with a rank.

DENSE_RANK ( ) function

The DENSE_RANK ( ) function is also a type of ranking function which calculates the rank of a value in a set of value and same values gets the same rank. The difference between rank () and dense_rank () is that in case of dense_rank () function, we get rank values as consecutive integers meaning that the next consecutive rank is not skipped in case of ties. Let us rerun the previous example with dense_rank () function instead of rank () function

Code:

SELECT year,
country,
product,
profit,
DENSE_RANK() OVER(PARTITION BY country ORDER BY PROFIT DESC) AS dense_rnk
FROM sales;

Let us execute the query in SQL developer.

Output:

Oracle Window Functions5

Explanation: As we can see in the above screenshot each row is provided with a rank.

Conclusion

In this article, we discussed the definition of windows function, and then we discussed about the various types of windows function that are available in the oracle and also went through the various examples based on those cases to help us understand better.

Recommended Articles

This is a guide to Oracle Window Functions. Here we discuss an introduction to Oracle Window functions, examples with code and output. You can also go through our other related articles to learn more –

  1. What is Oracle Database?
  2. Oracle vs OpenJDK
  3. MySQL vs Oracle
  4. Oracle vs SQL Server

All in One Data Science Bundle (360+ Courses, 50+ projects)

360+ Online Courses

50+ projects

1500+ Hours

Verifiable Certificates

Lifetime Access

Learn More

1 Shares
Share
Tweet
Share
Primary Sidebar
Oracle Tutorial
  • Advanced
    • Timestamp to Date in Oracle
    • Oracle Describe Table
    • Oracle Clauses
    • Oracle Having Clause
    • Oracle FOREIGN Key
    • PIVOT in Oracle
    • Oracle Alter Table
    • Oracle Queries
    • Oracle Views
    • Oracle Window Functions
    • Oracle String Functions
    • Oracle Date Functions
    • Oracle Analytic Functions
    • Oracle Aggregate Functions
    • Select in Oracle
    • INSERT in Oracle
    • DISTINCT in Oracle
    • Function in Oracle
    • Oracle GROUP_CONCAT
    • Oracle INSTR()
    • Oracle CONVERT
    • Oracle LENGTH()
    • Oracle EXISTS
    • Oracle REPLACE()
    • Oracle MERGE
    • Oracle LEAD()
    • Oracle EXTRACT()
    • Oracle LISTAGG()
    • Oracle SYS_CONTEXT()
    • Oracle COALESCE
    • Oracle NVL()
    • Oracle SYSDATE()
    • Oracle?Date Format
    • Oracle SYS_GUID()
    • Oracle WILDCARDS
    • Oracle Synonyms
    • Oracle Subquery
    • BETWEEN in Oracle
    • FETCH in Oracle
    • Oracle Index
    • Oracle Function-based Index
    • Oracle UNIQUE Index
    • Oracle Bitmap Index
    • Oracle Column
    • Oracle Triggers
    • Oracle Procedures
    • Sample Database for Oracle
    • Oracle LIKE Operator
    • ORDER BY in Oracle
    • Oracle ORDER BY DESC
    • GROUP BY in Oracle
    • Oracle GROUP BY HAVING
    • Oracle Aliases
    • Table in Oracle
    • Oracle Temporary Table
    • Oracle? Table Partition
    • Oracle rename table
    • Oracle CTE
    • Cursor in Oracle
    • Oracle LOCK TABLE
    • Oracle Tablespace
    • Oracle CARDINALITY
    • Oracle REGEXP
    • Oracle REGEXP_REPLACE
    • Oracle to_date
    • JSON in Oracle
    • Oracle COMMIT
    • Oracle GRANT
    • Oracle MD5
    • Oracle ROLLBACK
    • Oracle Users
    • Oracle TIMESTAMP
    • IF THEN ELSE in Oracle
    • Oracle While Loop
    • Oracle Clone Database
    • Oracle Backup Database
    • Oracle? XML
    • Oracle XMLAGG
    • Oracle XMLTABLE
    • Oracle Performance Tuning
    • Oracle B Tree Index
    • Oracle fusion
    • Oracle ebs
    • Oracle GRC
    • Oracle Cloud
    • Oracle HCM Cloud
    • Oracle Integration Cloud
    • Oracle Jinitiator
    • Oracle pathfinder
    • Oracle VirtualBox
    • Oracle Weblogic Server
    • Oracle decode
    • Oracle Exadata
    • Oracle ZFS
    • Oracle? utilities
    • JDBC Driver for Oracle
    • Oracle? DBA Versions
    • Oracle DBA Salary
  • Basic
    • Oracle Marketing Cloud
    • What is Oracle?
    • Career in Oracle
    • How to Install Oracle
    • Oracle Versions
    • What Is Oracle Database
    • Oracle Data Warehousing
    • Oracle Warehouse Builder
    • Career In Oracle Database Administrator
    • Career In Oracle DBA
    • What is Oracle RAC
    • Oracle DBA
    • Oracle? Vanderbilt
    • What is RMAN Oracle
    • Oracle Database Administration
    • Oracle Operators
    • Oracle Constraints
    • Oracle number
    • Oracle Data Types
    • Oracle UNIQUE Constraint
    • Oracle Check Constraint
  • Joins
    • Joins in Oracle
    • Inner Join in Oracle
    • Oracle Cross Join
    • Left Join in Oracle
    • OUTER Join in Oracle
    • Oracle Full Outer Join
    • Natural Join in Oracle
    • Oracle Self Join
    • Oracle hash join
    • Oracle? Update with Join
  • Oracle SET Operators
    • UNION in Oracle
    • Oracle UNION ALL
    • INTERSECT in Oracle
    • MINUS in Oracle
  • Interview Questions
    • Oracle Interview Questions
    • Oracle Apps Interview Questions
    • Oracle Apps Technical Interview Questions
    • Oracle Database Interview Questions
    • Oracle Forms Interview Questions
    • Oracle PL/SQL Interview Questions
    • Oracle RAC Interview Questions
    • Oracle SOA Interview Questions

Related Courses

Oracle Course Training

Oracle DBA Certification Course

MongoDB Certification Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Special Offer - Oracle Course Training Learn More