{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Missing Data" ] }, { "cell_type": "markdown", "metadata": { "cell_tags": [ "objectives" ] }, "source": [ "#### Objectives\n", "\n", "* Explain how databases represent missing information.\n", "* Explain the three-valued logic databases use when manipulating missing information.\n", "* Write queries that handle missing information correctly." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Real-world data is never complete—there are always holes.\n", "Databases represent these holes using special value called `null`.\n", "`null` is not zero, `False`, or the empty string;\n", "it is a one-of-a-kind value that means \"nothing here\".\n", "Dealing with `null` requires a few special tricks\n", "and some careful thinking.\n", "\n", "To start,\n", "let's have a look at the `Visited` table.\n", "There are eight records,\n", "but #752 doesn't have a date—or rather,\n", "its date is null:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%load_ext sqlitemagic" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "%%sqlite survey.db\n", "select * from Visited;" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
619DR-11927-02-08
622DR-11927-02-10
734DR-31939-01-07
735DR-31930-01-12
751DR-31930-02-26
752DR-3None
837MSK-41932-01-14
844DR-11932-03-22
" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Null doesn't behave like other values.\n", "If we select the records that come before 1930:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%sqlite survey.db\n", "select * from Visited where dated<'1930-00-00';" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "\n", "\n", "
619DR-11927-02-08
622DR-11927-02-10
" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "we get two results,\n", "and if we select the ones that come during or after 1930:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%sqlite survey.db\n", "select * from Visited where dated>='1930-00-00';" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "\n", "\n", "\n", "\n", "\n", "
734DR-31939-01-07
735DR-31930-01-12
751DR-31930-02-26
837MSK-41932-01-14
844DR-11932-03-22
" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "we get five,\n", "but record #752 isn't in either set of results.\n", "The reason is that\n", "`null<'1930-00-00'`\n", "is neither true nor false:\n", "null means, \"We don't know,\"\n", "and if we don't know the value on the left side of a comparison,\n", "we don't know whether the comparison is true or false.\n", "Since databases represent \"don't know\" as null,\n", "the value of `null<'1930-00-00'`\n", "is actually `null`.\n", "`null>='1930-00-00'` is also null\n", "because we can't answer to that question either.\n", "And since the only records kept by a `where`\n", "are those for which the test is true,\n", "record #752 isn't included in either set of results.\n", "\n", "Comparisons aren't the only operations that behave this way with nulls.\n", "`1+null` is `null`,\n", "`5*null` is `null`,\n", "`log(null)` is `null`,\n", "and so on.\n", "In particular,\n", "comparing things to null with = and != produces null:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%sqlite survey.db\n", "select * from Visited where dated=NULL;" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "\n", "
" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "%%sqlite survey.db\n", "select * from Visited where dated!=NULL;" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "\n", "
" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "To check whether a value is `null` or not,\n", "we must use a special test `is null`:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%sqlite survey.db\n", "select * from Visited where dated is NULL;" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "\n", "
752DR-3None
" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "or its inverse `is not null`:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%sqlite survey.db\n", "select * from Visited where dated is not NULL;" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
619DR-11927-02-08
622DR-11927-02-10
734DR-31939-01-07
735DR-31930-01-12
751DR-31930-02-26
837MSK-41932-01-14
844DR-11932-03-22
" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Null values cause headaches wherever they appear.\n", "For example,\n", "suppose we want to find all the salinity measurements\n", "that weren't taken by Dyer.\n", "It's natural to write the query like this:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%sqlite survey.db\n", "select * from Survey where quant='sal' and person!='lake';" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "\n", "\n", "\n", "\n", "
619dyersal0.13
622dyersal0.09
752roesal41.6
837roesal22.5
" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "but this query filters omits the records\n", "where we don't know who took the measurement.\n", "Once again,\n", "the reason is that when `person` is `null`,\n", "the `!=` comparison produces `null`,\n", "so the record isn't kept in our results.\n", "If we want to keep these records\n", "we need to add an explicit check:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%sqlite survey.db\n", "select * from Survey where quant='sal' and (person!='lake' or person is null);" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "\n", "\n", "\n", "\n", "\n", "
619dyersal0.13
622dyersal0.09
735Nonesal0.06
752roesal41.6
837roesal22.5
" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 10 }, { "cell_type": "markdown", "metadata": {}, "source": [ "We still have to decide whether this is the right thing to do or not.\n", "If we want to be absolutely sure that\n", "we aren't including any measurements by Lake in our results,\n", "we need to exclude all the records for which we don't know who did the work." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Challenges\n", "\n", "1. Write a query that sorts the records in `Visited` by date,\n", " omitting entries for which the date is not known\n", " (i.e., is null).\n", "\n", "1. What do you expect the query:\n", "\n", " ~~~\n", " select * from Visited where dated in ('1927-02-08', null);\n", " ~~~\n", "\n", " to produce?\n", " What does it actually produce?\n", "\n", "1. Some database designers prefer to use\n", " a [sentinel value](https://github.com/swcarpentry/bc/blob/master/gloss.md#sentinel-value)\n", " to mark missing data rather than `null`.\n", " For example,\n", " they will use the date \"0000-00-00\" to mark a missing date,\n", " or -1.0 to mark a missing salinity or radiation reading\n", " (since actual readings cannot be negative).\n", " What does this simplify?\n", " What burdens or risks does it introduce?" ] }, { "cell_type": "markdown", "metadata": { "cell_tags": [ "keypoints" ] }, "source": [ "#### Key Points\n", "\n", "* Databases use `null` to represent missing information.\n", "* Any arithmetic or Boolean operation involving `null` produces `null` as a result.\n", "* The only operators that can safely be used with `null` are `is null` and `is not null`." ] } ], "metadata": {} } ] }