简体   繁体   中英

Multiplication with NULL and empty column values in SQL

This was my Interview Question

there are two columns called Length and Breadth in Area table

Length  Breadth  Length*Breadth
20      NULL        ?

30                   ?

21.2     1           ?

I tried running the same question on MYSQL while inserting,To insert an empty value I tried the below query . Am I missing anything while inserting empty values in MYSQL.

insert into test.new_table values (30,);

Answers: With Null,Result is Null. With float and int multiplication result is float

As per your question the expected results would be as below.

SELECT LENGTH,BREADTH,LENGTH*BREADTH AS CALC_AREA FROM AREA;


LENGTH  BREADTH   CALC_AREA 
20      
30      0         0
21.2    1         21.2

For any(first) record in SQL SERVER if you do computation with NULL the answer would be NULL.

For any(second) record in SQL SERVER, if you do product computation between a non-empty value and an empty value the result would be zero as empty value is treated as zero.

For any(third) record in SQL SERVER, if you do computation between two non-empty data type values the answer would be a NON-EMPTY value.

Check SQL Fiddle for reference - http://sqlfiddle.com/#!3/f250a/1

That blank Breath (second row) cannot happen unless Breath is VARCHAR. Assuming that, the answers will be:

  1. NULL (since NULL times anything is NULL)

  2. Throws error (since an empty string is not a number. In Sql Server, the error is " Error converting data type varchar to numeric. ")

  3. 21.20 (since in Sql Server, for example, conversion to a numeric type is automatic, so SELECT 21.2 * '1' returns 21.20).

The product of any value and NULL is NULL. This is called "NULL propagation" if you want to Google it. To score points in an interview, you might want to mention that NULL isn't a value; it's a special marker.

The fact that the column Breadth has one entry "NULL" and one entry that's blank (on the second row) is misleading. A numeric column that doesn't have a value in a particular row means that row is NULL. So the second column should also show "NULL".

The answer to the third row, 21.2 * 1, depends on the data type of the column "Length*Breadth". If it's a data type like float, double, or numberic(16,2), the answer is 21.2. If it's an integer column (integer, long, etc.), the answer is 21.

A more snarky answer might be "There's no answer. The string "Length*Breadth" isn't a legal SQL column name."

Assuming that Length and Breadth are numerical types of some kind the second record does not contain possible values — Breadth must be either 0 or NULL.

In any event, any mathematical operation in SQL involving a NULL value will return the value NULL, indicating that the expression cannot be evaluated. The answer are NULL, impossible, and 21.2.

In standard SQL they would all generate errors because you are comparing values (or nulls) of different types:

CAST ( 20 AS FLOAT ) * CAST ( NULL AS INTEGER )  -- mismatched types error

CAST ( '' AS INTEGER )  -- type conversion error
CAST (  AS INTEGER )  -- type conversion error


CAST ( 21.2 AS FLOAT ) * CAST ( 2 AS INTEGER )  -- mismatched types error

On the other hand, most SQL product would implicitly cast values when comparing values (or nulls) of different types according to type precedence eg comparing float value to an integer value would in effect cast the integer to float and result in a float. At the product level, the most interesting question is what happens when you compare a null of type integer with a value (or even a null) of type float...

...but, frankly, not terribly interesting. In an interview you are presented with a framework (in the form of questions asked of you) on which to present your knowledge, skills and experience. The 'answer' here is to discuss nulls (eg point out that nulls are tricky to define and behave in unintuitive ways, which leads to frequent bugs and a desire to avoid nulls entirely, etc) and whether implicit casting is a good thing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM