Warning: Creating default object from empty value in /www/htdocs/v030397/mysql-qotd/wp-content/plugins/sitepress-multilingual-cms/sitepress.class.php on line 4991

Warning: Creating default object from empty value in /www/htdocs/v030397/mysql-qotd/wp-content/plugins/sitepress-multilingual-cms/sitepress.class.php on line 4993
Question 13: Given the following scenario, what is the result of the query below? – MySQL Question of the Day

Skip to content

By urs in mysql questions

 

mysql> create table dfttst (a int null default null,
b int null default 0,c int not null default 0);
Query OK, 0 rows affected (0.07 sec)

 

mysql> insert into dfttst values (1,1,1),(null,null,null),(0,0,0);
Query OK, 3 rows affected, 1 warning (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 1

 

mysql> show warnings;
+---------+------+---------------------------+
| Level   | Code | Message                   |
+---------+------+---------------------------+
| Warning | 1048 | Column 'c' cannot be null |
+---------+------+---------------------------+
1 row in set (0.05 sec)

 
What is the result of the query below?

select count(a),count(b),count(c) from dfttst;

a) 2,2,2
b) 3,3,3
c) 2,2,3

 
And still an extra question:
What would “select a + b + c from tt.dfttst;” return?
 
[ Data Types (15%) - Handling Missing or Invalid Data Values ]
 

Tags: , ,

Comment Feed

One Response


  1. Answer:

    c

    count(column-name) returns the amount of non-NULL values for the column.
    a and b can contain NULL values, but c can’t.

    mysql> select a + b + c from tt.dfttst;
    +-----------+
    | a + b + c |
    +-----------+
    |         3 |
    |      NULL |
    |         0 |
    +-----------+
    3 rows in set (0.00 sec)

     
    Any calculation with NULL will result in NULL.

     

You must be logged in to post a comment.