How to add a column to the table from another table in SQL?

79 views Asked by At

I have 2 tables :

  1. Person.Person
  2. Person.PersonPhone

The table Person.PersonPhone contains 4 columns :

BusinessEntityID
PhoneNumber
PhoneNumberTypeId
ModifiedDate

The first table contains more columns but I'm working only with 4 :

 FirstName, LastName, PersonType, BusinesEntityID

So, I want to add to my first table (Person.Person) column PhoneNumber from Person.PersonPhone. And here I encountered the difficulty.

This is what I did

USE AdventureWorks2019

SELECT FirstName, LastName, PersonType, BusinessEntityID
FROM Person.Person
WHERE PersonType = 'EM '

SELECT PhoneNumber
FROM Person.PersonPhone
WHERE PhoneNumberTypeID = 3

And this is my initial condition: check the content of Person.Person, Person.PersonPhone and Person.PhoneNumberType tables from AdventureWorks2019 database.

Write a query that shows each person's First Name, Last Name, Phone Number, but only for the records with "Employee" Person Type and "Work" Phone Type (please do not "hardcode" the phone type, use subquery or join instead)

1

There are 1 answers

0
James Fuller On

I tried your query code, and it took like 8 seconds for the phone number list to show up. I thought that was kind of odd, but the SQL app did it.

USE AdventureWorks2019

SELECT FirstName, LastName, PersonType, BusinessEntityID
FROM Person.Person
WHERE PersonType = 'EM '

SELECT PhoneNumber
FROM Person.PersonPhone
WHERE PhoneNumberTypeID = 3