-
Notifications
You must be signed in to change notification settings - Fork 1
/
2024-07-23-db-notes:-modern-sql.html
344 lines (339 loc) · 19.3 KB
/
2024-07-23-db-notes:-modern-sql.html
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="description" content="This is a personal note for the [[https://15445.courses.cs.cmu.edu/fall2022/notes/02-modernsql.pdf][CMU 15-445 L2 notes]]">
<link rel="alternate"
type="application/rss+xml"
href="https://chenyo.me/rss.xml"
title="RSS feed for https://chenyo.me">
<title>CMU 15-445 notes: Modern SQL</title>
<script type="text/javascript"
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'],['\\(','\\)']]}});
</script>
<meta name="author" content="chenyo">
<meta name="referrer" content="no-referrer">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="assets/style.css" type="text/css"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"/>
<link rel="favicon" type="image/x-icon" href="favicon.ico">
<script src="assets/search.js"></script></head>
<body>
<div id="preamble" class="status">
<header>
<h1><a href="https://chenyo.me">Chenyo's org-static-blog</a></h1>
<nav>
<a href="https://chenyo.me">Home</a>
<a href="archive.html">Archive</a>
<a href="tags.html">Tags</a>
<div id="search-container">
<input type="text" id="search-input" placeholder="Search anywhere...">
<i class="fas fa-search search-icon"></i>
</div>
</nav>
</header></div>
<div id="content">
<div class="post-date">23 Jul 2024</div><h1 class="post-title"><a href="https://chenyo.me/2024-07-23-db-notes:-modern-sql.html">CMU 15-445 notes: Modern SQL</a></h1>
<nav id="table-of-contents" role="doc-toc">
<h2>Table of Contents</h2>
<div id="text-table-of-contents" role="doc-toc">
<ul>
<li><a href="#org515af77">1. Terminology</a>
<ul>
<li><a href="#org64b79f9">1.1. SQL and relational algebra</a></li>
<li><a href="#orge60a6d0">1.2. SQL commands</a></li>
</ul>
</li>
<li><a href="#org45902cc">2. SQL syntax</a>
<ul>
<li><a href="#org694562e">2.1. Join</a></li>
<li><a href="#org459a7a6">2.2. Aggregation function</a></li>
<li><a href="#org7f18130">2.3. String operation</a></li>
<li><a href="#org7494271">2.4. Date and time</a></li>
<li><a href="#orgb771158">2.5. Output redirection</a></li>
<li><a href="#org1bda50d">2.6. Output control</a></li>
<li><a href="#orga836acd">2.7. Nested queries</a></li>
<li><a href="#org6d91bf7">2.8. Window functions</a></li>
<li><a href="#orgc124c24">2.9. Common Table Expressions (CTE)</a></li>
</ul>
</li>
</ul>
</div>
</nav>
<p>
This is a personal note for the <a href="https://15445.courses.cs.cmu.edu/fall2022/notes/02-modernsql.pdf">CMU 15-445 L2 notes</a>, along with some SQL command explained by <a href="https://claude.ai/chat/a2f07962-eb31-4f76-9a31-5e408722894b">Claude.ai</a>.
</p>
<div id="outline-container-org515af77" class="outline-2">
<h2 id="org515af77"><span class="section-number-2">1.</span> Terminology</h2>
<div class="outline-text-2" id="text-1">
</div>
<div id="outline-container-org64b79f9" class="outline-3">
<h3 id="org64b79f9"><span class="section-number-3">1.1.</span> SQL and relational algebra</h3>
<div class="outline-text-3" id="text-1-1">
<ul class="org-ul">
<li>Relational algebra is based on sets (unordered, no duplicates); SQL is based on bags (unordered, allows duplicates).</li>
<li>SQL is a declarative query language; users use SQL to specify the desired result, each DBMS determines the most efficient strategy to produce the answer.</li>
</ul>
</div>
</div>
<div id="outline-container-orge60a6d0" class="outline-3">
<h3 id="orge60a6d0"><span class="section-number-3">1.2.</span> SQL commands</h3>
<div class="outline-text-3" id="text-1-2">
<ul class="org-ul">
<li>Data manipulation language (DML): <code>SELECT</code>, <code>INSERT</code>, <code>UPDATE</code>, <code>DELETE</code>.</li>
<li><p>
Data definition language (DDL): <code>CREATE</code>.
</p>
<div class="org-src-container">
<pre class="src src-sql"><span style="color: #51afef;">CREATE</span> TABLR student (
sid <span style="color: #ECBE7B;">INT</span> <span style="color: #51afef;">PRIMARY</span> <span style="color: #51afef;">KEY</span>,
<span style="color: #51afef;">name</span> <span style="color: #ECBE7B;">VARCHAR</span>(<span style="color: #da8548; font-weight: bold;">16</span>),
login <span style="color: #ECBE7B;">VARCHAR</span>(<span style="color: #da8548; font-weight: bold;">32</span>) <span style="color: #51afef;">UNIQUE</span>,
age <span style="color: #ECBE7B;">SMALLINT</span>,
gpa <span style="color: #ECBE7B;">FLOAT</span>
);
</pre>
</div></li>
<li>Data control language (DCL): security, access control.</li>
</ul>
</div>
</div>
</div>
<div id="outline-container-org45902cc" class="outline-2">
<h2 id="org45902cc"><span class="section-number-2">2.</span> SQL syntax</h2>
<div class="outline-text-2" id="text-2">
</div>
<div id="outline-container-org694562e" class="outline-3">
<h3 id="org694562e"><span class="section-number-3">2.1.</span> Join</h3>
<div class="outline-text-3" id="text-2-1">
<ul class="org-ul">
<li><p>
Combine columns from one or more tables and produces a new table.
</p>
<div class="org-src-container">
<pre class="src src-sql"><span style="color: #5B6268;">-- </span><span style="color: #5B6268;">All students that get an A in 15-721</span>
<span style="color: #51afef;">SELECT</span> s.<span style="color: #51afef;">name</span>
<span style="color: #51afef;">FROM</span> enrolled <span style="color: #51afef;">AS</span> e, student <span style="color: #51afef;">AS</span> s
<span style="color: #51afef;">WHERE</span> e.grade = <span style="color: #98be65;">'A'</span> <span style="color: #51afef;">AND</span> e.cid = <span style="color: #98be65;">'15-721'</span>
<span style="color: #51afef;">AND</span> e.sid = s.sid
</pre>
</div></li>
</ul>
</div>
</div>
<div id="outline-container-org459a7a6" class="outline-3">
<h3 id="org459a7a6"><span class="section-number-3">2.2.</span> Aggregation function</h3>
<div class="outline-text-3" id="text-2-2">
<ul class="org-ul">
<li><code>AVG(COL)</code>, <code>MIN(COL)</code>, <code>MAX(COL)</code>, <code>COUNT(COL)</code>.</li>
<li><p>
Take as input a bag of tuples and produce a single scalar value.
</p>
<div class="org-src-container">
<pre class="src src-sql"><span style="color: #5B6268;">-- </span><span style="color: #5B6268;">Get number of students and their average GPA with a '@cs' login</span>
<span style="color: #51afef;">SELECT</span> <span style="color: #c678dd;">AVG</span>(gpa), <span style="color: #c678dd;">COUNT</span>(sid) <span style="color: #51afef;">FROM</span> student <span style="color: #51afef;">WHERE</span> login <span style="color: #51afef;">LIKE</span> <span style="color: #98be65;">'@cs'</span>;
<span style="color: #5B6268;">-- </span><span style="color: #5B6268;">Get the unique students</span>
<span style="color: #51afef;">SELECT</span> <span style="color: #c678dd;">COUNT</span>(<span style="color: #51afef;">DISTINCT</span> login) <span style="color: #51afef;">FROM</span> student <span style="color: #51afef;">WHERE</span> login <span style="color: #51afef;">LIKE</span> <span style="color: #98be65;">'@cs'</span>;
</pre>
</div></li>
<li><p>
Non-aggregated values in <code>SELECT</code> output must appear in <code>GROUP BY</code>.
</p>
<div class="org-src-container">
<pre class="src src-sql"><span style="color: #5B6268;">-- </span><span style="color: #5B6268;">Get the average GPA in each course</span>
<span style="color: #51afef;">SELECT</span> <span style="color: #c678dd;">AVG</span>(s.gpa), e.cid
<span style="color: #51afef;">FROM</span> enrolled <span style="color: #51afef;">AS</span> e, student <span style="color: #51afef;">AS</span> s
<span style="color: #51afef;">WHERE</span> e.sid = s.sid
<span style="color: #51afef;">GROUP</span> <span style="color: #51afef;">BY</span> e.cid;
</pre>
</div></li>
<li><p>
<code>HAVING</code>: filter output results based on aggregation computation.
</p>
<div class="org-src-container">
<pre class="src src-sql"><span style="color: #51afef;">SELECT</span> <span style="color: #c678dd;">AVG</span>(s.gpa), e.cid
<span style="color: #51afef;">FROM</span> enrolled <span style="color: #51afef;">AS</span> e, student <span style="color: #51afef;">AS</span> s
<span style="color: #51afef;">WHERE</span> e.sid = s.sid
<span style="color: #51afef;">GROUP</span> <span style="color: #51afef;">BY</span> e.cid
<span style="color: #51afef;">HAVING</span> <span style="color: #c678dd;">AVG</span>(s.gpa) > <span style="color: #da8548; font-weight: bold;">3.9</span>;
</pre>
</div></li>
</ul>
</div>
</div>
<div id="outline-container-org7f18130" class="outline-3">
<h3 id="org7f18130"><span class="section-number-3">2.3.</span> String operation</h3>
<div class="outline-text-3" id="text-2-3">
<ul class="org-ul">
<li>Strings are case sensitive and single-quotes only in the SQL standard.</li>
<li>Use <code>LIKE</code> for string pattern matching:
<ul class="org-ul">
<li><code>%</code> matches any sub-string,</li>
<li><code>_</code> matches any one character</li>
</ul></li>
<li>Standard string functions: <code>UPPER(S)</code>, <code>SUBSTRING(S, B, E)</code>.</li>
<li><code>||</code>: string concatenation.</li>
</ul>
</div>
</div>
<div id="outline-container-org7494271" class="outline-3">
<h3 id="org7494271"><span class="section-number-3">2.4.</span> Date and time</h3>
<div class="outline-text-3" id="text-2-4">
<ul class="org-ul">
<li>Attributes: <code>DATE</code>, <code>TIME</code>.</li>
<li>Different DBMS have different date/time operations.</li>
</ul>
</div>
</div>
<div id="outline-container-orgb771158" class="outline-3">
<h3 id="orgb771158"><span class="section-number-3">2.5.</span> Output redirection</h3>
<div class="outline-text-3" id="text-2-5">
<ul class="org-ul">
<li><p>
One can store the results into another table
</p>
<div class="org-src-container">
<pre class="src src-sql"><span style="color: #5B6268;">-- </span><span style="color: #5B6268;">output to a non-existing table</span>
<span style="color: #51afef;">SELECT</span> <span style="color: #51afef;">DISTINCT</span> cis <span style="color: #51afef;">INTO</span> CourseIds <span style="color: #51afef;">FROM</span> enrolled;
<span style="color: #5B6268;">-- </span><span style="color: #5B6268;">output to an existing table with the same number of columns and column type</span>
<span style="color: #5B6268;">-- </span><span style="color: #5B6268;">but the names do not matter</span>
<span style="color: #51afef;">INSERT</span> <span style="color: #51afef;">INTO</span> CourseIds (<span style="color: #51afef;">SELECT</span> <span style="color: #51afef;">DISTINCT</span> cid <span style="color: #51afef;">FROM</span> enrolled);
</pre>
</div></li>
</ul>
</div>
</div>
<div id="outline-container-org1bda50d" class="outline-3">
<h3 id="org1bda50d"><span class="section-number-3">2.6.</span> Output control</h3>
<div class="outline-text-3" id="text-2-6">
<ul class="org-ul">
<li>Use <code>ORDER</code>, <code>ASC</code> and <code>DESC</code> to sort the output tuples; otherwise the output could have different order every time.</li>
<li><p>
Use <code>LIMIT</code>, <code>OFFSET</code> to restrict the output number.
</p>
<div class="org-src-container">
<pre class="src src-sql"><span style="color: #51afef;">SELECT</span> sid <span style="color: #51afef;">FROM</span> enrolled <span style="color: #51afef;">WHERE</span> cid = <span style="color: #98be65;">'15-721'</span>
<span style="color: #51afef;">ORDER</span> <span style="color: #51afef;">BY</span> <span style="color: #c678dd;">UPPER</span>(grade) <span style="color: #51afef;">DESC</span>, sid + <span style="color: #da8548; font-weight: bold;">1</span> <span style="color: #51afef;">ASC</span>;
<span style="color: #51afef;">LIMIT</span> <span style="color: #da8548; font-weight: bold;">10</span> OFFSET <span style="color: #da8548; font-weight: bold;">10</span>; <span style="color: #5B6268;">-- </span><span style="color: #5B6268;">output 10 tuples, starting from the 11th tuple</span>
</pre>
</div></li>
</ul>
</div>
</div>
<div id="outline-container-orga836acd" class="outline-3">
<h3 id="orga836acd"><span class="section-number-3">2.7.</span> Nested queries</h3>
<div class="outline-text-3" id="text-2-7">
<ul class="org-ul">
<li>Nested queries are often difficult to optimize.</li>
<li>The inner query can access attributes defined in the outer query.</li>
<li><p>
Inner queries can appear anywhere.
</p>
<div class="org-src-container">
<pre class="src src-sql"><span style="color: #5B6268;">-- </span><span style="color: #5B6268;">Output a column 'one' with 1s, the number of 1s</span>
<span style="color: #5B6268;">-- </span><span style="color: #5B6268;">equals to the number of rows in 'student'</span>
<span style="color: #51afef;">SELECT</span> (<span style="color: #51afef;">SELECT</span> <span style="color: #da8548; font-weight: bold;">1</span>) <span style="color: #51afef;">AS</span> one <span style="color: #51afef;">FROM</span> student;
<span style="color: #5B6268;">-- </span><span style="color: #5B6268;">Get the names of students that are enrolled in '15-445'</span>
<span style="color: #51afef;">SELECT</span> <span style="color: #51afef;">name</span> <span style="color: #51afef;">FROM</span> students
<span style="color: #51afef;">WHERE</span> sid <span style="color: #51afef;">IN</span> (
<span style="color: #51afef;">SELECT</span> sid <span style="color: #51afef;">FROM</span> enrolled
<span style="color: #51afef;">WHERE</span> cid = <span style="color: #98be65;">'15-445'</span>
);
<span style="color: #5B6268;">-- </span><span style="color: #5B6268;">Get student record with the highest id</span>
<span style="color: #5B6268;">-- </span><span style="color: #5B6268;">that is enrolled in at least one course.</span>
<span style="color: #51afef;">SELECT</span> student.sid, <span style="color: #51afef;">name</span>
<span style="color: #51afef;">FROM</span> student
<span style="color: #5B6268;">-- </span><span style="color: #5B6268;">the intermediate output is aliases as max_e</span>
<span style="color: #51afef;">JOIN</span> (<span style="color: #51afef;">SELECT</span> <span style="color: #c678dd;">MAX</span>(sid) <span style="color: #51afef;">AS</span> sid <span style="color: #51afef;">FROM</span> enrolled) <span style="color: #51afef;">AS</span> max_e
<span style="color: #5B6268;">-- </span><span style="color: #5B6268;">only select student who has the max_e</span>
<span style="color: #51afef;">ON</span> student.sid = max_e.sid;
<span style="color: #5B6268;">-- </span><span style="color: #5B6268;">the above is same as below, but `join` syntax is more preferred</span>
<span style="color: #51afef;">SELECT</span> student.sid, <span style="color: #51afef;">name</span>
<span style="color: #51afef;">FROM</span> student <span style="color: #51afef;">AS</span> s, (<span style="color: #51afef;">SELECT</span> <span style="color: #c678dd;">MAX</span>(sid) <span style="color: #51afef;">AS</span> sid <span style="color: #51afef;">FROM</span> enrolled) <span style="color: #51afef;">AS</span> max_e
<span style="color: #51afef;">WHERE</span> s.sid = max_e.sid;
</pre>
</div></li>
<li>Nested query results expression:
<ul class="org-ul">
<li><code>ALL</code>: must satisfy expression for all <b><b>rows</b></b> in sub-query.</li>
<li><code>ANY</code>, <code>IN</code>: must satisfy expression for at least one row in sub-query.</li>
<li><p>
<code>EXISTS</code>: at least one row is returned.
</p>
<div class="org-src-container">
<pre class="src src-sql"><span style="color: #5B6268;">-- </span><span style="color: #5B6268;">Get all courses with no students enrolled in</span>
<span style="color: #51afef;">SELECT</span> * <span style="color: #51afef;">FROM</span> course
<span style="color: #51afef;">WHERE</span> <span style="color: #51afef;">NOT</span> <span style="color: #51afef;">EXISTS</span>(
<span style="color: #51afef;">SELECT</span> * <span style="color: #51afef;">FROM</span> enrolled
<span style="color: #51afef;">WHERE</span> course.cid = enrolled.cid
)
<span style="color: #5B6268;">-- </span><span style="color: #5B6268;">Get students whose gpa is larget than the highest score in '15-712'</span>
<span style="color: #5B6268;">-- </span><span style="color: #5B6268;">and the login has a level > 3</span>
<span style="color: #51afef;">SELECT</span> student.sid, <span style="color: #51afef;">name</span>
<span style="color: #51afef;">FROM</span> student <span style="color: #51afef;">AS</span> S
<span style="color: #51afef;">WHERE</span> s.gpa > <span style="color: #51afef;">ALL</span> (
<span style="color: #51afef;">SELECT</span> course.score <span style="color: #51afef;">FROM</span> course
<span style="color: #51afef;">WHERE</span> course.cid = <span style="color: #98be65;">'15-712'</span>
)
<span style="color: #51afef;">AND</span> student.login <span style="color: #51afef;">IN</span> (
<span style="color: #51afef;">SELECT</span> login <span style="color: #51afef;">FROM</span> enrolled
<span style="color: #51afef;">WHERE</span> <span style="color: #51afef;">level</span> > <span style="color: #da8548; font-weight: bold;">3</span>
);
</pre>
</div></li>
</ul></li>
</ul>
</div>
</div>
<div id="outline-container-org6d91bf7" class="outline-3">
<h3 id="org6d91bf7"><span class="section-number-3">2.8.</span> Window functions</h3>
<div class="outline-text-3" id="text-2-8">
<ul class="org-ul">
<li>Perform sliding calculation across a set of tuples.</li>
</ul>
</div>
</div>
<div id="outline-container-orgc124c24" class="outline-3">
<h3 id="orgc124c24"><span class="section-number-3">2.9.</span> Common Table Expressions (CTE)</h3>
<div class="outline-text-3" id="text-2-9">
<ul class="org-ul">
<li>An alternative to windows or nested queries when writing more complex queries.</li>
<li><p>
CTEs use <code>WITH</code> to bind the output of an inner query to a temporary table.
</p>
<div class="org-src-container">
<pre class="src src-sql"><span style="color: #51afef;">WITH</span> cteName (col1, col2) <span style="color: #51afef;">AS</span> (
<span style="color: #51afef;">SELECT</span> <span style="color: #da8548; font-weight: bold;">1</span>, <span style="color: #da8548; font-weight: bold;">2</span>
)
<span style="color: #51afef;">SELECT</span> col1 + col2 <span style="color: #51afef;">FROM</span> cteName;
</pre>
</div></li>
</ul>
</div>
</div>
</div>
<div class="taglist"><a href="https://chenyo.me/tags.html">Tags</a>: <a href="https://chenyo.me/tag-study.html">study</a> <a href="https://chenyo.me/tag-database.html">database</a> <a href="https://chenyo.me/tag-cmu.html">cmu</a> </div></div>
<div id="postamble" class="status"><div id="search-results"></div>
<footer>
<div class="footer-content">
<div class="footer-left">
<p>© 2024 chenyo. Some rights reserved.</p>
<a rel="license" href="http://creativecommons.org/licenses/by-nc/4.0/">
<img alt="Creative Commons License" style="border-width: 0"
src="https://i.creativecommons.org/l/by-nc/4.0/88x31.png"/>
</a>
</div>
<div class="social-links">
<a href="https://t.me/chenyo17" target="_blank" rel="noopener noreferrer">
<i class="fab fa-telegram"></i>
</a>
<a href="https://github.com/chenyo-17" target="_blank" rel="noopener noreferrer">
<i class="fab fa-github"></i>
</a>
</div>
</footer></div>
</body>
</html>