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

mysql select multitable - join

say i had the following tables user_table id username 1 abc 2 def 3 ghij courses_table id title 1 csc 2 math 3 syn user_courses user_id course_id 2 1 1 3 2 3 i want to select the…
aadiahg
  • 119
  • 1
  • 1
  • 9
2
votes
1 answer

Return field in SQL that intersects on a second field?

I am trying to return the fields that have intersecting fields for a specific person. What I mean by this is Name Friend ---- ----- Joe Sally Joe Bill Mary Sally Mary Michael Mike Joe Bill Bill Bill Sally Gil …
user1490083
  • 361
  • 2
  • 7
  • 21
2
votes
7 answers

IN for many elements

I have tables: Table Site ╔════╦═══════════════╗ ║ ID ║ NAME ║ ╠════╬═══════════════╣ ║ 1 ║ stackoverflow ║ ║ 2 ║ google.com ║ ║ 3 ║ yahoo.com ║ ║ 4 ║ cnn.com ║ ╚════╩═══════════════╝ Table Widget ╔════╦════════════╗ ║ ID…
Al Steche
  • 23
  • 2
2
votes
2 answers

MySQL get all related IDs from a table matching an array

I'm facing a problem while trying to retrieve all productIDs from a table if they match all items in an array, in this case, return products only if they contain every ingredient the user searched for. Table looks like this ID produktID …
RemizZ
  • 57
  • 2
  • 7
2
votes
1 answer

Transpose table with "list" of the same attributes

Is it possible to transpose a table with repeated columns? Existing table: user_id question_id body 1 1 'Text1 1' 1 1 'Text1 1-2' 1 2 'Text1 2' 1 3 'Text1 3' 2…
2
votes
2 answers

Simple SQL Select with all matches

SQL question: I have table with two columns: ProductId and CatId I want to select products that belong to all of the passed in CatIds. For example this is my…
M. Ali Iftikhar
  • 3,125
  • 2
  • 26
  • 36
2
votes
1 answer

SQL query on showing a grouped rows that contains all values from a list

SQL QUERIES TABLE - CUSTOMER 1001,1 1001,2 1001,3 1002,1 1002,3 1003,3 TABLE - PRODUCT 1 2 3 The result should be 1001 coz it got all the values match the order table. The PRODUCT table might change over time. I want to find out who have bought…
Yin Cheong
  • 33
  • 4
2
votes
4 answers

SQL to return a merged set of results

I have the following SQL: SELECT `table1`.`value`, `table2`.* FROM `table2` INNER JOIN `table1` ON `table2`.`product_id` = `table1`.`entity_id` WHERE `table2`.`created_at` > '2012-04-23' and (`table1`.`value` = 264 OR `table1`.`value` =…
Greg Demetrick
  • 759
  • 1
  • 12
  • 28
2
votes
3 answers

SQL, only if matching all foreign key values to return the record?

I have two tables Table A type_uid, allowed_type_uid 9,1 9,2 9,4 1,1 1,2 24,1 25,3 Table B type_uid 1 2 From table A I need to return 9 1 Using a WHERE IN clause I can return 9 1 24
Jules
  • 7,568
  • 14
  • 102
  • 186
1
vote
3 answers

Using SQL IN AND together

Is there a way to use IN and AND together? The problem is that I want to match my query to multiple values using one column. SELECT * FROM product INNER JOIN values USING(product_id) WHERE value = "large" AND value = "short" The problem is I can't…
stixx
  • 142
  • 1
  • 8
1
vote
3 answers

SQL queries involving ' for all'

I could not get a hint on how to write SQL queries for A and B for the following schema. Programme (Pid:int, Department:string...) Employee (Eid:int, Department:string..) Participation (Pid:int, Eid:int, ..) A. Names of programmes participated by…
Nemo
  • 4,601
  • 11
  • 43
  • 46
1
vote
4 answers

Can all SQL queries be represented in Relational Algebra, Domain and Tuple relational calculus

My query includes a having and count or all in. How are these represented in RA/DRC/TRC? Would I have to simplify my SQL query even more? Here is a simplified example: empl(employee (primary key), city) managers(employee (primary key), manager…
1
vote
2 answers

SQL to find a common element in 2 lists

I run into this situation from time to time, where I want to take a list of things and see if any of them exist in another list. If you have a single number, it's simple enough to use a where clause to say something like: where topic_id IN (select…
Cmaso
  • 1,095
  • 1
  • 10
  • 23
1
vote
2 answers

Retrieve rows that are fully compatible with each other in an SQL database

I have this simple structure in my database CREATE TABLE species ( _id INTEGER PRIMARY KEY, name TEXT NOT NULL ); CREATE TABLE compatibility ( _id INTEGER PRIMARY KEY, speciesA INTEGER, speciesB INTEGER, compatibility TINYINT NOT…
Nathan Lo Sabe
  • 197
  • 1
  • 13
1
vote
2 answers

Find all records with same join column data

I have these…
user09
  • 920
  • 2
  • 12
  • 38