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
Chud37
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
fedorqui
source
to share
select u.*
from users u join users u2 on u.deptID = u2.deptID
where u2.id = 3
+2
paul
source
to share
SELECT *
FROM Users
WHERE deptID IN
(SELECT deptID FROM Users WHERE userID = 3)
+1
Sergio
source
to share
try it
select my.*
from myTable my
join myTable myt on my.deptID = myt.deptID
where myt.id = 3
0
echo_Me
source
to share
select * from data where deptid in (select deptid from data where id=3);
Use this query
0
Sathesh s
source
to share