What is the use of WITH TIES keyword in SELECT statement in SQL Queries?

前端 未结 7 1574
醉梦人生
醉梦人生 2021-01-30 20:15
SELECT TOP 5 WITH TIES EmpNumber,EmpName 
FROM Employee 
Order By EmpNumber DESC

This above query return more than five result, What is the use of \"Wi

7条回答
  •  半阙折子戏
    2021-01-30 20:32

    The WITH TIES allows you to return more rows with values that match the last row in the limited result set. Note that WITH TIES may cause more rows to be returned than you specify in the expression.

    The selection of which the rows to return is nondeterministic.
    This means that if you run the query again, without the underlying data changing, theoretically you could get a different set of three rows.
    In practice, the row selection will depend on physical conditions like :

    • optimization choices
    • storage engine choices
    • data layout
    • etc...

    If you actually run the query multiple times, as long as those physical conditions don’t change, there’s some likelihood you will keep getting the same results. But it is critical to understand the “physical data independence” principle from the relational model, and remember that at the logical level you do not have a guarantee for repeatable results. Without ordering specification, you should consider the order as being arbitrary, resulting in a nondeterministic row selection.

    Your current result is like below :

    EmpNumber EmpName  Ranking
    11        Maria    1
    23        José     2
    456       Pedro    3
    456       Pedro    3 --WITH TIES
    

    Probably your table is containing duplicate rows or may have historical ones as in general the EmpNumber is unique.

    From Reference

提交回复
热议问题