SQL Select all in the table that have the same identifier as ".."

Basically, I have a table of support users and there are different users of different levels in different departments.

So, let's say I basically have the following table:

id | userID | deptID | level
1      1        1        1
2     119       1        2
3      2        1        3
4     101       2        1
5     104       2        2

      

And I have a number id

, so let's say I want all users with the same deptID

as user in id

:, 3

returning the first three rows.

What will the SQL statement be?

+3


source to share


5 answers


You can use the following query containing a subquery:



SELECT * 
FROM <table> 
WHERE deptID=(
              SELECT deptID FROM <table> WHERE userID=3
             )

      

+6


source


select  u.*
from    users u join users u2 on u.deptID = u2.deptID
where   u2.id = 3

      



+2


source


SELECT *
FROM Users
WHERE deptID IN 
    (SELECT deptID FROM Users WHERE userID = 3)

      

+1


source


try it

select my.*
from myTable my
join myTable myt on my.deptID = myt.deptID
where myt.id = 3

      

0


source


select * from data where deptid in (select deptid from data where id=3);

      

Use this query

0


source







All Articles