Menangani null

Pelaporan dengan SQL

Tyler Pernes

Learning & Development Consultant

Apa arti null sebenarnya?

+--------+-----------------+
| order  | price_per_unit  |
|--------|-----------------|
| 1      | 4.50            |
| 2      | 2.25            |
| 3      | null            |
+--------+-----------------+
  • Belum diproses?
  • Gratis?
  • Harga flat?
Pelaporan dengan SQL

Masalah dengan null

soccer_games
+---------+-------+-------+
| game_id | home  | away  |
|---------|-------|-------|
| 123     | 3     | 2     |
| 124     | 2     | null  |
| 125     | null  | 1     |
+---------+-------+-------+
SELECT *, home + away AS total_goals
FROM soccer_games;
Pelaporan dengan SQL

Masalah dengan null

+---------+-------+-------+--------------+
| game_id | home  | away  | total_goals  |
|---------|-------|-------|--------------|
| 123     | 3     | 2     | 5            |
| 124     | 2     | null  | null         |
| 125     | null  | 1     | null         |
+---------+-------+-------+--------------+
Pelaporan dengan SQL

Masalah dengan null

SELECT 
    region, 
    COUNT(DISTINCT athlete_id) AS athletes
FROM summer_games AS s
JOIN countries AS c
ON s.country_id = c.id
GROUP BY region;
+----------+-----------+
| region   | athletes  |
|----------|-----------|
| BALTICS  | 42        |
| OCEANIA  | 62        |
| null     | 10        |
+----------+-----------+
  • Tidak jelas apa yang dimaksud null!
Pelaporan dengan SQL

Perbaikan 1: Memfilter null

original_table
+--------+-----------------+
| order  | price_per_unit  |
|--------|-----------------|
| 1      | 4.50            |
| 2      | 2.25            |
| 3      | null            |
+--------+-----------------+
SELECT *
FROM original_table
WHERE price_per_unit IS NOT NULL;
Pelaporan dengan SQL

Perbaikan 1: Memfilter null

+--------+-----------------+
| order  | price_per_unit  |
|--------|-----------------|
| 1      | 4.50            |
| 2      | 2.25            |
+--------+-----------------+
Pelaporan dengan SQL

Perbaikan 2: COALESCE()

Sintaks: COALESCE(field, null_replacement)

SELECT 
    COALESCE(region,'Independent Athletes') AS region,
    COUNT(DISTINCT athlete_id) AS athletes
FROM summer_games AS s
JOIN countries AS c
ON s.country_id = c.id;
+-----------------------+-----------+
| region                | athletes  |
|-----------------------|-----------|
| BALTICS               | 42        |
| OCEANIA               | 62        |
| Independent Athletes  | 10        |
+-----------------------+-----------+
Pelaporan dengan SQL

Perbaikan 2: COALESCE()

soccer_games
+---------+-------+-------+
| game_id | home  | away  |
|---------|-------|-------|
| 123     | 3     | 2     |
| 124     | 2     | null  |
| 125     | null  | 1     |
+---------+-------+-------+
SELECT *, COALESCE(home,0) + COALESCE(away,0) AS total_goals
FROM soccer_games;
Pelaporan dengan SQL

Perbaikan 2: COALESCE()

+---------+-------+-------+--------------+
| game_id | home  | away  | total_goals  |
|---------|-------|-------|--------------|
| 123     | 3     | 2     | 5            |
| 124     | 2     | null  | 2            |
| 125     | null  | 1     | 1            |
+---------+-------+-------+--------------+
Pelaporan dengan SQL

Null akibat kueri

Penyebab:

  • LEFT JOIN tidak mencocokkan semua baris
  • Tidak ada kondisi CASE yang terpenuhi
  • Lainnya!
Pelaporan dengan SQL

Mengukur dampak null

Rasio baris yang null

SELECT SUM(CASE when country IS NULL then 1 else 0 end) / SUM(1.00)
FROM orders;
+-------+
| .12   |
+-------+

Rasio pendapatan yang null

SELECT SUM(CASE when country IS NULL then revenue else 0 end) / SUM(revenue)
FROM orders;
+-------+
| .25   |
+-------+
Pelaporan dengan SQL

Ayo berlatih!

Pelaporan dengan SQL

Preparing Video For Download...