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


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

Streamline Your Business Process with Real Estate Data Entry Services

People in the real estate business have to deal with huge volumes of data on a regular basis. It includes the data that is generated from legal documents, property valuation statements, latest commercial and non-commercial property information, etc. This database should be regularly updated and processed properly to help you in your business procedures. With access to relevant real-time data, your business organization can thrive in the commercial real estate market. In…


Posted by Alvaro Dee on March 27, 2017 at 2:48am

Augment the Speed and Efficiency of Website Development with Joomla

Over the past few years, Joomla has emerged out as one of the greatest platforms for designing, developing, and deploying websites and web applications worldwide. Joomla scales up extremely well, uses less memory and gives a brilliant performance. Hire skilled and experienced Joomla developers to use Joomla CMS for developing simple and complex corporate websites. You can even enhance the functioning of your existing website by using this open source platform.

By outsourcing Joomla…


Posted by Alvaro Dee on March 20, 2017 at 4:00am

Top 10 Interview Questions & Answers for Java Developers

An organization hiring a Java developer is looking for someone who can code the applications flawlessly. For the period after Java training, the following is a very important list of ten Java interview questions. Make sure you tap the benefits of this comprehensive list of questions shared by our Java counterparts to crack your next interview.

1. What is immutable…


Posted by Michael Warne on March 17, 2017 at 3:00am

Get the Best Website for Your Business in a Set Time Frame

If you sell products online, you must be knowing how time-consuming and mundane it is to upload products. There are chances of you looking for someone to handle your product data entry tasks. A company that has a wealth of experience and is known for providing its customers with data entry services for a variety of eCommerce solutions can deal efficiently with high volumes of work without any hassle.…


Posted by Alvaro Dee on March 17, 2017 at 2:47am

© 2017   Created by Steve Karam.   Powered by

Badges  |  Report an Issue  |  Terms of Service