Skip to content

Instantly share code, notes, and snippets.

@fweep
Last active August 29, 2015 14:01
Show Gist options
  • Save fweep/5af1c5fc8084fe219129 to your computer and use it in GitHub Desktop.
Save fweep/5af1c5fc8084fe219129 to your computer and use it in GitHub Desktop.
SQL problem
create table file_upload (
id serial not null,
name text
);
create table reg_domain (
id serial not null,
name text
);
create table file_upload_reg_domain_map (
file_upload_id integer not null references file_upload(id),
reg_domain_id integer not null references reg_domain(id)
);
give list of reg_domain.id, e.g. [1, 2, 3]
need to find all file_upload.id that are common to every reg_domain.id
if file_upload_reg_domain_map contains:
file_upload_id reg_domain_id
-------------- -------------
1 1
1 2
1 3
2 1
2 2
3 1
then the query for reg_domain.id [1,2,3] commonalities should return file_upload.id 1 (only file_upload_id shared by all reg_domain_id)
given reg_domain.id [1,2,3,4], should return nothing, since reg_domain_id 4 has no file_upload_id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment