Tags
Complex SQL Challenges, complex sql statement(puzzle), Complex TSQL Challenge, Divide rows into two columns, find records not ending with s, find records not ending with s puzzle, Interesting Interview Questions, Interview Puzzles in SQL Server, Interview Qs.SQL SERVER Questions, Interview questions on Joins, Interview Questions on SQL, Interview SQL Puzzles, InterviewQuestions, InterviewQuestions for SQL, Joins, Joins Interview questions, Joins Puzzle, Khowal, last non null puzzle, Learn complex SQL, Learn SQL, Learn T-SQL, Objective Puzzle, Pawan, Pawan Khowal, Pawan Kumar, Pawan Kumar Khowal, PL/SQL Challenges, Prev Value puzzle, Previous value puzzle, Puzzle SQL, puzzle sql developer, Puzzle SQl Server, Puzzles, PUzzles in SQL SERVER, Queries for SQL Interview, Records not ending with a character Puzzle, Records not ending with s Puzzle, SELECT Puzzle, SQL, SQL 2012, SQL 2014, SQL 2014 Interview Questions, SQL Challenge, SQL Challenges, SQL Interview Questions, SQL Joins, SQL pl/sql puzzles, SQL puzzle, SQL Puzzles, SQL Queries, SQL Quiz, SQL Server Database, SQL server filter index with LIKE and RIGHT function?, SQL Server Interview Puzzle, SQL SERVER Interview questions, SQL Server Puzzle, SQL SERVER Puzzles, SQL Skills, SQL Sudoku, SQL Top clause, SQL Trikcy question, sql/database interview for puzzle sql developer, SQLSERVER, T SQL Puzzles, T-SQL Challenge, T-SQL Puzzle, T-SQL Query | [ Replace 6 Consecutive Digits with x from a string Puzzle ], T-SQL Query | [ Replace 6 or more Consecutive Digits with x from a string Puzzle ], T-SQL Query | [ The Complex Week Puzzle ], T-SQL Tricky Puzzles, The Biggest Gap Puzzle, The Gap Puzzle, The Gap Puzzle Puzzle, The Tree Puzzle, TOP Clause, Tough SQL Challenges, Tough SQL Puzzles, Tricky Questions, TSQL, TSQL Challenge, TSQL Challenges, TSQL Interview questions, TSQL Puzzle, TSQL Puzzles, TSQL Queries, Week puzzle, What You Can (and Can't) Do With Filtered Indexes
Friends, I have a quick question for you. Why does SUM of an empty table returns NULL & Not 0 ? Let me just create some sample examples for you.
Example 1
-- CREATE TABLE testSUMNulls ( ID TINYINT ) GO INSERT INTO testSUMNulls (ID) VALUES (NULL),(NULL),(NULL),(NULL) SELECT SUM(ID) FROM testSUMNulls -- |
Example 2
-- CREATE TABLE testSumEmptyTable ( ID TINYINT ) GO SELECT SUM(ID) Sums FROM testSumEmptyTable -- |
Example 3
-- CREATE TABLE testSumTable ( ID TINYINT ) GO INSERT INTO testSumTable(ID) VALUES (10),(20),(30),(40) SELECT SUM(ID) Sums FROM testSumTable WHERE ID = 1 -- |
Now in all the cases we will get NULL as the sum output. Now in all the cases we will get NULL as the sum output. As far as I know SQL internally works with Bags(https://msbiskills.com/2015/09/15/sql-server-what-are-bags-sets/) not sets. Now if our bag doesn’t have anything why we are getting NULL and not 0. Now the question is why it so ?
Please add your answers in the comments section. Enjoy !! We all need to learn.
MSBISKILLS.COM
Pawan Kumar Khowal
You must be logged in to post a comment.