2

I am new to SQL and I am learning inner joins. However, when I run my query, I am receiving more outputs than I should be.

SELECT          pfr.pno AS 'Property Number', 
                    pfr.street, 
                    pfr.rooms, 
                    pfr.rent, 
                    CONCAT(o.fname, ' ', o.lname) AS 'Owner Name', 
                    CONCAT(s.fname,' ',s.lname) AS 'Staff Name', 
                    v.date, 
                    v.comment
From                property_for_rent AS pfr
INNER JOIN  owner AS o
ON                  pfr.ono = o.ono
INNER JOIN  staff AS s
ON                  pfr.sno = s.sno
INNER JOIN  viewing AS v
ON                  pfr.pno = v.pno
WHERE           pfr.pno = 'PG4';

I have attached a screenshot of my output. I hope this is enough information!

Query Output Image

Table in use Image

5
  • 1
    Your duplicates aren't duplicate. You have 2 rows in viewing that match that pfr number (I would assume that means the property has been viewed 2 times). One with no comment in may and one with 'too remote' sometime in April. Commented Oct 10, 2017 at 17:23
  • Obviously you have one-to-many relationship on these tables. I think it behaves correctly. It's just a matter of what logic do you want to handle the duplicated data. Commented Oct 10, 2017 at 17:24
  • Show your viewing table data for that id Commented Oct 10, 2017 at 17:24
  • The INNER JOIN keyword selects all rows from both tables as long as there is a match between the columns. Commented Oct 10, 2017 at 17:27
  • I see now, the viewing table has 2 viewings for the property. Thanks! Commented Oct 10, 2017 at 17:28

2 Answers 2

1

When you join two tables, this will happen if you don't have a 1:1 relationship.

For example, you have two records in the "viewing" table, so there must be two rows shown even though there is just one property.

If want a single property result, you need to logically aggregate the viewing table, for example by getting the most recent viewing for the property.

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

Comments

0

I would not consider these duplicate rows. If you look at the date column for the PG4 transaction, you'll notice that the dates are different. These are two different transactions and are not considered dups. You can consider use the Distinct clause if you are looking to return only one record of a particular column. Hope this helps!

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.