You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+51Lines changed: 51 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,8 @@
2
2
3
3
A (somewhat opinionated) list of SQL tips and tricks I've picked up over the years.
4
4
5
+
Please note that some of these tips might not be relevant for all RDBMs. For example, the `::` syntax ([tip 6](#you-can-use-the--operator-to-cast-the-data-type-of-a-value)) does not work in SQLite.
6
+
5
7
Feel free to contribute your own by opening a pull requests!
6
8
7
9
## Table of contents
@@ -13,6 +15,8 @@ Feel free to contribute your own by opening a pull requests!
13
15
5)[Rename calculated fields to avoid ambiguity](#rename-calculated-fields-to-avoiding-ambiguity)
14
16
6)[You can use the `::` operator to cast the data type of a value](#you-can-use-the--operator-to-cast-the-data-type-of-a-value)
15
17
7)[You can (but shouldn't always) `GROUP BY` column position](#you-can-but-shouldnt-always-group-by-column-position)
18
+
8)[Anti-joins are your friend](#anti-joins-are-your-friend)
19
+
9)[Comment your code!](#comment-your-code)
16
20
17
21
18
22
## Use a leading comma to separate fields
@@ -145,4 +149,51 @@ FROM employees
145
149
GROUP BY1-- dept_no is the first column in the SELECT clause.
146
150
ORDER BY2DESC
147
151
;
152
+
```
153
+
154
+
## Anti-joins are your friend
155
+
Anti-joins are incredible useful, mostly (in my experience) for when when you only want to return rows/values from one table that aren't present in another table.
156
+
- You could instead use a subquery although conventional wisdom dictates that
157
+
anti-joins are faster.
158
+
-`EXCEPT` is an interesting operator for removing rows from one table which appear in another query table but I suggest you read up on it further before using it.
159
+
160
+
```SQL
161
+
-- Anti-join.
162
+
SELECT
163
+
video_content.*
164
+
FROM video_content
165
+
LEFT JOIN archive
166
+
onvideo_content.series_id=archive.series_id
167
+
WHERE1=1
168
+
ANDarchive.series_id IS NULL-- Any rows with no match will have a NULL value.
169
+
170
+
-- Subquery.
171
+
SELECT
172
+
*
173
+
FROM video_content
174
+
WHERE1=1
175
+
AND series_id NOT IN (SELECT DISTINCT SERIES_ID FROM archive) -- Be mindful of NULL values (see tip 4).
176
+
177
+
-- EXCEPT.
178
+
SELECT series_id
179
+
FROM video_content
180
+
EXCEPT
181
+
SELECT series_id
182
+
FROM archive
183
+
```
184
+
185
+
## Comment your code!
186
+
While in the moment you know why you did something if you revisit
187
+
the code weeks, months or years later you might not remember.
188
+
- In general you should strive to write comments that explain why you did something, not how.
189
+
- Your colleagues and future self will thank you!
190
+
191
+
```SQL
192
+
SELECT
193
+
*
194
+
FROM video_content
195
+
LEFT JOIN archive -- New CMS cannot process archive video formats.
0 commit comments