Let us understand how to handle nulls.
-
By default if we try to add or concatenate null to another column or expression or literal, it will return null.
-
If we want to replace null with some default value, we can use coalesce
.
-
coalesce
 returns first not null value if we pass multiple arguments to it.
-
We have a function called as nullif
. If the first argument is equal to second argument, it returns null. It is typically used when we compare against 2 columns where nulls are also involved.
-
You might have seen functions like nvl
, nvl2
 etc with respect to databases like Oracle. Postgres does not support them.
The sql extension is already loaded. To reload it, use:
%reload_ext sql
env: DATABASE_URL=postgresql://itversity_retail_user:retail_password@localhost:5432/itversity_retail_db
* postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db
1 rows affected.
* postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db
1 rows affected.
* postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db
1 rows affected.
* postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db
Done.
[]
* postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db
Done.
[]
* postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db
5 rows affected.
[]
* postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db
5 rows affected.
sales_person_id |
sales_amount |
commission_pct |
1 |
1000.0 |
10 |
2 |
1500.0 |
8 |
3 |
500.0 |
None |
4 |
800.0 |
5 |
5 |
250.0 |
None |
* postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db
5 rows affected.
sales_person_id |
sales_amount |
commission_pct |
incorrect_commission_amount |
1 |
1000.0 |
10 |
100.00 |
2 |
1500.0 |
8 |
120.00 |
3 |
500.0 |
None |
None |
4 |
800.0 |
5 |
40.00 |
5 |
250.0 |
None |
None |
* postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db
5 rows affected.
sales_person_id |
sales_amount |
commission_pct |
commission_pct_1 |
1 |
1000.0 |
10 |
10 |
2 |
1500.0 |
8 |
8 |
3 |
500.0 |
None |
0 |
4 |
800.0 |
5 |
5 |
5 |
250.0 |
None |
0 |
* postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db
5 rows affected.
sales_person_id |
sales_amount |
commission_pct |
commission_amount |
1 |
1000.0 |
10 |
100.00 |
2 |
1500.0 |
8 |
120.00 |
3 |
500.0 |
None |
0.00 |
4 |
800.0 |
5 |
40.00 |
5 |
250.0 |
None |
0.00 |
* postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db
1 rows affected.
* postgresql://itversity_retail_user:***@localhost:5432/itversity_retail_db
1 rows affected.