4 Things to Know About the GREATEST Function in Oracle

The GREATEST function in Oracle is one that is helpful, but I haven't seen used very often. If you do use it, keep these things in mind.
What Is The GREATEST Function?
The first thing to know about the function is what it does.
GREATEST finds the greatest value in a list of provided values.
The syntax is:
GREATEST ( expr1, [ expr_n] )
You provide it with a set of expressions, and the function will find the greatest of these.
It's Not an Aggregate Function
Unlike the MAX function, the GREATEST function is not an aggregate function. It doesn't accept multiple rows and transform them into a single row.
The GREATEST function will return a row for each row that you provide. So, if you have a query like this:
SELECT first_name, GREATEST(score_jan, score_feb, score_mar)
FROM students;
This query will return every first_name, and the highest value from those three columns. It doesn't aggregate the values - they will be evaluated for each row and will most likely be different.
Works with Numbers, Characters, and Dates
The GREATEST function works with the three main data types - numbers, characters, and dates.
If the provided values are numeric, Oracle works out the highest number.
If the provided values are characters, Oracle returns the last value if they were sorted alphabetically.
If the provided values are dates, Oracle returns the latest date.
This makes it quite a flexible function. You don't need to convert data types for the function to work. Simply provide the columns or values you need to the function.
If Any Expressions Are NULL, NULL Will Be Returned
As I mentioned earlier, the Oracle GREATEST function works with several different data types.
It can also accept NULL values. However, if any of the provided values are NULL, then the entire function returns NULL.
For example, let's take a look at this query:
SELECT GREATEST(10, 15, 8, 4, 71, 45)
FROM dual;
This query will return 71 because that's the highest number.
Let's see an example using columns. Assuming we have one row in a "scores" table with these values:
score_jan = 81
score_feb = 72
score_mar = 85
If we run a GREATEST on these columns, we get the result of 85.
SELECT GREATEST(score_jan, score_feb, score_mar)
FROM scores;
Now, what if we update the table to say that score_jan is NULL?
score_jan = NULL
score_feb = 72
score_mar = 85
SELECT GREATEST(score_jan, score_feb, score_mar)
FROM scores;
The same query will return NULL, because one of the input values is NULL.
So, those are some things that you'll need to remember when using the GREATEST function. It's a useful function, so keep these points in mind!

Views: 27


You need to be a member of Oracle Community to add comments!

Join Oracle Community

Oracle Community On

I'm not a fan of advertising, and so I will not be including any advertisements on OracleCommunity.net. However, managing this community does not come without cost! If you are willing to donate to help pay for the monthly community fees and domain services I accept Bitcoin and PayPal donations.

Donate Bitcoins



Blog Posts

Suntec’s Data Mining Services Help You Get Efficient Data

It is a well- established fact that the availability of accurate and updated data is the base of undertaking successful business operations. Data is required at all point of business operations ranging from buying of goods and services from suppliers to marketing your final product. Often, business organizations fail to utilize their data to its full potential.

At SunTec, our…


Posted by Alvaro Dee on May 26, 2017 at 3:46am

Convert Prospects into Repeat Customers with Amazon Data Entry Services

eCommerce store owners get a better exposure for their products by selling on a platform like Amazon. If you are thinking about selling on Amazon or you are already a seller who needs help in managing product listings on this popular online marketplace, stop worrying. Professionals with sound technical knowledge of Amazon product listings, categorization, content, etc. are there to make things easier for you.

You can outsource …


Posted by Alvaro Dee on April 12, 2017 at 10:05pm

Increase Sales with Unique and Compelling Product Descriptions

Are you thinking about opening an online store? Or, do you already have one? Whether you have a small online business or a big store, the manner in which you list your products leads to a dramatic impact on your sales.

Don’t just sell a product, you need to sell an experience.
You yourself get excited about the features and specifications of your products. But the problem is that your…


Posted by Alvaro Dee on April 10, 2017 at 11:18pm

Business Research Helps You Understand Critical Traits of Your Business

Business organization needs strong customer and actionable industry insights to develop better understanding of its market and other important aspects. It can meet business objectives and make tactical, strategic and operational decisions with the help of business research services. The growth of a company largely depends on effective business research. You can conduct a successful market research with…


Posted by Alvaro Dee on April 3, 2017 at 3:23am

© 2017   Created by Steve Karam.   Powered by

Badges  |  Report an Issue  |  Terms of Service