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
3
votes
1 answer

array_agg contains another array_agg

t1 id|entity_type 9|3 9|4 9|5 2|3 2|5 t2 id|entity_type 1|3 1|4 1|5 SELECT t1.id, array_agg(t1.entity_type) FROM t1 GROUP BY t1.id HAVING ARRAY_AGG(t1.entity_type ORDER BY t1.entity_type) = (SELECT…
Marcin
  • 57
  • 7
3
votes
2 answers

Relational Algebra - Finding EXACTLY two values

Below is a Table and simple SQL to find the cities that occur in exactly two states Name State ---- ----- Paris MO Paris TX Paris VA Austin MA Austin TX Burling VT Result: Only Austin will qualify as it occurs in exactly two…
3
votes
2 answers

SQL: how to get rows containing only certain ids?

I've got the following table: +--------+--------+ | group | user | +--------+--------+ | 1 | 1 | | 1 | 2 | | 2 | 1 | | 2 | 2 | | 2 | 3 | +--------+--------+ I need to select group, containing…
Roman Bekkiev
  • 3,010
  • 1
  • 24
  • 31
3
votes
3 answers

Postgresql select people you may know orderded by number of mutual friends

I'm building a social network, and I want my members to be able to easily find new friends. Just like in Facebook, I want to suggest them some people they may know by the number of mutual friends they have. My PostgreSQL database structure for…
user1026090
  • 458
  • 3
  • 9
  • 23
3
votes
3 answers

I need to use a comma delimited string for SQL AND condition on each word

So I have a parameter. Lets say: @Tags = 'Red,Large,New' I have a field in my table called [Tags] Lets say one particular row that field contains "Red,Large,New,SomethingElse,AndSomethingElse" How can I break that apart to basically achieve the…
user1447679
  • 3,076
  • 7
  • 32
  • 69
3
votes
2 answers

SQL query for finding pairs that share the same set of values

I have table Tasks(employee_name, task) ------------------ Joe | taskA Joe | taskB Ted | taskA Jim | taskB Ray | taskA Ray | taskB John| taskA Tim | taskC I need to find all pairs of employees that have the same set of tasks. For example using the…
hebime
  • 577
  • 1
  • 5
  • 15
3
votes
2 answers

SQL server,find distinct groups in a table

I have table prdID item percentage 1 10 50 1 20 50 2 10 50 2 20 50 3 20 30 3 20 70 4 10 50 4 20 30 4 30 …
user2333435
  • 41
  • 1
  • 4
3
votes
1 answer

Count items in category A & B (MySQL)

I’m trying to alter something to make it work for me. I think my question is pretty easy to anwer for someone who knows SQL. I have the following table (two columns): entry_id | cat_id 5 | 3 6 | 3 7 | 3 7 | 5 7 |…
3
votes
2 answers

tricky sql query - finding alternative supplier( relational division )

This is a question I got from a book (don't remember which), it goes like this: You have three tables: Supplier (supId, name) Product (prodId, name) inventory (supId, prodId) You need to find, with one query, all the suppliers that have in their…
Ami Malimovka
  • 437
  • 7
  • 22
3
votes
4 answers

How to find if a list/set is contained within another list

I have a list of product IDs and I want to find out which orders contain all those products. Orders table is structured like this: order_id | product_id ---------------------- 1 | 222 1 | 555 2 | 333 Obviously I can do it with…
Amati
  • 1,484
  • 1
  • 16
  • 29
3
votes
3 answers

SQL aggregate unique pairs

I have a PostgreSQL table that is mostly a bridge table but it also has some extra stuff. Essentially it holds the information about players in a game. So we have a unique id for this instance of a player in a game. Then an id that is FK to game…
Tom Carrick
  • 6,349
  • 13
  • 54
  • 78
2
votes
4 answers

What are ways to get mutual friends in a (1, 2) (2, 1) friendship scenario?

I'm developing a website wherein when a person adds another person as a friend, 2 entries are created. Let's say uid 1 adds uid 2 as a friend, the following rows are created in MySQL. activity_id uid1 uid2 1 1 2 2 2 …
MikeS
  • 383
  • 2
  • 7
  • 22
2
votes
3 answers

MSSQL Select with "vertical"-where

I don't really know how to explain except with "vertical where". Imagine the following table: TAGID|PRODUCTID|SHOP_ID 59 |3418-7 |38 61 |3418-7 |38 60 |4227-4 |38 61 |4227-4 |38 Now I want to return all product IDs, that have…
Esben
  • 1,943
  • 1
  • 18
  • 37
2
votes
3 answers

Can you solve this simple SQL query?

Suppose it's a website that sells photo cameras. Here are my entities (tables): Camera: A simple camera Feature: A feature like: 6mp, max resolution 1024x768, The thing is between cameras and feature i've got a Many to Many relationship, so i have…
santiagobasulto
  • 11,320
  • 11
  • 64
  • 88
2
votes
1 answer

Joining 3 tables SQL

I am trying to solve this problem for quite some time and the output is wrong. Can anyone help me with this? I am using mysql, so full join doesnt work. Thank yyou 3 tables: Frequents (attributes: drinker, bar, times_a_week), Likes (attributes:…
kaboom
  • 307
  • 6
  • 16