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
7
votes
4 answers

Return distinct pairs of names which have the same exact items in column

I want to find the distinct pairs of names in the table which have the same exact items in the items column. For instance: CREATE TABLE t ( name VARCHAR(255), item VARCHAR(255) ); INSERT INTO t VALUES("Alice", "Orange"); INSERT INTO t…
maregor
  • 777
  • 9
  • 32
6
votes
9 answers

What is a SQL statement to select an item that has several attributes in an item/attribute list?

Say I have a table that has items and attributes listed like, frog green cat furry frog nice cat 4 legs frog 4 legs From the items column I want to select unique objects that have both the green and 4 legs attribute. I would expect…
Jason Christa
  • 12,150
  • 14
  • 58
  • 85
6
votes
5 answers

Join between mapping (junction) table with specific cardinality

I have a simple question about the most efficient way to perform a particular join. Take these three tables, real names have been changed to protect the innocent: Table: animal animal_id name ... ====================== 1 bunny 2 …
brettw
  • 10,664
  • 2
  • 42
  • 59
6
votes
4 answers

MySQL find row through another table

I have two tables: game `id` INT(11) game_tags `game` INT(11) `tag_id` INT(11) game_tags.game = game.id I am horrible with MySQL, so here is my question: I want to be able to find what games have a certain amount of tag_id's. So if…
Matt
  • 1,151
  • 3
  • 13
  • 34
6
votes
2 answers

SQL Query similar to IN where clause with AND condition instead of OR

I am trying to optimize my SQL query, so that we will not have to process the response on our JVM. Consider we have following table with entries: +-----------+-------------+ | Column1 | Column2 | +-----------+-------------+ |val11 |val21 …
Krishna Kumar
  • 511
  • 1
  • 9
  • 21
6
votes
7 answers

SQL how to search a many to many relationship

I have a database with two main tables notes and labels. They have a many-to-many relationship (similar to how stackoverflow.com has questions with labels). What I am wondering is how can I search for a note using multiple labels using SQL? For…
Josh Moore
  • 13,338
  • 15
  • 58
  • 74
6
votes
3 answers

Borrowers that take all loans using NOT EXISTS

I want to find the borrowers who took all loan types. Schema: loan (number (PKEY), type, min_rating) borrower (cust (PKEY), no (PKEY)) Sample tables: number | type | min_rating ------------------------------ L1 | student | 500 L2 |…
maregor
  • 777
  • 9
  • 32
6
votes
4 answers

Find rows that have same value in one column and other values in another column?

I have a PostgreSQL database that stores users in a users table and conversations they take part in a conversation table. Since each user can take part in multiple conversations and each conversation can involve multiple users, I have a…
Jim
  • 13,430
  • 26
  • 104
  • 155
6
votes
2 answers

How can I find groups of records that match other groups of records (relational division?)

For setting consolidated account handling, I want to find out accounts that have "exactly the same" set of owners. I think it might work to pivot the owners with dynamic sql, then use ranking functions, but I don't want to pursue that approach; I…
Levin Magruder
  • 1,905
  • 18
  • 26
5
votes
1 answer

SQL: many-to-many relationship and the 'ALL' clause

I have a table products and a table locations which are linked together in a many-to-many relationship with a table products_locations. Now a client can select a set of products, and I want to run a query that selects only the locations, where ALL…
Kugelblitz
  • 582
  • 5
  • 24
5
votes
2 answers

Seeking extended Divide operator explanation

I am reading about Codd’s Eight Original Operators in Inside Microsoft SQL Server 2008: T-SQL Querying by Itzik Ben-Gan, Lubor Kollar, Dejan Sarka, and Steve Kass and do not understand the Divide operator. Quotes defining the Divide operator: “A…
Jesslyn
  • 696
  • 10
  • 25
5
votes
1 answer

SQL: Get Products from a category but also must be in another set of categories

I am currently stuck in a situation. The scenario is this. I have products who may be associated with multiple categories. The data structure is shown below: Products Table: product_id name 1 Lemon 2 Kiwis 3 …
5
votes
1 answer

MySQL ONLY IN() equivalent clause

I am giving a very abstract version of my question here, so please bear with me. I have a query that will check whether a particular body has certain multiple parameters of same type. Example, a boy has multiple selection as far as chocolates are…
Vijay Kumar Kanta
  • 1,111
  • 1
  • 15
  • 25
4
votes
1 answer

SQL use of conditional AND on a join table to ensure at least two or multiple values are matched

I have a join table named languages_services that basically joins the table services and languages. I need to find a service that is able to serve both ENGLISH (language_id=1) and ESPANOL (language_id=2). table…
chadwtaylor
  • 221
  • 2
  • 10
4
votes
4 answers

SQL Select all rows where subset exists

I'm sure there is an answer present for this question but bear with me as I'm new to SQL and am not sure how to ask the question. I have data like this (this is shorthand purely for example). This is in a postgres db. table1 id value 1 111 1…
MFD3000
  • 854
  • 1
  • 11
  • 26
1
2
3
31 32