Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select a MAX on Date column not working with empty table

Tags:

sql

oracle

i am running the following query on one of my table which having a date column.

select max(date) from mydate

Problem is, when table is empty, then also its returning a row. I am using Oracle 11 .

Any suggestion?

like image 583
CrazyC Avatar asked Dec 22 '22 14:12

CrazyC


1 Answers

This is a valid behavior: aggregate functions usually return a NULL being applied to an empty set, and COUNT(*) returns 0.

If you don't want a row if the table is empty, use this:

SELECT  MAX(date)
FROM    mydate
HAVING  COUNT(*) > 0
like image 115
Quassnoi Avatar answered Jan 15 '23 16:01

Quassnoi