Megan Taylor

front-end dev, volunteacher, news & data junkie, bibliophile, Flyers fan, sci-fi geek and kitteh servant

Intro to Databases: SQL Null Values

Demo: simple college admissions database
College (cName, state, enrollment)
Student (sID, sName, GPA, sizeHS)
Apply (sID, cName, major, decision)

NULL values: undefined or unknown

insert into Student values (432, ‘Kevin’, null, 1500);
insert into Student values (321, ‘Lori’, null, 2500);

find students whose GPA is greater than 3.5
select sID, sName, GPA
from Student
where GPA > 3.5;

NULL values will not show up

find students whose GPA is less than or equal than 3.5
select sID, sName, GPA
from Student
where GPA <= 3.5; NULL values will not show up find students whose GPA is less than or equal to 3.5 or greater than 3.5 select sID, sName, GPA from Student where GPA > 3.5 or GPA <= 3.5; NULL values will not show up select sID, sName, GPA from Student where GPA > 3.5 or GPA <= 3.5 or GPA is null; NULL values will show up find students with GPA > 3.5 or high schools smaller than 1600
select sID, sName, GPA, sizeHS
from Student
where GPA > 3.5 or sizeHS < 1600 or sizeHS>=1600;

students with NULL in GPA still show up

count (distinct xxx) does not count NULLs
distinct will count NULLs

November 5, 2011 | Comments Off on Intro to Databases: SQL Null Values | Categories: Posts | Permalink

Comments are closed.

%d bloggers like this: