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
2
votes
3 answers

Relational Division - Mimicking 'ONLY IN'

I am trying to write a query in Oracle SQL that takes two parameters and finds throughout the table all of the instances where only either or occur, regardless of how many of them there are within the search. Here's an example of what I'm looking…
Cody
  • 49
  • 9
2
votes
5 answers

Multiple conditions in INNER JOIN

I have two tables user and product which have a one-to-many relationship (one usermay have multiple products). I want to create a query to get all users which have both an orange and a banana. In my example below this would be john and leeroy. How…
Hedge
  • 16,142
  • 42
  • 141
  • 246
2
votes
1 answer

Understanding Division in Relational Algebra

I have a bit of trouble understanding this slide regarding division in Relational Algebra. I did some research and was referred to by many people to On Making Relational Algebra Comprehensible by Lester I McCann. I'm having trouble on understanding…
Maggie Liu
  • 344
  • 1
  • 3
  • 15
2
votes
2 answers

Group key-value columns into a rows per group with column per key

I need to show groups of key/value pairs with n keys stored in a MS SQL Database in a table with one record per group and one column per key. I found that example but in my case the keys (finally the columns) are not fix and must come from the…
2
votes
2 answers

How to filter results using relational division in a dynamic search query?

Using following query i'm going to filter results based on selected tags or categories: DECLARE @categories NVARCHAR(MAX), @tags NVARCHAR(MAX); SELECT @categories = '1,2,4', -- comma separated category ids @tags = '2,3' --…
dNitro
  • 5,145
  • 2
  • 20
  • 45
2
votes
2 answers

SQL Server query with intersect except or union relational Algebra

I am trying to solve a problem. It seems that of a brain teaser if you ask me. Given two tables, return only values from the first table when there is a match for EVERY record in a second table. So a record in table 1 must have a match to every…
StevenG
  • 33
  • 3
2
votes
1 answer

MySQL get pairs of rows from same set of foreign table

My issue is the next one. I have 3 tables: People, Cars and Driven: People: Id Name 1 | Tom 2 | James 3 | Charles 4 | Eric 5 | Thomas 6 | Robert 7 | Kim 8 | Ellias Cars: Id Name 1 | Ford 2 | Nissan 3…
2
votes
2 answers

Return all the rows for id's that have all three seq number present

I have a table which looks something like this: table1 +----+-----+------+ | id | seq | test | +----+-----+------+ | 1 | 1 | HR | | 1 | 2 | RR | | 2 | 1 | HR | | 2 | 2 | RR | | 2 | 3 | OXY | | 3 | 1 | HR | | 3 | 2 |…
2
votes
1 answer

How to find items with multiple tags on them?

Problem: I have a table named item_tag_assn which maps items with tags (many-to-many association table). I need to find out items which have a set of tags applied to them. For example, if my table has following data: item_id | tag_id…
John Doe
  • 49
  • 5
2
votes
1 answer

Find the id of students who take every course, in mysql

I have 3 tables: Student (Id, Name, Country) Course (CrsCode, CrsName, Type, Instructor) Results(Id, CrsCode, Grade) I have to solve below q's by using SQL query. Id and CrsCode are key fields in every table. The Type field specifies the course…
user5447339
2
votes
5 answers

Complicated SQL Query--finding items matching multiple different foreign keys

So imagine that you have a table of Products (ID int, Name nvarchar(200)), and two other tables, ProductsCategories (ProductID int, CategoryID int) and InvoiceProducts (InvoiceID int, ProductID int). I need to write a query to produce a set of…
Andy Edinborough
  • 4,367
  • 1
  • 29
  • 28
2
votes
3 answers

EXCEPT command - Find sailors who’ve reserved all boats

I am reading a textbook and I do not understand this query: Find sailors who’ve reserved all boats. We have 3 tables: Sailors: sid, sname, rating, age (primary: sid) Boats: bid (primary: bid) Reserves: sid, bid, day (primary, sid, bid, day)…
NNguyen
  • 113
  • 1
  • 6
2
votes
5 answers

SQL Retrieve rows that have all rows from other table

I am facing some issues retrieving data using SQL in one specific scenario: Suppose I have the following tables: A (id, attr_a, attr_b); B (id, attr_d, attr_e); C (id_a, id_b); As you can see, table C have FK referencing id from table A, and…
Rogger Fernandes
  • 805
  • 4
  • 14
  • 28
2
votes
2 answers

Postgresql: the best way to check if column is equal to multiple values simultaneously

If I want to check if a column is equal to one or other value, I write: where col1 in (1, 4, 9); But if I want to check if a column is equal to those values simultaneously, I should write: where col1 = 1 and col1 = 4 and col1 = 9; Is there in SQL…
Stepan Pavlov
  • 119
  • 2
  • 3
  • 7
2
votes
1 answer

SQL Query - Single column value in two different columns

Let's say I have the following two tables Table 1 Column1 Column2 Column3 1 A ABC 1 B DEF 1 C DEF 1 D GHI 1 E GHI 2 A1 ABC 2 B1 DEF 2 C1 …
aakashgupta.0205
  • 647
  • 1
  • 8
  • 23