This document contains my solutions to the SQLZoo 'Using NULL' section using MySQL syntax, along with my personal learning notes and explanations.
List the teachers who have NULL for their department.
Why we cannot use =
You might think that the phrase dept=NULL
would work here but it doesn't - you can use the phrase dept IS NULL
.
My Solution:
SELECT name
FROM teacher
WHERE dept IS NULL;
My Notes:
Use IS NULL
to filter null values in SQL.
In SQL, NULL
is not the same as a value of 0
.
From Wikipedia:
"A null should not be confused with a value of 0. A null indicates a lack of a value, which is not the same as a zero value.
In SQL, null is a marker, not a value.
This usage is quite different from most programming languages, where a null value of a reference means it is not pointing to any object."
Note the INNER JOIN
misses the teachers with no department and the departments with no teacher.
My Solution:
SELECT teacher.name, dept.name
FROM teacher
INNER JOIN dept ON (teacher.dept = dept.id);
Use a different JOIN
so that all teachers are listed.
My Solution:
SELECT teacher.name, dept.name
FROM teacher
LEFT JOIN dept ON (teacher.dept = dept.id);
My Notes:
LEFT JOIN
includes all teachers, even those without a department.
Use a different JOIN
so that all departments are listed.
My Solution:
SELECT teacher.name, dept.name
FROM teacher
RIGHT JOIN dept ON (teacher.dept = dept.id);
My Notes:
RIGHT JOIN
ensures all departments are listed, even if they have no teachers.
Use COALESCE
to print the mobile number. Use the number '07986 444 2266' if there is no number given. Show teacher name and mobile number or '07986 444 2266'.
My Solution:
SELECT
teacher.name,
COALESCE(teacher.mobile, '07986 444 2266') AS 'mobile number'
FROM teacher;
My Notes:
COALESCE()
returns the first non-null value from its arguments.
Use the COALESCE
function and a LEFT JOIN
to print the teacher name and department name. Use the string 'None' where there is no department.
My Solution:
SELECT
teacher.name,
COALESCE(dept.name, 'None') AS 'department name'
FROM teacher
LEFT JOIN dept ON (teacher.dept = dept.id);
Use COUNT
to show the number of teachers and the number of mobile phones.
My Solution:
SELECT
COUNT(teacher.name),
COUNT(teacher.mobile)
FROM teacher;
Use COUNT
and GROUP BY dept.name
to show each department and the number of staff. Use a RIGHT JOIN
to ensure that the Engineering department is listed.
My Solution:
SELECT
dept.name,
COUNT(teacher.name)
FROM teacher
RIGHT JOIN dept ON (teacher.dept = dept.id)
GROUP BY dept.name;
Use CASE
to show the name of each teacher followed by 'Sci' if the teacher is in dept 1 or 2 and 'Art' otherwise.
My Solution:
SELECT
teacher.name,
(CASE
WHEN dept IN (1, 2) THEN 'Sci'
ELSE 'Art'
END) AS 'Sci or Art'
FROM teacher;
My Notes:
CASE
allows you to return different values under different conditions.
Use CASE
to show the name of each teacher followed by 'Sci' if the teacher is in dept 1 or 2, show 'Art' if the teacher's dept is 3 and 'None' otherwise.
My Solution:
SELECT
teacher.name,
(CASE
WHEN dept IN (1, 2) THEN 'Sci'
WHEN dept = 3 THEN 'Art'
ELSE 'None'
END) AS 'Sci, Art, or None'
FROM teacher;