Week 4 - Who worked with whom?

The Camp_assignment table lists where each person worked and when. Your goal is to answer, Who worked with whom? That is, you are to find all pairs of people who worked at the same site, and whose date ranges overlap while at that site. This can be solved using a self-join.

A self-join of a table is a regular join, but instead of joining two different tables, we join two copies of the same table, which we will call the “A” copy and the “B” copy:

SELECT * FROM Camp_assignment A JOIN Camp_assignment B ...

The idea is that the above join will give us rows that pair every person/site/date range with every other person/site/date range. With no conditions on the join, since there are \(441\) rows in the Camp_assignment table, the join will produce \(441^2 = 194481\) rows. But out of all those rows we want only those where the two people worked at the same site. So:

┌─────────┬────────────┬──────────────┐
│  Site   │ Observer_1 │  Observer_2  │
│ varchar │  varchar   │   varchar    │
├─────────┼────────────┼──────────────┤
│ lkri    │ apopovkina │ jloshchagina │
│ lkri    │ apopovkina │ gsedash      │
└─────────┴────────────┴──────────────┘

Submit your final SQL query.

Bonus problem!

Produce this much nicer table by joining with the Personnel table:

┌─────────┬─────────────────────┬───────────────────┐
│  Site   │       Name_1        │      Name_2       │
│ varchar │       varchar       │      varchar      │
├─────────┼─────────────────────┼───────────────────┤
│ lkri    │ Anastasia Popovkina │ Gleb Sedash       │
│ lkri    │ Anastasia Popovkina │ Julya Loshchagina │
└─────────┴─────────────────────┴───────────────────┘

You’ll need to join with the Personnel table twice, once for each observer column. You may need give abbreviations to tables (e.g., JOIN Personnel AS p1) to distinguish the tables and columns. You can do it!


This work is licensed under CC BY 4.0

UCSB logo