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.
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