1

I have created a hypothetical scenario with some dummy data. Below are 3 basic tables and I'm looking to write a query to find what property/properties Mary has viewed. In this scenario Mary has viewed 1 property a flat in Glasgow.

Table 1: Client

=====================
ID  Name        
=====================
5   Tom
6   Mary
7   John

Table 2: Property

=====================
ID  CITY        TYPE    
=====================
14  Aberdeen    House
16  Glasgow     Flat
21  Glasgow     House
94  London      Flat

Table 3: Viewing

========================
Client  Property    Date    
========================
5       14      01-12-2016
5       21      08-12-2016
6       16      10-10-2016
5
  • 1
    INNER JOIN would be my first thought, particularly for someone learning SQL. Commented Oct 5, 2017 at 20:21
  • What are you trying to show? Commented Oct 5, 2017 at 20:24
  • SEarle1986 I was just trying to see how I would go about seeing what property Mary viewed Commented Oct 5, 2017 at 20:27
  • Sorry, missed that bit! The question has answers now but I agree with them that JOIN (Or INNER JOIN) is the way to go Commented Oct 5, 2017 at 20:29
  • Thanks SEarle1986 I see them, i'm just looking at the two answers now and trying figure them out as one uses join and the other inner Join Commented Oct 5, 2017 at 20:34

1 Answer 1

4

Definitely use inner joins for this, a quick example of this could be

SELECT c.Name, p.Type, p.City, v.Date
FROM Viewing v 
JOIN Client c ON v.client = c.ID
JOIN Property p ON v.property = p.ID
WHERE c.Name = 'Mary'

That should show you who view what and when. I've used aliases on the table names just to keep it neat.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.