Select duplicates by PrimaryKey I have to select all duplicates by PrimaryKey

Here is my DB

ID   UserID   GroupID
1    1        1
2    1        1
3    1        2
4    2        2


If I pass ID=1 need to select row 1 and 2

Here is my searched id:

DECLARE @id INT = 1;


Here is my SQL Script 1:

SELECT *
FROM table as t1
WHERE EXISTS  (
      SELECT 1
      FROM table as t2
      WHERE  
           t2.ID=@id AND
           t2.UserID = t1.UserID AND
           t2.GroupID = t1.GroupID
)


Here is my SQL Script 2:

SELECT t1.*
FROM table as t1 INNER JOIN 
(
    SELECT *
    FROM table as t3
    WHERE t3.ID = @id
) as t2 ON t2.UserID = t1.UserID AND t2.GroupID = t2.GroupID


With this Selects the result is ok, but I'm not sure is the correct way and who is the best for the performance

Please give me some advice. Thanks.
Both your queries are fine, assuming that (ID) is the primary key of the table (or it has a UNIQUE constraint).

The 2nd query can be simplified, removing the derived table:

SELECT t1.*
FROM table as t1 INNER JOIN 
     table as t2
     ON t2.UserID = t1.UserID AND t2.GroupID = t2.GroupID
WHERE t2.ID = @id ;




For performance, you may need an index on (UserID, GroupID) - with any of the queries.