Null'larla başa çıkma

SQL ile Raporlama

Tyler Pernes

Learning & Development Consultant

Null gerçekte ne demek?

+--------+-----------------+
| order  | price_per_unit  |
|--------|-----------------|
| 1      | 4.50            |
| 2      | 2.25            |
| 3      | null            |
+--------+-----------------+
  • Henüz işlenmedi mi?
  • Ücretsiz mi?
  • Sabit fiyat mı?
SQL ile Raporlama

Null sorunları

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

Null sorunları

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

Null sorunları

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        |
+----------+-----------+
  • Null'un neyi temsil ettiği belirsiz!
SQL ile Raporlama

Çözüm 1: Null filtreleme

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;
SQL ile Raporlama

Çözüm 1: Null filtreleme

+--------+-----------------+
| order  | price_per_unit  |
|--------|-----------------|
| 1      | 4.50            |
| 2      | 2.25            |
+--------+-----------------+
SQL ile Raporlama

Çözüm 2: COALESCE()

Sözdizimi: 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        |
+-----------------------+-----------+
SQL ile Raporlama

Çözüm 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;
SQL ile Raporlama

Çözüm 2: COALESCE()

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

Sorgu sonucu oluşan null'lar

Nedenler:

  • LEFT JOIN tüm satırları eşleştirmez
  • Hiçbir CASE koşulu sağlanmaz
  • Daha birçok neden!
SQL ile Raporlama

Null'ların etkisini ölçmek

Null olan satırların oranı

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

Null olan gelirin oranı

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

Ayo berlatih!

SQL ile Raporlama

Preparing Video For Download...