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

Intersection of Records in Postgres

Suppose I have labels with multiple stores associated with them like so: label_id | store_id -------------------- label_1 | store_1 label_1 | store_2 label_1 | store_3 label_2 | store_2 label_2 | store_3 label_3 | store_1 label_3 |…
Student
  • 287
  • 4
  • 9
3
votes
3 answers

How to filter conditioned on multiple rows in sql

I'm looking for a way to filter over two row conditions. In general in basic sql it's possible to use the where and or statements to filter the query. What if i want to filter over two rows. I want to find every patient in following example, which…
mischva11
  • 2,811
  • 3
  • 18
  • 34
3
votes
3 answers

SQL: Find intersection in a double many-to-many relatiion

Following is a simplified version of my schema and data: users: id | name 1 | Peter 2 | Max 3 | Susan restaurants: id | name 1 | Mario 2 | Ali 3 | Alfonzo 4 | BurgerQueen dishes: id | name 1 | Burger 2 | Pizza 3 |…
3
votes
3 answers

SQL query to find a row with a specific number of associations

Using Postgres I have a schema that has conversations and conversationUsers. Each conversation has many conversationUsers. I want to be able to find the conversation that has the exactly specified number of conversationUsers. In other words,…
bento
  • 4,846
  • 8
  • 41
  • 59
3
votes
1 answer

Select rows with a specific column greater than zero in some related rows

I have the following tables in a Postgres 9.5 database: product_days Column | Type | Modifiers -----------+---------+---------------------------------------------- id | integer | not null default…
Siavosh
  • 2,314
  • 4
  • 26
  • 50
3
votes
1 answer

List name that have every items in MsSQL

TableA Name Items A 1 A 2 A 2 A 3 A 3 B 1 B 1 B 2 B 2 C 1 C 2 C 2 C 3 TableB Items ItemsName 1 One 2 Two 3 Three I want to list Name A C Because A and C have every items in TableB. And B doesn't…
Alan Chuang
  • 73
  • 1
  • 8
3
votes
3 answers

SQL - check if a value in a list does not exist in table and return boolean accordingly

I have a simple database schema composed of 3 tables User id name matricule Document id serial User_Document (join table) user_id document_id I want to check if all items of list( list of Document.serial) exists in the join…
ulquiorra
  • 931
  • 4
  • 19
  • 39
3
votes
2 answers

ALL operator in WHERE clause in Rails

The association is as shown below. InstructorStudent has_many :fees Fee belongs_to :instructor_student I want to get the instructor student who has monthly detail in all given array. If monthly details is not present in any one of them then it…
ashvin
  • 2,020
  • 1
  • 16
  • 33
3
votes
3 answers

In postgres, what is the best way to do an AND with a join table?

I have different categories of user, and a join table that allows users to be in more than one category. My join table is called categories_users and it consists of a user_id and a category_id. I want to filter for users that are in both…
wildrhombus
  • 113
  • 7
3
votes
3 answers

How to secure table for avoid duplicate data

I cant resolve the problem how secure my table to avoid duplicate combination of attributes_positions. The best way to show you what I mean is the following image column id_combination represents number of combination. Combination consists of…
3
votes
2 answers

Select rows with most similar set of attributes

I have a PostgreSQL 8.3.4 DB to keep information about photo taggings. First off, my table definitions: create table photos ( id integer , user_id integer , primary key (id, user_id) ); create table tags ( photo_id integer , user_id…
wannabe programmer
  • 653
  • 1
  • 9
  • 23
3
votes
2 answers

How to represent relational division(basic algebra expression) in terms of SQL

Query: Find names of sailors who have reserved all boats This can be represented in relation algebra as: 1. πsname ( ((σsid,bid Reserves) / (σbid Boats)) ⋈ Sailors) As per Relational algebra, Division can also be represented using basic algebra…
Ankita Rane
  • 171
  • 1
  • 1
  • 12
3
votes
3 answers

Division by 1 in programming languages

So what if we tell a programming language to calculate this: X/1, where X is any number. Do they actually calculate the output or check/ignore 1 and return X? Furthermore, when coding something like the above is it worth checking if the divider is…
czioutas
  • 1,032
  • 2
  • 11
  • 37
3
votes
3 answers

SQL query recipes with these ingredient(s) but NOT these ingredient(s)

I am doing an advanced search functionality for a recipes website where the users can query; Recipes with the presence of certain ingredients (up to 3 ingredients) Recipes without the presence of certain ingredients (up to 3 ingredients) Under…
forseth31
  • 83
  • 1
  • 7
3
votes
2 answers

Converting From NOT EXISTS to NOT IN

I have three tables: sailor (sname, rating); boat (bname, color, rating); reservation (sname, bname, weekday, start, finish); In order to get a list of sailors who have reserved every red boat, I have: select s.sname from sailor s where…