Questions tagged [relational-division]

Operation in relational algebra or in an RDBMS (mostly SQL) to partition relations. The inverse of a Cartesian product (CROSS JOIN in SQL).

E.F. Codd identified eight relational algebra operators in his defining paper "A Relational Model of Data for Large Shared Data Banks". Division is the least known and probably most complex of them. It is the inverse operation of a Cartesian product (CROSS JOIN in SQL).

It means partitioning a (bigger) table with rows from another (smaller) table. For requirements like:
"Find tuples that combine a particular value in one attribute with several values in another attribute."

A practical example for a setup with a many-to-many relationship between clubs and people:
"Find all clubs where Sue and John and James are members."

More information

467 questions
4
votes
2 answers

Activerecord return objects that match ALL attributes in array

Right now I have this query: @events = Event.joins(:packages).where("packages.kind IN (?)", packages).distinct And it returns objects that match just a single attribute in the packages array. I would like ActiveRecord to only return objects that…
Antonio
  • 731
  • 7
  • 28
4
votes
3 answers

Select people with latest balance for one credit card being greater than for another

In a PostgreSQL 9.5.3 DB, I have a credit_card_balances table referencing a persons table which tracks the balances of various credit cards associated to a particular person: CREATE TABLE persons ( id serial PRIMARY KEY, name text ); CREATE…
ira
  • 735
  • 1
  • 7
  • 12
4
votes
1 answer

Forming groups of spatio-temporally near trajectories in R or PostgreSQL

I'm doing some trajectory analysis using R and PostgreSQL. In order to form groups of trajectory segments where successive positions are spatio-temporally near, I've created the following table. What I'm still missing is the column group_id, which…
4
votes
3 answers

Group by having at least one of each item

Say I have a table matching person_ids to pets. I have a list of the ideal pets that a homeowner must have (at least one of each) and, from the following table, I want to see who meets the requirements. That list is, of course, (dog, cat, tiger).…
aralar
  • 3,022
  • 7
  • 29
  • 44
4
votes
4 answers

Compound course prerequisites (One or more of a,b,c and either x or y as well as z style)

Thanks everyone for the input, especially during the closing hours of the bounty, it's been incredible helpful. This is a followup question to Select courses that are completely satisfied by a given list of prerequisites, and further explains the…
lberezy
  • 450
  • 5
  • 19
4
votes
3 answers

Select courses that are completely satisfied by a given list of prerequisites

I'm trying to write an SQL query that will return a list of courses that a person is eligible for given a list of their completed subjects (to be used as prerequisites). I have my database laid out as such. Prerequisite: …
lberezy
  • 450
  • 5
  • 19
4
votes
2 answers

What does a double not exists clause mean?

SELECT c.name FROM Customer c WHERE NOT EXISTS(SELECT w.WID FROM Woker w WHERE NOT EXISTS(SELECT la FROM look_after la WHERE la.CID…
Halso Johnson
  • 75
  • 1
  • 4
4
votes
1 answer

Having Trouble Writing SQL query Many To Many Relationship

I have a Django application with the following postgres db tables: Publication and Tag Publication { title tags } Tag { title } Tag and Publication have a many to many relationship. What I want to do is do an and/or combo search: for…
jac300
  • 5,182
  • 14
  • 54
  • 89
4
votes
3 answers

How to return rows that have the same column values in MySql

Lets consider the following table- ID Score 1 95 2 100 3 88 4 100 5 73 I am a total SQL noob but how do I return the Scores featuring both IDs 2 and 4? So it should return 100 since its featured in both ID 2 and 4
4
votes
2 answers

SQL find sets with common members (relational division)

I have separate sets of "classes" and "groups", each of which has been assigned one or more tags. I would like to find, for each group, the subset of classes that contains the same (or more) tags for each group. Some sample data: declare @Groups…
4
votes
2 answers

Relational division in LINQ?

Relational division is one of Codd's primitive relational operators, colloquially known as the suppliers who supply all parts. There have been various translations into SQL e.g. Celko discusses several approaches using the example of the pilots who…
onedaywhen
  • 55,269
  • 12
  • 100
  • 138
4
votes
1 answer

Sql IN function

I have something like this as an sql table: EmplID  SkillID 1780      128 1780      133 2061      128 2068      128 Lets say I pass in a string to a stored proc as in '128,133'. I have a split function so the sql might be like the following: SELECT…
David Whitten
  • 573
  • 1
  • 4
  • 12
3
votes
3 answers

Selecting primary from a mapping row SQL

I have a table that references a bunch of articles, the table contains tags for those articles. Like this: tag text article_id bigint I want to select all article_ids with a set of tags, say tag1, tag2, tag3 but the article could also have tag4,…
ehiller
  • 1,346
  • 17
  • 32
3
votes
2 answers

Finding the id's which include multiple criteria in long format

Suppose I have a table like this, id tagId 1 1 1 2 1 5 2 1 2 5 3 2 3 4 3 5 3 8 I want to select id's where tagId includes both 2 and 5. For this fake data set, It should return 1 and 3. I tried, select id from…
maydin
  • 3,715
  • 3
  • 10
  • 27
3
votes
2 answers

Given a set of IDs, Return the Subset of Orders with only those IDs

Given a set of product_ids, what are the order_ids that only have those product_ids? For the example below, I'm only wanting order_ids that have some combination of (a,b,c). I have 2 tables like below: "transactions" table: order_id | product_id…
1 2
3
31 32