Skip to content

Commit ac763be

Browse files
committed
added more tips
1 parent 70c68d3 commit ac763be

File tree

3 files changed

+52
-14
lines changed

3 files changed

+52
-14
lines changed

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
A (somewhat opinionated) list of SQL tips and tricks I've picked up over the years.
44

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+
57
Feel free to contribute your own by opening a pull requests!
68

79
## Table of contents
@@ -13,6 +15,8 @@ Feel free to contribute your own by opening a pull requests!
1315
5) [Rename calculated fields to avoid ambiguity](#rename-calculated-fields-to-avoiding-ambiguity)
1416
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)
1517
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)
1620

1721

1822
## Use a leading comma to separate fields
@@ -145,4 +149,51 @@ FROM employees
145149
GROUP BY 1 -- dept_no is the first column in the SELECT clause.
146150
ORDER BY 2 DESC
147151
;
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+
on video_content.series_id = archive.series_id
167+
WHERE 1=1
168+
AND archive.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+
WHERE 1=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.
196+
on video_content.series_id = archive.series_id
197+
WHERE 1=1
198+
AND archive.series_id IS NULL
148199
```

examples.sql

Lines changed: 0 additions & 14 deletions
This file was deleted.

placeholder.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SELECT *

0 commit comments

Comments
 (0)