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

Efficient way to get all articles with a set of tags in MySQL

I need an efficient way to select all articles with at least tags "Tag1" and "Tag2". This is the standart way with the following database schema: articles(id, title) article_tag(articleid, tagid) tag(id, name) SELECT a.* FROM article a INNER…
evodevo
  • 479
  • 1
  • 7
  • 14
2
votes
4 answers

SQL Query to select pairs of members only if they share the exact values

I have the following table members: ID hobby 1 Football 1 Tennis 1 Football 2 Cards 2 Painting 3 Tennis 3 Football 4 Cards and i want to select pairs of members only if they have the exact same hobbies (without…
IJokl
  • 56
  • 6
2
votes
2 answers

Select all Things in a given list of Categories (or more Categories)

Imagine a simple two column table, that has thing_id and category_id. Yep, it's used as a many to many connector between Things and Categories, but matters not, all that is relevant is that we have this table ThingCategory with these two columns. I…
Bernd Wechner
  • 1,854
  • 1
  • 15
  • 32
2
votes
2 answers

Left outer join involving three tables

From the 3 tables below I need to find 'John', who has a bike but not a car. I'm trying to use this syntax Select <> from TableA A left join TableB B on A.Key = B.Key where B.Key IS null so in practise I create a left join from the two tables but…
siwmas
  • 389
  • 2
  • 15
2
votes
1 answer

SQL Get Records based on multiple row value

I am trying to create a SQL report that can do logic based on a boolean that exists in multiple rows. I created a bunch of fake data below, and my attempts. IMPORT NOTE: I am writing this using SSMS but plan to run it in MySql. Ran into some issues…
Travis
  • 657
  • 6
  • 24
2
votes
1 answer

query for many to many record matching

I have table tag_store like below I want to filter the ids which has all tag provided in a like SELECT st.id from public."tag_store" st inner join (SELECT x.tg_type,x.tg_value FROM json_to_recordset …
Md. Parvez Alam
  • 4,326
  • 5
  • 48
  • 108
2
votes
1 answer

Is this natural join operation used correctly? (Relational Algebra)

I have the following task given from the professor:R-E Modell Assume the companies may be located in several cities. Find all companies located in every city in which “Small Bank Corporation” is located. Now the professor's solution is the…
2
votes
2 answers

How to conditionally select rows based on other rows

I've got two tables with a simple 1:n relation. One table contains ingredients and their availability. The other contains recipes and their respective ingredients. Table recipes RecipeA | IngredientA RecipeA | IngredientB RecipeB |…
Nikno
  • 119
  • 1
  • 8
2
votes
2 answers

Finding objects only where all of their has_many collections have a value for a specific column

I have an Album object that has many Tracks. Tracks have a youtube_uid column. I'd like to query albums where all of their tracks' youtube_uids are present. I know the technique to find albums with tracks where there's at least one track with a…
Carl Edwards
  • 13,826
  • 11
  • 57
  • 119
2
votes
3 answers

How to select items which belong to one group but not another

Sqlite3 How do I select animals which are only pets and not food? POF is "pet or food" column. An animal can belong to both groups. This is a smaller version of the real problem. I don't want to split this into more tables. animal …
Sonicsmooth
  • 2,673
  • 2
  • 22
  • 35
2
votes
2 answers

How to find equal subsets?

I have a table with subsets. How to find reader id's with the same subsets as given id? For example: Input reader = 4 The expected output: reader 1 and 5. Subsets size is not always = 3 as in the example it can be dynamic. What is correct SQL…
ZedZip
  • 5,794
  • 15
  • 66
  • 119
2
votes
2 answers

Query to find students that failed all given subjects

I'm trying to find the students that have failed every subject in a set of subjects via PostgreSQL queries. Students fail a subject if they have a not null mark < 50 for at least one course offering of the subject. And I want to find the students…
Casper Lindberg
  • 1,053
  • 1
  • 11
  • 16
2
votes
3 answers

How to fetch records from a table that matches with all values in the other table

How to write a query that fetches all the drivers who drive all the available buses? TABLE BUS -------- BUS_ID -------- 1 2 3 4 TABLE DRIVER ----------------------- BUS_ID | DRIVER_NAME ----------------------- 1 | John 2 | John 1 | …
Jalal
  • 37
  • 5
2
votes
2 answers

Oracle SQL Is there a way to find intersections of data sets stored in same table when number of data sets are dynamic

Imagine the following data set. (This is not the exact same problem I am working on, but this is just a simple example that demonstrate the requirement) Student ID, Course ID S1 C1 S1 C2 S1 C3 S2 C1 S2 …
Rakhitha
  • 328
  • 2
  • 11
2
votes
4 answers

Join query with only columns that have all values in `in` clause

I'm creating a simple filtering system for my website. I have a many to many relationship between venues and amenities. Here are my tables. NOTE: all ids are uuids. Making them short for simplicity venues: | id | name …