Search code examples
sqlms-accessms-access-2016

what is the use of AND with ORDER BY?


What does the AND do in a query like so

SELECT *
FROM table
WHERE SaleNo > 200,
ORDER BY EmployeeNo AND SaleNo;

How is it different from

SELECT *
FROM table
WHERE SaleNo > 200,
ORDER BY EmployeeNo, SaleNo;

Both queries actually return a table back but I don't understand the meaning of 1st query. ORDER BY is usually used along with pipes and commas for additional filterings. TIA


Solution

  • ORDER BY EmployeeNo AND SaleNo;
    

    The above becomes this:

    ORDER BY (EmployeeNo AND SaleNo)
    

    And then becomes:

    ORDER BY (true/false AND true/false)
    

    And then

    ORDER BY (true/false)
    

    So the expression is converted to a true/false expression.

    If the 2 columns are number types, then 0 values = false, and any NON zero value = true

    so, you have

      ORDER BY ( (EmployeeNo <> 0) AND (SaleNo <> 0) )
    

    So, if both EmplyeeNo and SaleNo have a value?

    You get this

      ORDER BY ( (true) AND (True))
    
      ORDER BY True
    

    The value is thus -1.

    What this means is that if both values have a number, (not 0), then they will appear first in the list and all others if one of the two numbers are 0 or both are 0 will follow.

    So, to order by a column, and then by a anohter, you use

    col1, col2  - as you have.
    

    if you go

    col1 AND col2
    

    Then this is Boolean (true/false) expression, and not a order by one column, and then on to the next.