relational-division

Find rows that have same value in one column and other values in another column?

ぃ、小莉子 提交于 2019-12-04 07:10:41
I have a PostgreSQL database that stores users in a users table and conversations they take part in a conversation table. Since each user can take part in multiple conversations and each conversation can involve multiple users, I have a conversation_user linking table to track which users are participating in each conversation: # conversation_user id | conversation_id | user_id ----+------------------+-------- 1 | 1 | 32 2 | 1 | 3 3 | 2 | 32 4 | 2 | 3 5 | 2 | 4 In the above table, user 32 is having one conversation with just user 3 and another with both 3 and user 4. How would I write a query

MySQL ONLY IN() equivalent clause

可紊 提交于 2019-12-03 06:25:31
问题 I am giving a very abstract version of my question here, so please bear with me. I have a query that will check whether a particular body has certain multiple parameters of same type. Example, a boy has multiple selection as far as chocolates are concerned. But, I want to choose boys from the table who have exactly the chocolates I mention. Not more not less and not 'LIKE' or not 'IN()'. SELECT boy_id from boys_chocolates WHERE chocolate_id ONLY IN('$string'); ..where of course '$string' is a

SQL query through an intermediate table

≯℡__Kan透↙ 提交于 2019-12-03 04:22:58
问题 Given the following tables: Recipes | id | name | 1 | 'chocolate cream pie' | 2 | 'banana cream pie' | 3 | 'chocolate banana surprise' Ingredients | id | name | 1 | 'banana' | 2 | 'cream' | 3 | 'chocolate' RecipeIngredients | recipe_id | ingredient_id | 1 | 2 | 1 | 3 | 2 | 1 | 2 | 2 | 3 | 1 | 3 | 3 How do I construct a SQL query to find recipes where ingredients.name = 'chocolate' and ingredients.name = 'cream'? 回答1: This is called relational division. A variety of techniques are discussed

How to find all pizzerias that serve every pizza eaten by people over 30?

a 夏天 提交于 2019-12-02 22:48:40
I'm following the Stanford Database course and there's a question where we have Find all pizzerias that serve every pizza eaten by people over 30 using Relational Algebra only. The problem consist of a small database with four relations: Person(name, age, gender) // name is a key Frequents(name, pizzeria) // [name,pizzeria] is a key Eats(name, pizza) // [name,pizza] is a key Serves(pizzeria, pizza, price) // [pizzeria,pizza] is a key I know how to find which pizza's people over 30 eat and make a cross-product of them, so I could check which pizzeria has both. I can make a list of all the

Minimal number of groups necessary to cover user/product permissions

隐身守侯 提交于 2019-12-02 19:14:55
问题 I have a list of 365 customers. Each of them has a potentially unique list of products they are allowed to order, up to 18 out of 24 total products (at present). I would like to use groups to assign the permissions. How can I determine the minimum number of unique permission sets? I have a hunch the answer involves relational division, but I'm still very fuzzy on how that works. To clarify : I am not just interested in which users have exactly the same permissions. I want to find groups I can

SQL query through an intermediate table

南笙酒味 提交于 2019-12-02 17:38:23
Given the following tables: Recipes | id | name | 1 | 'chocolate cream pie' | 2 | 'banana cream pie' | 3 | 'chocolate banana surprise' Ingredients | id | name | 1 | 'banana' | 2 | 'cream' | 3 | 'chocolate' RecipeIngredients | recipe_id | ingredient_id | 1 | 2 | 1 | 3 | 2 | 1 | 2 | 2 | 3 | 1 | 3 | 3 How do I construct a SQL query to find recipes where ingredients.name = 'chocolate' and ingredients.name = 'cream'? This is called relational division. A variety of techniques are discussed here . One alternative not yet given is the double NOT EXISTS SELECT r.id, r.name FROM Recipes r WHERE NOT

Postgresql: Query returning incorrect data

允我心安 提交于 2019-12-02 17:19:36
问题 Suppose i have a table empgroupinfo and i want to fetch the employeeid who come in exact this two groupId 500 and 501 (will come dynamically) only, should not come in more or less number of group, where empid != 102 which is in 500 groupid. I have tried following query: select empid from empgroupinfo where empgroupid in(500,501) and empid != 102 group by empid having count(empid) = 2 But this above query also returns the empId that are in other groups. I want to fetch the empid for the case

Minimal number of groups necessary to cover user/product permissions

我只是一个虾纸丫 提交于 2019-12-02 08:53:10
I have a list of 365 customers. Each of them has a potentially unique list of products they are allowed to order, up to 18 out of 24 total products (at present). I would like to use groups to assign the permissions. How can I determine the minimum number of unique permission sets? I have a hunch the answer involves relational division, but I'm still very fuzzy on how that works. To clarify : I am not just interested in which users have exactly the same permissions. I want to find groups I can use, each user potentially a member of multiple groups, that can reproduce the permissions. For

Database Relational Algebra: How to find actors who have played in ALL movies produced by “Universal Studios”?

限于喜欢 提交于 2019-12-02 08:40:37
Given the following relational schemas, where the primary keys are in bold: movie( movieName , whenMade); actor( actorName , age); studio( studioName , location, movieName); actsIn( actorName , movieName ); How do you find the list of actors who have played in EVERY movie produced by "Universal Studios"? My attempt: π actorName ∩ (σ studioName=“Universal Studios” studio) |><| actsIn, where |><| is the natural join Are you supposed to use cartesian product and/or division? :\ Here are the two steps that you should follow: Write an expression to find the names of movies produced by “Universal

Postgresql: Query returning incorrect data

元气小坏坏 提交于 2019-12-02 08:15:32
Suppose i have a table empgroupinfo and i want to fetch the employeeid who come in exact this two groupId 500 and 501 (will come dynamically) only, should not come in more or less number of group, where empid != 102 which is in 500 groupid. I have tried following query: select empid from empgroupinfo where empgroupid in(500,501) and empid != 102 group by empid having count(empid) = 2 But this above query also returns the empId that are in other groups. I want to fetch the empid for the case when employees are in exactly these two groupids (500 and 501) only and empid != 102 . Your WHERE clause