Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do these queries return different results?

Tags:

sql

mysql

SELECT DISTINCT productName, listPrice 
FROM product 
WHERE productID IN (SELECT productID FROM saleItem WHERE quantity = 2)
  AND productID NOT IN (SELECT productID FROM saleItem WHERE quantity <> 2)

Hey everyone, I'm querying a database for a class to recap the lessons from the semester and am a bit confused. The query above returns a different number of results than the query below. It's an entry level course and I still can't figure out how quantity = 2 differs from NOT in (SELECT productID FROM saleItem WHERE quantity <> 2). The way I am reading the syntax seems they should only return a quantity of 2. If anyone could help, It would be hugely appreciated! If this a poorly formatted question I apologize.

SELECT DISTINCT productName, listPrice 
FROM product 
WHERE productID IN (SELECT productID FROM saleItem WHERE quantity = 2)
like image 596
Abe Avatar asked Dec 02 '17 16:12

Abe


People also ask

Why does the same query take different times?

Either you are getting different performance because the system is under a different sort of load, you are getting different performance because of data volume changes, or you are getting different performance because you are getting different query plans.

Why some queries return the same rows more than once?

The cause of the error is a subquery returning more than one row of information. This error in multi-row returns on the subquery originates from an outer query failing to use appropriate, designated keywords to specify values for comparison in the subquery.

What is queries how they are different from views?

A view can be described as a virtual table, created from a SQL query stored in the database. Performance: it can improve data access performance as queries involving several tables generate transactions already exchanged to generate the view.

What do you think are the reasons why there is a slow query result?

Slow queries are frequently caused by combining two or more large tables together using a JOIN. Review the number of joins in your query, and determine if the query is pulling more information than is actually needed.


3 Answers

Consider product IDs that appear in multiple rows of saleItem:

  • Some rows would have quantity of 2
  • Some other rows would have quantity different from 2

If you have two saleItem rows for the same productId, and at least one of these rows has quantity not equal to 2, the row corresponding to that productId would be excluded from the first query.

For example, if your saleItem looks like this

saleItemId ProductId Quantity
---------- --------- --------
       100         1        2
       101         2        3
       102         2        2

then the first query would exclude productId=2, while the second query would include it.

like image 55
Sergey Kalinichenko Avatar answered Oct 14 '22 16:10

Sergey Kalinichenko


I still can't figure out how quantity = 2 differs from NOT in (SELECT productID FROM saleItem WHERE quantity <>2)

Probably NULL values, you could simple check it with:

SELECT distinct productName, listPrice 
FROM product 
WHERE productID NOT IN (SELECT COALESCE(productID,-1) 
                        FROM saleItem 
                        WHERE quantity <> 2) --assuming that quantity is NOT NULL
like image 21
Lukasz Szozda Avatar answered Oct 14 '22 16:10

Lukasz Szozda


Let's assume your tables with following sample data:

Products:

productID | productName | listprice
-------------------------------------
   1      |      A      |   100
-------------------------------------
   2      |      B      |   200
-------------------------------------
   3      |      C      |   300
-------------------------------------
   4      |      D      |   400

saleItem:

productId | quantity
----------------------
    1     |   2
----------------------
    2     |   2
----------------------
    3     |   5
----------------------
    4     |   Null
----------------------
    1     |    3

Note: I have assumed here that quantity field doesn't have Not Null constraint.

Query :

       SELECT  productID FROM saleItem WHERE quantity =2

Result:

   productID
  -----------
       1
  -----------
       2
  -----------
       1

Query :

       SELECT distinct productID FROM saleItem WHERE quantity =2

Result:

   productID
  -----------
       1
  -----------
       2

Query:

SELECT  productID FROM saleItem WHERE quantity <> 2 // <> operator won't include rows with quantity having NULL values 

Result:

   productID
  ------------
       1
  -----------
       3
  -----------
       1    

Query:

SELECT  distinct productID FROM saleItem WHERE quantity <> 2 

Result:

   productID
  ------------
       1
  -----------
       3

Hence, Query 1 can be converted in:

SELECT distinct productName, listPrice 
FROM product 
WHERE productID IN (1,2,1) 
AND productID NOT in (1,3,1);

Results:

    productID | productName | listprice
    -------------------------------------
       2      |      B      |   200

So, Query 1 is showing results of productID which only has one value for quantity and that is 2.

Query 2:

SELECT distinct productName, listPrice 
FROM product 
WHERE productID IN (1,2,1);

Results:

    productID | productName | listprice
    -------------------------------------
       1      |      A      |   100
    -------------------------------------
       2      |      B      |   200

So, Query2 will return info for those productID who has 2 as quantity but may or may not have other values quantity as well.

Last Note:

1) quantity <> 2 won't include rows with NULL quantity. But Not IN ( quantity <> 2) will include rows that has value for quantity other than 2 including NULL.

Hope it helps:

like image 36
Harshil Doshi Avatar answered Oct 14 '22 15:10

Harshil Doshi