Forum Discussion

ahamed0820's avatar
ahamed0820
Copper Contributor
Jan 13, 2024

Incorrect Result Set

Hi All

Please explain why the first select statement returns the data when there is no available record in the table 

Declare @Temp Table (EmployeeName Varchar(100),DOB Datetime)

Select 'Test',MAX(DOB) From @Temp

 

Select 'Test',MAX(DOB) From @Temp
Group by DOB

 

Select * From @Temp


Thanks

  • ahamed0820 

     

    The results are correct because you are using an aggregate function, which allows the 'Test' literal to generate a result set row that just happens to have a NULL value for the aggregate result.

     

    If you were not performing an aggregation - as shown in the following example, you would get no rows (which is what you seem to have incorrectly expected for the aggregate scenario).

     

    SELECT 'Test', * From @Temp;

     

    Cheers,

    Lain

  • LainRobertson's avatar
    LainRobertson
    Silver Contributor

    ahamed0820 

     

    The results are correct because you are using an aggregate function, which allows the 'Test' literal to generate a result set row that just happens to have a NULL value for the aggregate result.

     

    If you were not performing an aggregation - as shown in the following example, you would get no rows (which is what you seem to have incorrectly expected for the aggregate scenario).

     

    SELECT 'Test', * From @Temp;

     

    Cheers,

    Lain

Resources