Tags
A puzzle, A quick query puzzle, A SQL Puzzle, A SQL Server Puzzle, Complex SQL Challenges, complex sql statement(puzzle), Complex TSQL Challenge, Interesting Interview Questions, Interview Qs.SQL SERVER Questions, Interview questions on Joins, Interview Questions on SQL, InterviewQuestions, InterviewQuestions for SQL, Join SQL Server tables where columns include NULL values, Joins, Joins Interview questions, Joins Puzzle, Khowal, Learn complex SQL, Learn SQL, Learn T-SQL, NULLs and JOINs, Objective Puzzle, Pawan, Pawan Khowal, Pawan Kumar, Pawan Kumar Khowal, PL/SQL Challenges, puzzle sql developer, Puzzles, Puzzles in SQL, PUzzles in SQL SERVER, Queries for SQL Interview, SELECT, SELECT Puzzle, SELECT SUM returns a row when there are no records, Some useful SQL puzzles to teach SQL, 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 QUERY PUZZLES, SQL Quiz, SQL Server - Best way to Handle NULLS in Joins, SQL Server Database, SQL SERVER Interview questions, SQL Server Puzzle, SQL SERVER Puzzles, SQL Server: joining NULL values, SQL Skills, SQL Sudoku, SQL Top clause, SQL Tricky Puzzles, SQL Trikcy question, sql/database interview for puzzle sql developer, SQLSERVER, T SQL Puzzles, T-SQL Challenge, The Biggest Gap Puzzle, The Gap Puzzle, The GroupBy Puzzle, Tough SQL Challenges, Tough SQL Puzzles, Tricky Puzzles in SQL, tricky puzzles in SQL Server, Tricky Questions, TSQL, TSQL Challenge, TSQL Challenges, TSQL Interview questions, TSQL Puzzles, TSQL Queries, TSQLPuzzles, Week puzzle, What is the SUM of an Empty Table?, Why does ANSI SQL define SUM(no rows) as NULL?, Why does SUM(…) on an empty recordset return NULL instead of 0?
A SQL Puzzle | What is the SUM and Count of an Empty Table?
Let’s start by creating some sample data.
-- CREATE TABLE testEmpty ( ID INT ) GO SELECT SUM(ID) Sums FROM testEmpty SELECT COUNT(*) Cnts FROM testEmpty CREATE TABLE testEmpty1 ( ID INT NOT NULL ) GO SELECT COUNT(*) Cnts FROM testEmpty1 SELECT SUM(ID) Sums FROM testEmpty1 --
If you run the above statements for both the tables, What will be the sum value and the Count value for the above queries and their reasons ?
Please add answers in the comments section !
Cheers, Keep Learning !!
Pawan Kumar Khowal
MSBISKills.com
sum is null and count is 0
LikeLike
Could you tell us the reason for the same ? If you can explain in detail.
LikeLike