Lesson 3 of 5 · 2 min
Joins
A join matches rows from two tables. A left join keeps every row on the left even when nothing matches, which is how you find what is missing.
import sqlite3
db = sqlite3.connect(":memory:")
db.executescript("""
create table docs (id int, title text);
create table chunks (doc_id int, body text);
insert into docs values (1, 'refunds'), (2, 'shipping'), (3, 'privacy');
insert into chunks values (1, 'thirty days'), (1, 'damaged items'), (2, 'two days');
""")
query = """
select d.title, count(c.body) as pieces
from docs d left join chunks c on c.doc_id = d.id
group by d.id
"""
print(db.execute(query).fetchall())
The zero is the useful part: one document was never indexed.
