How to select id if element contains all data in a list in SQL?

user1842633

I have a DB with the following schema:

StoreID | BookName
------------------
   1      Harry Potter
   1      Lord of the Rings
   1      Hobbit
   2      Hobbit
   2      Hunger Games
   3      American Gods
   3      Redwall
   4      Calvin and Hobbes

I want get all the store IDs that contain all the books mentioned in the list ['Lord of the Rings', 'Hobbit']

Is there an operator in SQL that can do exactly that?

I know that the IN operator in SQL can obtain all the storeId's that contain at least one of the books in the list. I believe the script would look something like this:

Select StoreID
FROM BookStores
WHERE BookName IN ['Lord of the Rings', 'Hobbit']

Is there a script that can do something similar except only return storeId's if they contain all of the books mentioned in the list? (In this case, the only id returned would be 1)

GMB

You can use aggregation:

Select StoreID
FROM BookStores
WHERE BookName IN ('Lord of the Rings', 'Hobbit')
GROUP BY StoreID
HAVING COUNT(*) = 2

This assumes that the same book does not appear twice in the same store. If that many happen, then you can change the HAVING clause to:

HAVING COUNT(DISTINCT BookName) = 2

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to find if a list contains any element of another list in sqlite

From Dev

Check if element is in the list (contains)

From Dev

How to select element that contains only the class of "foobar", don't select if it contains any other classes?

From Dev

How to get a list which contains at least all the values of another list?

From Dev

SQL Server XQuery to select record if XML Column contains element with value

From Dev

How can I select an element that contains " " within its HTML?

From Dev

How to select an element directly after an element with id

From Dev

How to select element in Protractor by html attribute when value contains

From Dev

How to get all select element in JQuery

From Dev

Select all but last list-element

From Dev

How to select all data in PyMongo?

From Dev

Delete all previous lists if a following list contains same element

From Dev

Select all the last dates from all list of id

From Dev

Select All from Collection Where ID in List

From Dev

How to select all the records even if there is no data in sql

From Dev

How to get a list which contains at least all the values of another list?

From Dev

Select all but last list-element

From Dev

Select element if contains text

From Dev

Select distinct column A where column B contains all required data?

From Dev

How to get the count of all the web element in a web page that contains "ID" attribute?

From Dev

MySQL: select all the data but if contains ' * ', then show the last four characters

From Dev

SQL Server - Multiple Transaction ID's - If contains - Only select Latest

From Dev

How to check if a list already contains an element in Python?

From Dev

Xpath - How to select an element that contains a text but nothing more

From Dev

How to select every element in an element list

From Dev

How to get a list contains each element in a tensor?

From Dev

Select all data of sql table + AS

From Dev

SQL Query - how to select count all value from table and displaying all counted and its id?

From Dev

SQL How to select data that has all values above a value

Related Related

  1. 1

    How to find if a list contains any element of another list in sqlite

  2. 2

    Check if element is in the list (contains)

  3. 3

    How to select element that contains only the class of "foobar", don't select if it contains any other classes?

  4. 4

    How to get a list which contains at least all the values of another list?

  5. 5

    SQL Server XQuery to select record if XML Column contains element with value

  6. 6

    How can I select an element that contains " " within its HTML?

  7. 7

    How to select an element directly after an element with id

  8. 8

    How to select element in Protractor by html attribute when value contains

  9. 9

    How to get all select element in JQuery

  10. 10

    Select all but last list-element

  11. 11

    How to select all data in PyMongo?

  12. 12

    Delete all previous lists if a following list contains same element

  13. 13

    Select all the last dates from all list of id

  14. 14

    Select All from Collection Where ID in List

  15. 15

    How to select all the records even if there is no data in sql

  16. 16

    How to get a list which contains at least all the values of another list?

  17. 17

    Select all but last list-element

  18. 18

    Select element if contains text

  19. 19

    Select distinct column A where column B contains all required data?

  20. 20

    How to get the count of all the web element in a web page that contains "ID" attribute?

  21. 21

    MySQL: select all the data but if contains ' * ', then show the last four characters

  22. 22

    SQL Server - Multiple Transaction ID's - If contains - Only select Latest

  23. 23

    How to check if a list already contains an element in Python?

  24. 24

    Xpath - How to select an element that contains a text but nothing more

  25. 25

    How to select every element in an element list

  26. 26

    How to get a list contains each element in a tensor?

  27. 27

    Select all data of sql table + AS

  28. 28

    SQL Query - how to select count all value from table and displaying all counted and its id?

  29. 29

    SQL How to select data that has all values above a value

HotTag

Archive