Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the GROUP BY clause manage the NULL values?

Tags:

sql

sql-server

How does the GROUP BY clause manage the NULL values? Does it correspond to the general treatment of these values?

like image 335
Nizam Uddin Sikder Avatar asked Apr 02 '14 05:04

Nizam Uddin Sikder


People also ask

Does GROUP BY clause ignore NULL values?

We can see that the first result value is a NULL represented by an empty string (the empty line before the IT department). This empty space represents all the NULL values returned by the GROUP BY clause, so we can conclude that GROUP BY treats NULLs as valid values.

How does the GROUP BY clause work?

The GROUP BY clause instructs the DBMS to group the data and then perform the aggregate (function) on each group rather than on the entire result set. Aside from the aggregate calculation statements, every column in your SELECT statement must be present in the GROUP BY clause.

How are NULLs treated if they exist in grouping attributes?

How are NULLs treated if they exist in grouping attributes? They are separated into a group created for all tuples with a NULL value in grouping attribute. A query (SELECT statement) inside a query that can appear as part of a condition in the WHERE or HAVING clauses as well as in the FROM clause.


2 Answers

Null values of a column are grouped as a separate group.

See SQL Fiddle demonstrating Group By and aggregate functions on nullable column

like image 158
Mudassir Hasan Avatar answered Sep 22 '22 06:09

Mudassir Hasan


You mean, when you GROUP BY a nullable column? All rows with a NULL in the column are treated as if NULL was another value.

If a grouping column contains null values, all null values are considered equal, and they are put into a single group.

http://technet.microsoft.com/en-us/library/ms177673.aspx

like image 31
Euro Micelli Avatar answered Sep 20 '22 06:09

Euro Micelli