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

Relational Algebra translation of division

I came across this journal: http://users.dcc.uchile.cl/~cgutierr/cursos/BD/divisionSQL.pdf that gave a method of translating an relational algebra division into sql. I am a little skeptical if this actually works, Given T1 (A,B) and T2(B) SELECT A…
user
  • 854
  • 2
  • 12
  • 28
2
votes
5 answers

Multiple row conditions in WHERE clause

I am having trouble writing the following query in MySQL. I have a table called pizz0r_pizza_ingredients which looks something like this: | id | pizza_id | ingredient | amount | measure | +----+----------+------------+--------+---------+ | 6 | 1…
2
votes
4 answers

Making simple SQL more efficient

SQL Fiddle. I'm having a slow start to the morning. I thought there was a more efficient way to make the following query using a join, instead of two independent selects -- am I wrong? Keep in mind that I've simplified/reduced my query into this…
vol7ron
  • 40,809
  • 21
  • 119
  • 172
2
votes
4 answers

SQL: find records having data for each month in a given date range

Consider the following tables =# \d users Column | Type --------+----------------------- id | integer name | character varying(32) =# \d profiles Column | Type ---------+--------- id | integer user_id | integer =# \d…
Alex
  • 1,618
  • 1
  • 17
  • 25
2
votes
2 answers

SQL Relational Division with additional criteria

I have two tables. A sales table containing invoice numbers, part numbers, and quantities and a Bundles table, Containing a bundle id, part numbers and quantities. E.g: Sales: Invoice_No | Part_No | QTY ----------------------------- 1 |aaa …
Ben Baker
  • 23
  • 3
2
votes
4 answers

SQL query records containing all items in list

I have the following tables: MOVIES MOVIE_ID TITLE ---------- ----------------------------- 1 The Shawshank Redemption 2 The Godfather 3 The Godfather: Part II 4 The Dark Knight 5 Pulp Fiction …
Sinista
  • 437
  • 2
  • 6
  • 12
2
votes
1 answer

SQL query over parents with exact same set of children

I have parent-child relation in my table (simplified version listed below): | parent | child | |--------+-------| | p1 | c1 | | p1 | c2 | |--------+-------| | p2 | c1 | | p2 | c2 | |--------+-------| | p3 | c1 …
2
votes
3 answers

SQL: Only output the color/colors of balls that are in every box

I'm trying to solve this SQL problem, I have a dozen different colored balls and a few boxes. Now, I'm trying to write an SQL query that outputs the color of the balls that are in every box. So, if Green balls exist in Box A, B and C, but Blue and…
user3124096
  • 21
  • 1
  • 2
2
votes
2 answers

MYSQL: Find rows where multiple ID match

I have a table setup similarly as below. genre_id series_id 1 4 1 2 2 5 4 1 2 4 3 3 What I want to do is to be able to find all series based on the mix of genres selected. For example finding all…
RWW
  • 49
  • 2
  • 6
2
votes
1 answer

MySQL Relational Division

I am having difficulties to solve one exercise: For which People there is a Restaurant, that serves ALL their favorite beers. (Yes, we actually have this in school :D) I have got 2 Tables that can be used: Table1: Favoritebeer (Name, Surname,…
Chris
  • 3,581
  • 8
  • 30
  • 51
2
votes
1 answer

PostgreSQL conditional conjunction and disjunction in one query

How to create a query which selects products of given features where feature statement is formed by "and" or "or" condition depending on a group they belong to? Description of the situation There is a store with products. Products may have features…
rafis
  • 233
  • 2
  • 4
  • 10
2
votes
5 answers

Find pair of students who take exactly the same classes

I have to find a pair of students who take exactly the same classes from table that has studentID and courseID. studentID | courseID 1 1 1 2 1 3 2 1 3 1 3 2 3 …
2
votes
1 answer

Using INTERSECT with tables from a WITH clause

What is wrong with this query: WITH volcan AS (SELECT DISTINCT v.numturista FROM viaje v, sitio s WHERE v.numsitio = s.numsitio AND s.tipo = 'Volcan'), desierto AS (SELECT DISTINCT v.numturista …
2
votes
2 answers

How to solve this relational division query?

I have a table (movies) with two foreign keys, (movie_id, genre_id). Of course, both fields aren't unique. How do I select the movies that have two or more specific genres? I want to do something like: SELECT movie_id FROM movies WHERE genre_id = 1…
Fosfor
  • 331
  • 2
  • 3
  • 15
2
votes
2 answers

SQL query (in SQL, relational algebra and tuple relational calculus)

Im doing a test exam where I've gotten stuck on one particular query, in both its SQL code, relational algebra and tuple relational calculus. The query states: Find the (city,state) pairs which house a branch of every type which is listed in the…