Search code examples
sqlansi-sql

SQL query for the number of cases when a value of column1 (non-unique) can't be found within any record where column2 meets a basic criteria


I am doing a beginners' SQL tutorial and I started to wonder whether a simple SQL query on this table: http://www.sqlcourse2.com/items_ordered.html could tell the number of items (also 1) which have only been purchased more items at a time, so there is no record which contains the quantity column with a value of 1 AND the item. I am really beginner at this so please try to keep it simple.

Thank you in advance!


Solution

  • Welcome to the fascinating world of SQL.

    Well - I'm not giving you the answer, but a hint (after all, it's a training and your own thinking and finding the solution would be the best way for you to learn something new).

    The way you formulate your question is somewhat puzzling.

    When I combine what you ask with what is possible with SQL, the question that would make sense to me would be that you need to list (or count, I did not understand that very well) the items (or the complete rows in the table with matching item, that was not clear either), that were never sold with a quantity of 1.

    If that's what you need, you will need a subselect to get all distinct items that were sold with a quantity of 1, and select the rows from your base table whose item value is not in the list you get from the subselect.

    Do you need more hints?

    Marco