1

I have two tables called owner and condo_unit and I need to get selected information from both of the tables. These are my two tables below:

Owner

OwnerNum, LastName, FirstName, Adress

Condo_Unit

Condo_ID, LocationNum, OwnerNum

How do I get one of the OwnerNum, LastName and LocationNum? I'm struggling with this since there is an OwnerNum in each of the tables. Just started learning SQL so I'm a bit confused on how to do so. If anyone can help that would be great.

3 Answers 3

2

You can do Inner Join/Join.

SELECT
        o.OwnerNum,
        o.LastName,
        cu.LocationNum
FROM
        owner o -- o and cu (below) are alias for owner and condo_unit table respectively, you can use owner.ownerNum too, but it becomes messy when used with lot of tables and tables with big names 
JOIN
        condo_unit cu
        ON cu.ownernum = o.ownernum -- this line joins two tables on a common column/id and retrieves common records for the selected columns

Joins help you to conditionally select common data from multiple tables. There are multiple joins like Left Join, Right join, Inner Join. It's good that you are learning them - they are very important for any kind of web-development and also for writing complex queries later on in the career.

For starters you can follow this: https://www.w3schools.com/sql/sql_join.asp

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

Comments

1

Just join:

select o.ownernum, o.lastname, cu.locationnum
from owner o
inner join condo_unit cu on cu.ownernum = o.ownernum

Comments

0

Use join query

  SELECT 
OW.OWNERNUM,
OW.LASTNAME,
CO.LOCATIONNUM
FROM OWNER OW INNER JOIN CONDO_UNIT CO 
ON OW.OWNERNUM=CO.OWNERNUM

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.