SQLforGeeks
  • Home
  • SQL
  • SQL Tutorial
  • SQL Syntax
  • Our Services
Contact
  1. SQL
  2. SQL WITH TIES

Text copied!

« Previous
Next »

SQL WITH TIES

July 8, 2023, 9:37 p.m. under SQL

  • Hi! You should check SQL CTE post first.

    In SQL, the WITH TIES is used in conjunction with the ORDER BY clause. When you use WITH TIES, it means you want to get more results than you asked for if there are any "ties" with the last result.

    For example : if you ask for the top 5 salaries and two people have the same fifth-highest salary, WITH TIES will include those two people in your result, giving you 6 rows.

    Here's the syntax for using WITH TIES :
    SELECT TOP n WITH TIES column1, column2, column3 ...
    FROM table_name
    ORDER BY column_name DESC;
            

    • n : It is the number of rows you want to retrieve. i.e. 5 rows

    • column1, column2, column3, ... : These are the columns you want to retrieve.

    • table_name : It is the name of the table you're querying.

    • column_name : It is the column by which you're ordering the result set.

    Here's an example of how to use WITH TIES in a SQL query :

    1. Let assume you have a table called Employees with columns like Employee_id, Employee_Name, Gender, Salary, and Joining_Date.

    SQL WITH TIES

    2. If you've noticed, we have three employees earning the same salary of 4000, which is the highest in our records. Let's retrieve the top 2 employees with the highest salary using the TOP clause along with WITH TIES.

    3. Run below SQL statement :

    SELECT 
    	TOP 2 WITH TIES
    	Employee_id, Employee_Name, Gender, Salary, Joining_Date
    FROM [Employees]
    ORDER BY Salary DESC;
            
    SQL WITH TIES

    4.The previous query brought back 3 rows instead of just the top 2 because we included the WITH TIES keywords with the TOP clause. WITH TIES includes additional rows when multiple rows share the same value. In our case, multiple employees share the same salary of 4000, resulting in a tie.

    5. Now, let's execute it without using WITH TIES and see what output we receive. Run below SQL statement :

    SELECT 
    	TOP 2 
    	Employee_id, Employee_Name, Gender, Salary, Joining_Date
    FROM [Employees]
    ORDER BY Salary DESC;
            
    SQL WITH TIES

    6. You can confirm that when we query without the WITH TIES, we receive only 2 rows.

    Hence, you have successfully demonstrated the use of the WITH TIES. Thank you.

    Frequently Asked Questions :

    What is SQL with ties?

    SQL with ties refers to retrieving additional rows that share the same value as the last row in an ordered result set.

    What is top 10 with ties in SQL?

    Top 10 with ties in SQL retrieves the top 10 rows from a result set, including additional rows if they share the same value as the 10th row.

    What does select top 1 with ties mean?

    Select top 1 with ties means retrieving the top row from a result set, including additional rows if they share the same value as the top row.

    What is percentage with ties in SQL?

    Percentage with ties in SQL involves retrieving a specified percentage of rows from a result set, including additional rows if they share the same value as the last row in the specified percentage.
    Thank You! You should check Define Transaction in DBMS post next.
    « Previous
    Next »
    RELATED :

    What is SQL

    What is T-SQL

    Difference between DBMS and Data Warehouse

    Download SQL Server

    Install SQL Server

    Download SQL Server Management Studio SSMS

    SQL Server Management Studio

    SQL Database

    Download database

    Restore database

    Backup database

    Attach database

    Detach database

    Create database

    Delete database

    Rename database

    Select database

    Database offline

    Database online

    SQL Commands

    SQL Tables

    Create table

    Truncate table

    Delete table

    Rename table

    Select table

    Alter table

    SQL Data Types

    SQL Comments

    SQL Constraints

    SQL Joins

    SQL inner join

    SQL left join

    SQL right join

    SQL full join

    SQL cross join

    SQL self join

    INSERT INTO SELECT statement

    INSERT INTO statement

    SQL Clauses

    SELECT clause

    FROM clause

    WHERE clause

    GROUP BY clause

    HAVING clause

    ORDER BY clause

    JOIN clause

    UNION clause

    UNION ALL clause

    TOP clause

    DISTINCT clause

    SQL Operators

    SQL Arithmetic operators

    SQL Comparison operators

    SQL Logical operators

    UNION operator

    UNION ALL operator

    INTERSECT operator

    EXCEPT operator

    LIKE operator

    NOT LIKE operator

    IN operator

    NOT IN operator

    IS NULL operator

    IS NOT NULL operator

    EXISTS operator

    NOT EXISTS operator

    BETWEEN operator

    NOT BETWEEN operator

    SQL Functions

    SQL Built-In functions

    CHARINDEX function

    DATEADD function

    CONCAT function

    LEN function

    REPLACE function

    SUBSTRING function

    CASE statement

    GETDATE function

    DATEPART function

    DATEDIFF function

    CAST function

    TRY_CAST function

    CONVERT function

    TRY_CONVERT function

    ISNULL function

    NULLIF function

    COALESCE function

    SQL Window functions

    ROW_NUMBER function

    RANK function

    DENSE_RANK function

    IIF function

    CHOOSE function

    SQL Store Procedure

    Store Procedure vs. Function

    SQL Subquery

    SQL Aliases

    Temp table

    SQL Error Handling

    SQL Variables

    SQL Views

    SQL Merge

    SQL CTE

    Define Transaction in DBMS

    ACID properties in DBMS

    Types of Triggers in DBMS


    • Have Some Questions?
    logo

    Elevate your data experience with SQL excellence

    Quick Links
    •  Home
    •  SQL Tutorial
    •  SQL Syntax
    •  Our Services
    Our Services
    • Web Development
    • BI Development
    • Data Warehousing
    • Data Integration ETL


    Follow Us

    GST Registered: XXAXXXXXXXZX
    Mumbai, Maharashtra, India support@sqlforgeeks.com

    © 2025 Copyright™ | All Rights Reserved | Privacy Policy