-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL NULL functon queries.sql
More file actions
130 lines (119 loc) · 3.29 KB
/
Copy pathSQL NULL functon queries.sql
File metadata and controls
130 lines (119 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
use salesdb;
/*In aggregate calculations null is totally ignored
except when using count(*)*/
-- Handle NULL in DATA AGGREGATIONS
-- Find the avg scores of customers
select
customerid,
score,
coalesce(score, 0) as score2,
round(avg(score) over(), 0) as avg_of_scores,
round(avg(coalesce(score, 0)) over(), 0) as avg_scores2
from customers;
-- Handle NULLS in MATHEMATICAL OPERATIONS
/* Dispaly teh full name of customers in a single field
by merging their first and last names
and add 10 bonus points to each customer score*/
select
customerid,
firstname,
lastname,
concat(firstname,' ',coalesce(lastname, '')) as full_name,
score,
coalesce(score, 0) + 10 bonus_score
from customers;
-- Handling NULLS in JOINS
-- Handling NULLS in SORTING DATA
/* Sort the customers from lowest to to highest score,
with nulls apperaing last */
select
customerid,
score,
coalesce(score, 999999999) type1_static,
case when score is null then 1 else 0 end type2_FLAG
from customers
order by coalesce(score, 999999999);
-- After solving CASE it sorts the data with same FLAG
select
customerid,
score,
case when score is null then 1 else 0 end type2_FLAG
from customers
order by case when score is null then 1 else 0 end, score;
-- NULL function NULLIF
-- Replaces a real value with a NULL
/* compares 2 expressions
-gives NULL if they are equal
-First value if they are not equal
nullif(value1, value2) */
-- Use Case DIVISION by ZERO
-- preventing the error dividing by zero
-- Find the sales price for each order by dividibg sales by quantity
select
orderid,
sales,
quantity,
round(sales / nullif(quantity, 0), 0) as price
from orders;
-- IS NULL & IS NOT NULL
-- IS NULL USE CASE FILTERING DATA
-- Searching for missing information
-- identify the customers who has no score
select
*
from customers
where score is null;
-- List all customers who have score
select
*
from customers
where score is not null;
-- USE CASE Anti-Joins
-- Find the unmatched rows between 2 tables
-- List all details of customers who have not placedany orders
select
c.*
from customers c
left join orders o
on c.customerid = o.customerid
where o.customerid is null; -- Better to use keyword
-- NULL, EMPTY STRING, BLANK SPACE
with orders as(
select 1 as id, 'A' as category union
select 2, NULL union
select 3, '' union
select 4, ' '
)
select
*,
length(category) as categorylen
from orders;
-- HANDLING NULLS - DATA POLICIES
/* 1. Only use NULLS and EMPTY string avoid blank spaces(use TRIM to remove blanks)
2. only use NULLS avoid both EMPTY string and BLANK spaces (Use NULLIF to rapalce
EMPTY string and BLANK spaces to NULL)
3.use default value 'UNKNOWN' and avoid using NULLS, EMPTY string, BLANK spaces
*/
with orders as(
select 1 as id, 'A' as category union
select 2, NULL union
select 3, '' union
select 4, ' '
)
select
*,
length(category) as categorylen,
trim(category) as policy1,
length(trim(category)) policy1_len,
nullif(trim(category), '') as policy2,
coalesce(nullif(trim(category), ''),'unknown') policy3,
length(coalesce(nullif(trim(category), ''),'unknown')) policy3_len
from orders;
/* USE CASES:
1. Data aggregations
2. Mathematical Operations
3. joining Tables
4. Sorting data
5. Finding unmatched data - Left, right-anti join
6. Data policies
*/