sqlzoo 8.Using Null answer

If you have any questions, please comment or chat in private. Please chat with the blogger privately, thank you.

Link to the original title: https://sqlzoo.net/wiki/Using_Null

Other question solution links: https://blog.csdn.net/aiqiyizz/article/details/109057732

The difficulty is not great, the fish that has been touched for a long time. .

8 Using Null

8.1 NULL, INNER JOIN, LEFT JOIN, RIGHT JOIN

SELECT name
FROM teacher
WHERE dept IS NULL

8.2

Direct example

SELECT teacher.name, dept.name
 FROM teacher INNER JOIN dept
           ON (teacher.dept=dept.id)

8.3

SELECT teacher.name, dept.name
 FROM teacher LEFT JOIN dept
           ON (teacher.dept=dept.id)

8.4

SELECT teacher.name, dept.name
 FROM teacher RIGHT JOIN dept
           ON (teacher.dept=dept.id)

8.5 Using the COALESCE function

Application of COALESCE

SELECT name, COALESCE(mobile, '07986 444 2266')
FROM teacher

8.6

SELECT teacher.name, COALESCE(dept.name, 'None')
FROM teacher LEFT JOIN dept ON teacher.dept = dept.id

8.7

SELECT COUNT(name), COUNT(mobile)
FROM teacher

8.8

SELECT dept.name, COUNT(teacher.dept) AS 'the number of staff'
FROM teacher RIGHT JOIN dept ON teacher.dept = dept.id
GROUP BY dept.name

8.9 Using CASE

It's not difficult, but my brain twitched while writing

SELECT name, CASE WHEN dept IN (1, 2) THEN 'Sci' ELSE 'Art' END
FROM teacher

8.10

SELECT
  name,
  CASE
    WHEN dept IN (1, 2) THEN 'Sci'
    WHEN dept = 3 THEN 'Art'
    ELSE 'None'
  END
FROM
  teacher

Guess you like

Origin blog.csdn.net/aiqiyizz/article/details/109062327