Tags

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,


T-SQL Output Query | [ The Null Puzzle ]

Please go through the query and the sample input data and provide the output of the query written below-

--

CREATE TABLE testNULLsHere
(
	 ID SMALLINT
	,VALUE SMALLINT
)
GO

INSERT INTO testNULLsHere
VALUES
(1,10),(2,NULL),(1,50),(2,NULL),(3,NULL)
GO

--

Data

ID VALUE
1 10
2 NULL
1 50
2 NULL
3 NULL

PUZZLE 1 – What will be the output of this query

--

SELECT 
	ID , SUM(VALUE) 
FROM 
	testNULLsHere
GROUP BY
	ID

--

Let’s insert some data into the existing table

--

INSERT INTO testNULLsHere
VALUES
(2,0),(3,NULL),(3,0)
GO

--

Data

ID VALUE
1 10
2 NULL
1 50
2 NULL
3 NULL
2 0
3 NULL
3 0

PUZZLE 2 – What will be the output of this query

--

SELECT 
	ID , SUM(VALUE) 
FROM 
	testNULLsHere
GROUP BY
	ID

--

Please leave a comment if you need solution to the above puzzle

Keep Learning

Pawan Kumar Khowal

MSBISKills.com

Advertisements