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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "22482673",
"metadata": {},
"outputs": [],
"source": [
"import agh_db_lectures\n",
"agh_db_lectures.prepare_notebook_for_sql()"
]
},
{
"cell_type": "markdown",
"id": "83f4fffc",
"metadata": {},
"source": [
"# The \"NULL\" value"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8ec0bb1a",
"metadata": {},
"outputs": [],
"source": [
"# Open the diagram in an image viewer for more convenience.\n",
"agh_db_lectures.nw_diagram.download_open()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d4c3ba6c",
"metadata": {},
"outputs": [],
"source": [
"# Connect to the (locally-hosted) database.\n",
"%sql postgresql://demo_user:demo_pwd@localhost:25432/agh_it_northwind"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b594cc93",
"metadata": {},
"outputs": [],
"source": [
"agh_db_lectures.download_restore_nw_postgres_dump()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fc14542d",
"metadata": {},
"outputs": [],
"source": [
"%%sql\n",
"SELECT company_name, phone, fax\n",
"FROM customers"
]
},
{
"cell_type": "markdown",
"id": "4e1266e1",
"metadata": {},
"source": [
"_notes_\n",
"\n",
"- Theodore, what would an employee do if an customer to record in the db had no phone number?\n",
" - Give many ideas.\n",
" - What would be their pros and cons be?\n",
"- `SELECT company_name, phone, fax`\n",
"- Jupyter (well, Python) happens to have `NULL` values converted to `None`s and presented to us this way.\n",
"- Do not work:\n",
" - `WHERE fax = NULL`, and\n",
" - `WHERE fax <> NULL`.\n",
"- Do work\n",
" - `WHERE fax IS NULL`, and\n",
" - `WHERE fax IS NOT NULL`.\n",
" - Also, `WHERE fax <> '011-4988261' OR fax = '011-4988261'` won't return all table rows."
]
},
{
"cell_type": "markdown",
"id": "ad4700a9",
"metadata": {},
"source": [
"## \"NULL\" in arithmetic and comparison operations"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c08e4ed5",
"metadata": {},
"outputs": [],
"source": [
"%%sql\n",
"SELECT NULL <> 'unknown fax number'"
]
},
{
"cell_type": "markdown",
"id": "b2619658",
"metadata": {},
"source": [
"_notes_\n",
"\n",
"- Try\n",
" - `SELECT NULL`,\n",
" - `SELECT NULL > 5`,\n",
" - `SELECT NULL <> 'unknown fax number'`,\n",
" - `SELECT NULL = NULL`, and\n",
" - `SELECT NULL IN (TRUE, FALSE, NULL)`,\n",
" - but note that a test for presence of `NULL` (or any other value) in an empty tuple (e.g., `SELECT NULL IN (SELECT 'whatever' LIMIT 0)`) would still give `FALSE`."
]
},
{
"cell_type": "markdown",
"id": "0613f611",
"metadata": {},
"source": [
"## \"NULL\" with logical operators"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3cf39c08",
"metadata": {},
"outputs": [],
"source": [
"%%sql\n",
"SELECT NOT NULL"
]
},
{
"cell_type": "markdown",
"id": "66b696f3",
"metadata": {},
"source": [
"_notes_\n",
"\n",
"- `TRUE OR NULL`\n",
"- `FALSE AND NULL`\n",
"- `NOT`"
]
},
{
"cell_type": "markdown",
"id": "502f27dc",
"metadata": {},
"source": [
"## The \"COALESCE\" function"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5af26eab",
"metadata": {},
"outputs": [],
"source": [
"%%sql\n",
"SELECT company_name,\n",
" CASE\n",
" WHEN fax IS NULL THEN\n",
" 'unknown'\n",
" ELSE\n",
" fax\n",
" END\n",
"FROM customers"
]
},
{
"cell_type": "markdown",
"id": "ff370664",
"metadata": {},
"source": [
"_notes_\n",
"\n",
"- Replace the `CASE` expression with `COALESCE(fax, 'unknown') AS fax`."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "/gnu/store/q35bxk1i5blx0rcgxphhrq9xqmgha9bz-python-3.11.11/bin/python3",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|