Select from two different tables - sql

So, I have two tables, Table R and Table N. The data in Table R is from Table N. My problem is I don't know the SELECT query that will display the result such as below, because there are 4 names, and SQL can only choose 1. Is there a query or any other way to get the result?
Table R:
Id1 Id2 Id3 Id4
1 3 5 7
2 4 6 8
Table N:
Id Name
1 A
2 B
3 C
4 D
5 E
6 F
7 G
8 H
After the SELECT statement, the result should look like this:
Name1 Name2 Name3 Name4
A C E G
B D F H
Anyway, thanks for helping. ^_^

SELECT
N1.Name AS `Name1`,
N2.Name AS `Name2`,
N3.Name AS `Name3`,
N4.Name AS `Name4`
FROM
R
INNER JOIN
N N1
ON
N1.Id = R.Id1
INNER JOIN
N N2
ON
N2.Id = R.Id2
INNER JOIN
N N3
ON
N3.Id = R.Id3
INNER JOIN
N N4
ON
N4.Id = R.Id4

select
n1.Name as Name1,
n2.Name as Name2,
n3.Name as Name3,
n4.Name as Name4
from
R
inner join N n1 on n1.id = R.id1
inner join N n2 on n2.id = R.id2
inner join N n3 on n3.id = R.id3
inner join N n4 on n4.id = R.id4

You can rename the table in the query
You can join the same table multiple times provided you rename it, so:
select N1.Name, N2.Name, N3.Name, N4.Name
from R
join N as N1 on N1.Id = Id1
join N as N2 on N2.Id = Id2
join N as N3 on N3.Id = Id3
join N as N4 on N4.Id = Id4
(of course that's equivalent to
select N1.Name, N2.Name, N3.Name, N4.Name
from R, N as N1, N as N2, N as N3, N as N4
where N1.Id = Id1 and
N2.Id = Id2 and
N3.Id = Id3 and
N4.Id = Id4
(the on clauses are just added to where and comma is join)

Related

How to join two tables NULL filling attributes that aren't related to other table

Hey im trying to join two tables
Table1
M N a3
--------
3 a W
3 b Q
3 c W
3 d Q
Table2
M N a4
--------
3 e M
3 f K
3 g K
3 h M
I want:
Result:
M N a3 a4
-----------
3 a W ∅
3 b Q ∅
3 c W ∅
3 d Q ∅
3 e ∅ M
3 f ∅ K
3 g ∅ K
3 h ∅ M
I tried:
select M, N, a3, a4
from Table1 t1
join Table2 t2 on (t1.M = t2.M and t1.N = t2.N)
I tried this but it gives me a null set since the N from T1 is disjoint from the N in T2.
Is there a way to null fill the columns that aren't related to that table specifically like what is described in the example code?
You seem to want union all:
select m, n, a3, null as a4 from table1
union all select m, n, null, a4 from table2
If there are (m, n) tuples that are common between the two tables, and you want them on a single row in the resultset, then it is a bit different. You can full join:
select coalesce(t1.m, t2.m) as m, coalesce(t1.n, t2.n) as n, t1.a3, t2.a4
from table1 t1
full join table2 t2 on t2.m = t1.m and t2.n = t1.n

query to select non matching records from two tables

I have 2 tables like this:
Table Student_Old:
id name city
1 A X
2 B Y
3 C Z
Table Student_new:
id name city
1 A X
2 M Y
3 C K
As you can see for Id 2,name is mismatching and for Id 3, city is mismatching in both tables(I am doing comparison on ID which is primary key in both table, basically student_new is backup table for old ). Now I want to get these 2 rows which are not matching.
e.g:
student_old s1, student_new s2-
s1.id s2.id s1.name s2.name s1.city s2.city
2 2 B M X X
3 3 C C Z K
Since you don't care about records which are in one table but not in the other, a simple JOIN will suffice:
SELECT s1.id AS old_id, s2.id AS new_id,
s1.name AS old_name, s2.name AS new_name,
s1.city AS old_city, s2.city AS new_city
FROM student_old s1
JOIN student_new s2
ON s1.id = s2.id
WHERE s1.name != s2.name OR s1.city != s2.city
Output:
old_id new_id old_name new_name old_city new_city
2 2 B M Y Y
3 3 C C Z K
SELECT so.id AS id_1, so.name AS name_1, so.city AS city_1,
sn.id AS id_2, sn.name AS name_2, sn.city AS city_2
FROM student_old so
INNER JOIN student_new sn
ON so.id = sn.id
WHERE so.name <> sn.name AND so.city <> sn.city
use join
select os.*,ns.* Student_Old os join Student_new ns on os.id=ns.id
where os.city!=ns.city OR os.name!=ns.name
Use inner join and where clause do filter out non matching name and city
select a.* , b.* from Student_Old inner join Student_new
on a.id=b.id
where a.name<>b.name or a.city<>b.city

Get the lowest Score of each distinct Name and distinct Group

Need to get the lowest Score of each distinct Name and distinct Group. Thank you.
Data:
Group Name Score
A N1 1
B N1 5
C N1 3
A N1 4
A N2 2
A N3 6
A N1 8
B N1 7
B N2 9
Result:
A N1 1
B N1 5
C N1 3
A N2 2
A N3 6
B N2 9
select [Group], Name , min( Score)
from my_table
group by [Group], Name

SQL Query without duplicates

I have 3 tables like this:
Names:
ID(K) Name
--------------
1 n1
2 n2
Jobs:
ID Job
------------
1 j1
1 j2
Phones:
ID Phone
--------------
1 p1
1 p2
1 p3
1 p4
I'm doing a SELECT with LEFT OUTER JOIN:
SELECT Names.Name, Jobs.Job, Phones.Phone
FROM
Names LEFT OUTER JOIN Jobs ON Jobs.ID = Names.ID
LEFT OUTER JOIN Phones ON Phones.ID = Names.ID
and returns:
n1 j1 p1
n1 j1 p2
n1 j1 p3
n1 j1 p4
n1 j2 p1
n1 j2 p2
n1 j2 p3
n1 j2 p4
n2 null null
but i need this results:
n1 j1 p1
n1 j2 p2
n1 null p3
n1 null p4
n2 null null
what's the solution?
You're not specifying which RDBMS, but this query works on at least SQL Server, PostgreSQL and Oracle. There may be a smarter analytic function to do this, but it escapes me at the moment.
WITH pjobs AS (
SELECT id, job,
ROW_NUMBER() OVER(PARTITION BY id ORDER BY job) rn FROM jobs
), pphones AS (
SELECT id, phone,
ROW_NUMBER() OVER(PARTITION BY id ORDER BY phone) rn FROM phones
)
SELECT name, job, phone
FROM pjobs FULL JOIN pphones ON pjobs.rn = pphones.rn AND pjobs.id = pphones.id
RIGHT JOIN names ON names.id = pjobs.id OR names.id = pphones.id
An SQLfiddle to test with.

SQl Server 2005 Pivot

I want to basically join these 3 table in a view for a report:
Class Table:
ID Name
1 N1
2 N2
3 N3
Flags Table:
ID ClassID Flags
1 1 F1
2 1 F2
3 2 F6
4 2 F3
5 3 F2
Sessions Table:
ID ClassID Sessions
1 1 S1
2 1 S4
3 2 S3
4 3 S5
5 3 S4
So my desired view should be something like this:
ID Name Flags Sessions
1 N1 F1,F2 S1,S4
2 N2 F6,F3 S3
3 N3 F2 S5,S4
Select C.Id, C.Name
, Stuff(
(
Select ', ' + F1.Flags
From Flags As F1
Where F1.ClassId = C.Id
Order By F1.Flags
For Xml Path('')
), 1, 2, '') As Flags
, Stuff(
(
Select ', ' + S1.Sessions
From Sessions As S1
Where S1.ClassId = C.Id
Order By S1.Flags
For Xml Path('')
), 1, 2, '') As Sessions
From Class As C
select Class.ID,Name,Flags,Sessions
from Class
inner join flags on class.id = flags.classid
inner join sessions on class.id = sessions.classid