I have two tables:
Table_1: (Name, Street, City)
Table_2: (Name, Teacher_Name)
The column Name in Table_1 contains Student's Name as well as Teacher's name while in Table_2 column Name contains only Student's Name and Teacher_Name contains Teacher's Name corresponding to the Student.
So my question is that what will be the SQl query for Finding all students who live in the same Street and City as their Teachers.
please help and thanks in advance.
You need to join Table_1 twice. Like so:
SELECT students.Name, teachers.Name
FROM Table_2 AS teacher_student
JOIN Table_1 AS students ON students.Name = teacher_student.Name
JOIN Table_1 AS teachers ON teachers.Name = teacher_student.Teacher_Name
WHERE students.Street = teachers.Street
AND students.City = teachers.City
Try this query:
SELECT DISTINCT t1.Name
FROM Table_1 t1 INNER JOIN Table_2 t2
ON t1.Name = t2.Name
INNER JOIN Table_1 t3
ON t3.Name = t2.Teacher_Name
WHERE t1.Street = t3.Street AND t1.City = t3.City
You need to join twice to achieve what you want. The first join is necessary to connect the address table with the bridge table of students to teachers. Then you need to join again to get the addresses for those teachers.
Related
SELECT name FROM table1
WHERE name NOT IN (
SELECT name, school FROM table2
UNION
SELECT name, school FROM table3
)
This syntax keeps flagging error near syntax 'Union'. Any suggestion on how to fix this please?
In the subquery, only select the name column that's used as a filter. Unless you need combination of the name and school columns, in which case they'll need to be concatenated. The space in the name of table3 will need to be removed, unless this is the actual name of the table. If it is, it will be to be enclosed in brackets, i.e. [table 3].
SELECT name FROM table1
WHERE name NOT IN (
SELECT name FROM table2
UNION
SELECT name FROM table3
)
This is functionally the same and may be faster in some cases
SELECT name
FROM table1 as t1
left join table2 as t2 on t1.name = t2.name
left join table3 as t3 on t1.name = t3.name
where coalesce(t2.name,t3.name,'new') = 'new'
you can also use a where clause like this:
where t2.name is null and t3.name is null
I am trying to join only select columns from 3 different tables and haven't been successful.
Table 1, Patient, has the following columns I need:
ExternalID, UserDefinedXML, ServiceSiteUid
Table 2, PDI, has the following columns I need:
Patient ID, FirstName, LastName, State
Table 3, ListServiceSite has the following columns I need:
ServiceSiteUid, Name
I need to join Patient and PDI based on the common ID columns ExternalID and PatientID, and then join Patient and ListServiceSite by ServiceSiteUid.
Here's what I have that has been unsuccessful:
SELECT
*
FROM
(SELECT
ExternalID, UserDefinedXml, ServiceSiteUid
FROM
Patient) A
INNER JOIN
(SELECT
[Patient ID], FirstName, LastName, State
FROM
PatDemogImport) B ON A.ExternalID = B.[Patient ID]
WHERE
UserDefinedXml IS NOT NULL;
I am very new to SQL so please have patience! Thank you in advance.
This should do it
SELECT *
FROM Patient as p
JOIN PatDemogImport as pdi
ON p.ExternalID = pdi.[Patient ID]
JOIN ListServiceSite as lss
ON lss.ServiceSiteUid = p.ServiceSiteUid
WHERE p.UserDefinedXml IS NOT NULL;
SELECT * FROM PATIENT
JOIN
PDI on PATIENT.ExternalID = PDI.ExternalID AND PATIENT.PatientID = PDI.PatientID
JOIN
ListServiceSite on ListServiceSite.ServiceSiteUid = PATIENT.ServiceSiteUid
where
UserDefinedXml IS NOT NULL;
That should give you what you're looking for. There's no need for subselects in this instance.
Essentially what you are doing is joining the tables based on the relationship between columns. So in the above we join PDI and PATIENT based on their 2 shared columns and ListServiceSite and PATIENT on ServiceSiteUid.
Please note that when you do a SELECT *, you will get duplicate columns in the result (since you're basically getting all the columns across all those tables) so I'd recommend specifying what you select. i.e. SELECT PATIENT.ExternalID, ....
Finally, I'd recommend reading up on the different kind of joins. The above is just a regular join but there are several others like OUTER JOIN INNER JOIN RIGHT JOIN LEFT JOIN and they all have differences.
SELECT Patient.*
FROM Patient INNER JOIN PatDemogImport as pdi ON
Patient.ExternalID=pdi.Patientid
Inner join ListServiceSite on
Patient.ServiceSiteUid=ListServiceSite.ServiceSiteUid
WHERE Patient.UserDefinedXml IS NOT NULL
Isn't it just as simple as this? You want an inner join and you just need to specify the columns you need. This should give you exactly the answer you're looking for. These other answers have the same idea, but they assume that the tables contain only the columns listed and no other columns. PatientID should be the same as ExternalID so no need to specify that column and C.ServiceSiteUid should be the same as in Patient.
SELECT A.ExternalID, A.UserDefinedXML, A.ServiceSiteUid,
B.FirstName, B.LastName, B.State,
C.Name
FROM Patient A
INNER JOIN PatDemogImport B ON B.PatientID = A.ExternalID
INNER JOIN ListServiceSite C ON C.ServiceSiteUid = A.ServiceSiteUid
WHERE A.UserDefinedXML IS NOT NULL
I have two tables and did an inner join on them based on id. Now I need to name this joined table. How do I do that? The reason I want to name this table is because I have to join this result table with some other tables.
You created what is commonly referred t as a relvar, not another table. You can join to it by placing it's SELECT definition in a sub query like this:
SELECT
FROM (
-- original join SQL
) t
INNER JOIN table2 on ...
You can create an alias for the result of a join using a subquery. For example:
select *
from (
select *
from tbl1
join tbl2
on tbl1.id = tbl2.id
) as JoinAlias
join tbl3
on JoinAlias.id = tbl3.id
You can often make do without such an alias. For example, consider a third join:
select *
from tbl1
join tbl2
on tbl1.id = tbl2.id
join tbl3
on tbl3.col1 = tbl1.col1
and tbl3.col2 = tbl2.col2
As you can see, the third join's condition can refer to columns from all earlier tables.
select * from (
select * from TableA a join TableB b on a.id = b.a_id
) as p;
As per the comments, it seems that a view might be what you are looking for:
create view MyView
as
select * from TableA a join TableB b on a.id = b.a_id;
It won't be listed in the tables (rather in the views), but will behave just like a table in your sql editor.
I need to insert 3 columns from one table to another, using JOIN by 3 fields: name, surname and age
I need update column status, status1 and status2 in table_2 with values from table_1
IF
table_1.name = table_2.name
table_1.surname = table_2.surname
table_1.age= table_2.age
UPDATE t2
SET
t2.[status]=t1.[status]
,t2.[status1]=t1.[status1]
,t2.[status2]=t1.[status2]
FROM [table_1] t1
INNER JOIN [table_2] t2
ON (t1.name=t2.name AND t1.surname=t2.surname AND t1.age=t2.age)
As you mentioned in comments that these table from different databases, then please change only the two line like.
FROM [yourDataBase1Name].[dbo].[table_1] t1
INNER JOIN [yourDataBase2Name].[dbo].[table_2] t2
Just update the table with Join.
it will be something like this:
UPDATE t2
SET t2.status = t1.status,
t2.status1 = t1.status1,
t2.status2 = t1.status2
FROM t2 JOIN t1 on (t1.first_name = t2.first_name AND t1.last_name = t2.last_name AND t1.age = t2.age);
Look here for more information:SQL update query using joins
SQL Fiddle: http://sqlfiddle.com/#!3/b3951/1
I need to join two tables to get all the records from the student_info table and just the records from the student_activities table where the student_id's are equal.
As there can be multiple records in the student activities table for a single student_id I'm getting duplicates when I print the output using a left join.
SELECT *
FROM student_info
LEFT JOIN student_activities
ON student_info.student_id=student_activities.student _id
It was suggested that I use the following but I get errors saying that specific fields are not part of an aggregate function.
SELECT student_info.student_id, student_info.student_name, student_info.phone, student_info.age, COUNT (student_activities.student_id) AS COA
FROM student_info
LEFT OUTER JOIN student_activities
ON student_info.student_id=student_activities.student_id
GROUP BY student_info.student_id
SELECT student_info.student_id, student_info.student_name, student_info.phone, student_info.age, ISNULL(t.COA, 0) COA
FROM student_info
LEFT JOIN (SELECT student_id, COUNT(*) COA FROM student_activities GROUP BY student_id) t
ON student_info.student_id = t.student_id
SELECT
student_info.student_id,
student_info.student_name,
student_info.phone,
student_info.age,
COUNT (student_activities.student_id) AS COA
FROM student_info LEFT OUTER JOIN student_activities
ON student_info.student_id=student_activities.student_id
GROUP BY student_info.student_id,student_info.student_name, student_info.phone, student_info.age
Not tested, but should work or something like this:
SELECT DISTINCT *
FROM student_info
WHERE student_info.student_id =
(SELECT student_activities.student _id FROM student_activities)