SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 LEFT OUTER JOIN
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

jurE
Starting Member

3 Posts

Posted - 12/02/2012 :  06:12:28  Show Profile  Reply with Quote
The question:
Display the names of the workers that live in the same cities as their chef.

Table one: Werknemer
columnes: wnr (workersnumber), wnaam (workersname), chef (wnr of the chefs)

Table two: Prive
columnes: wnr (workersnumber), wnaam (workersname), city (names of the city)

What should my OUTER JOIN LOOK LIKE?

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 12/02/2012 :  07:03:04  Show Profile  Reply with Quote
is chef also present as a worker in worker table?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

jurE
Starting Member

3 Posts

Posted - 12/02/2012 :  08:27:41  Show Profile  Reply with Quote
Yes!
quote:
Originally posted by visakh16

is chef also present as a worker in worker table?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/



Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2223 Posts

Posted - 12/03/2012 :  01:48:50  Show Profile  Reply with Quote
Try this once...........

;with cte 
AS
(	
	SELECT city, w.wnr, w.wnaam, chef
	FROM @Werknemer w 
	JOIN @Prive p ON w.wnr = p.wnr
)
SELECT wnaam
FROM cte c
WHERE city = (SELECT city FROM @Prive p WHERE p.wnr = c.chef)


--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 12/03/2012 :  07:38:07  Show Profile  Reply with Quote
quote:
Originally posted by jurE

Yes!
quote:
Originally posted by visakh16

is chef also present as a worker in worker table?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





SELECT w.wnaam
FROM Werknemer w
JOIN Prive p1
ON p1.wnr = w.wnr
JOIN Prive p2
ON p2.wnr = w.chef
WHERE p1.city = p2.city




------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

jurE
Starting Member

3 Posts

Posted - 12/04/2012 :  15:48:00  Show Profile  Reply with Quote
Thanks for all the replies!

This was the anwers:

SELECT A.wnaam AS chef, B.wnaam AS werknemer
FROM Werknemer A INNER JOIN Werknemer B
ON (A.wnr = B.chef)
INNER JOIN Prive C
ON (A.wnr = C.wnr)
INNER JOIN Prive D
ON (B.wnr = D.wnr AND C.woonplaats = D.woonplaats)
ORDER BY chef
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000