Efficient way for finding attributes in a table which belong together, based on second table - sql

I have two tables:
person
----------
id name
1 peter
2 paul
3 mary
4 george
5 andy
and
living
-------
id key
1 1
2 1
3 2
4 2
5 2
What would be the most efficient query for finding all ids in person which belong together, if I know the name. Let's say I want to find out: "Who is Mary living with"? In person, Mary's id is 3. I check living for the key, which belongs to Mary, which is 2. Now I can retrieve all ids in living which go with key 2, and I have the ids which live together with Mary.
This is what I got so far:
SELECT p2.name
FROM person p1
JOIN living l1
ON p1.id = l1.id
JOIN living l2
ON l1.key = l2.key
JOIN person p2
ON l2.id = p2.id
WHERE p1.name = 'mary'
Is there a better way? The result seems to be fine, but it doesn't feel right to perform three JOINS.

What you could do in you case is:
DEFINE name CHAR(15);
LET name = 'mary'
define keyValue char(10);
select l.key into keyValue from person p left outer join living l on p.id= l.id and p.name= name
select p.*, l.* from person p left outer join living l on p.id=l.id
where key= keyValue
I have never used Informix before, however the the best solution should be along those lines or at least close to them.
Thanks

Related

SQL Join to return data from The Same Column in the same table to two diffrent rows in result (Star Wars Example)

Newbie question about joining tables. I want to retrieve a name from a column TWICE in a SQL statement, and I'm running in circles.
I Have two Tables - "Company" & "People"
Table -"People"
ID
Name
Phone
1
Luke
555-1212
2
Leia
555-1234
3
Han
999-8888
4
Anikin
888-9876
5
Obi-wan
555-1212
6
R2-D2
#% - **!?
Table - "Company"
ID
CompanyName
PrimaryContact
AltContact
1
Speeders R Us
5
1
2
Droid Repair World
6
4
3
Luke's Second Hand Store
1
4
4
Cloak World
4
5
5
Ye Old Blaster Shoppe
3
2
If I want to get a result that gives BOTH the Contact Names for a Company, How would I do it?
I can get the PrimaryContact to JOIN Properly using something like...
SELECT C.*, P.Name as 'Primary'
FROM `Company` C
Join People P on
C.PrimaryContact = P.ID
WHERE C.ID =3
which successfully returns
ID
CompanyName
PrimaryContact
AltContact
Primary
3
Luke's Second Hand Store
1
4
Luke
But for the life of me, I can't figure out how to modify this SQL to also return "Anikin" as the Alternate Contact. Is this an example of where a UNION statement would help?
You can join to the same table multiple times, just give a new alias every time.
Join People P on C.PrimaryContact = P.ID
Join People P1 on C.AltContact = P1.ID
Join People altcontact on C.AltContact = altcontact.ID
Join People P256 on C.yetanotheralternateContact = P256.ID
You need to to join for ecervy contact another Persons table
SELECT C.ID,C.CompanyName, P.Name as 'Primary' , P.Phone As 'primary_Phone', P2.Name on 'Alternative', P2.Phone as 'Alternatibe_Phone
FROM `Company` C
Join People P on
C.PrimaryContact = P.ID
Join People P2 on
C.AltContact = P2.ID
WHERE C.ID

Count the sum of a left outer join table

I have two tables which is
Student
ID
Name
Gender
Address
1
Abby
Girl
street
2
Mark
Boy
street
3
Lula
Girl
street
4
Bren
boy
street
Lessons
ID
Lessons_code
3
MK2234
5
22324KL
6
KCS233
and I want to join these tables then get the sum result of the students that didn't took a lesson then group it by gender like this:
Gender
total
Boy
2
girl
1
I know it use sum() and left outer join (?) but I don't know how to implement it.
I would suggest not exists:
select s.gender, count(*)
from students s
where not exists (select 1
from lessons l
where l.id = s.id
)
group by s.gender;
You have a very awkward data model. I would expect a column called lessons.id to refer to the primary key of the lessons table. Instead. it seems to refer to a student. A better name would be student_id.

How can I SUM a COUNT(*) subquery to find two string values per ID?

I'm trying to build a SQL Server query to spit out numbers. Specifically, I need to know how many people like both Pizza and Soda but displayed by only showing the proper count for relationship.
I have 2 tables that look like this:
People:
1 Andy Relative
2 Jim Friend
3 Anderson Friend
4 Pamela Relative
Likes:
1 Pizza
1 Soda
2 Pizza
3 Soda
4 Pizza
4 Soda
My desired output would be the following. A list of how many relatives and how many friends like Pizza and Soda:
Relative 2
Friend 0
This is pretty much where I'm at but this is wholly incorrect:
SELECT
relation,
(select count(*) from likes lik where id = lik.id and pizzavalue = 'Pizza')
+
(select count(*) from likes lik where id = lik.id and pizzavalue = 'Soda')
FROM
dbo.People
INNER JOIN
Likes lik on ps.id = lik.id
Try:
select relation, count(*)
from People p
join likes l1 on l1.id = p.id and pizzavalue = 'Pizza'
join likes l2 on l2.id = p.id and pizzavalue = 'Soda'
group by relation
This will get first join the person table to the likes table and leave us with every person that likes both soda and pizza. The group by will return a single count per relation.

What Join should I use in SQL

I have four tables in my Database:
Person that contains ID(PK) and Name.
Person_Skill that contains ID(PK), PID(FK), Skill(FK) and SkillLevel(FK).
Skill that contains ID(PK) and SkillLabel.
SkillLevel that contains ID(PK) and Name.
Every skill has level from 0 to 7
Now I want to display all the skill that the person has(Include the skilllevel = 0)
Select
[dbo].Person.Name as Name,
[Skill].SkillLabel as SkillName,
[Person_Skill].[SkillLevel] as SkillLevel
From
([dbo].Person inner join [dbo].[Person_Skill] ON [dbo].[Person_Skill].PID= Person.ID)
inner join [dbo].[Skill] ON [dbo].[Person_Skill].Skill=Skill.ID
The above code only display the skill that person has from level 1 to level 7.
I believe the reason I only get the skill from level 1 to 7 is because the person table only contains the skill from level 1 to 7, but I'm not sure about this. I got the database from other. If my assumption is correct, is there anyway to do this? To get all the skills in the skill table and display the skill level that the person has(Include skillllevel =0).
Sample Data:
Person
ID Name
----------
1 Michael
2 Alex
Person_Skill
ID PID SkillID Skill_Level
5 1 10 5
6 2 11 1
7 1 12 7
8 1 13 5
Skill
ID Name
10 java
11 C++
12 HTML
13 ASP
14 C
15 .NET
16 C#
17 Objective
The expect results are;
Name SkillName SkillLevel
----------------------------
Alex java 0
Alex C++ 1
Alex HTML 0
Alex ASP 0
Alex C 0
Alex .NET 0
Alex C# 0
Alex Objective C 0
Michael java 5
Michael C++ 0
Michael HTML 7
Michael ASP 0
Michael C 0
Michael .NET 5
Michael C# 0
Michael Objective C0
The current query only output
Alex C++ 1
Michael java 5
Michael HTML 7
Michael .NET 5
Edit, if you want to return all skill names for each person, then you will want to use:
select d.p_name,
d.s_name SkillName,
coalesce(ps.Skill_Level, 0) Skill_Level
from
(
select p.id p_id, p.name p_name, s.id s_id, s.name s_name
from person p
cross join skill s
) d
left join person_skill ps
on d.p_id = ps.pid
and d.s_id = ps.skillid
left join skill s
on ps.skillid = s.id
See SQL Fiddle with Demo
If you want to include all Skills 0-7, then you will want to use a LEFT JOIN. You query will be similar to the following:
select p.Name as Name,
s.SkillLabel as SkillName,
ps.[SkillLevel] as SkillLevel
from [dbo].[Skill] s
left join [dbo].[Person_Skill] ps
on ps.Skill=s.ID
left join [dbo].Person p
on ps.PID = p.ID
Edit, without seeing any data it is difficult to determine. But if you want to retrieve all SkillLevels, then you will need to include that table. You might need to use:
select
p.Name as Name,
s.SkillLabel as SkillName,
sl.[Name] as SkillLevel
from SkillLevel sl
left join [dbo].[Person_Skill] ps
on ps.SkillLevel=sl.ID
left join [dbo].[Skill] s
on ps.Skill = s.Id
left join [dbo].Person p
on ps.PID = p.ID
You would want to use a LEFT JOIN which when tableA is inner joined on tableb would return all records from tableA regardless of whether or not there was a match from tableB
Therefore, if there are no persons with a skill of 0, you will still get back all of the person records.
An INNER JOIN will only return rows where there is a match on both sides. So in your code if a person does not have a skill with level 0 it would not be returned.
You could do a LEFT or RIGHT join and these get all the rows from the table on either the left or the right side. I think you probably want to use a left join, but without knowing more about your schema it's hard to say for sure. See the answer given to the question Left join and Left outer join in SQL Server for more detail on the differences in different join types

Return field in SQL that intersects on a second field?

I am trying to return the fields that have intersecting fields for a specific person. What I mean by this is
Name Friend
---- -----
Joe Sally
Joe Bill
Mary Sally
Mary Michael
Mike Joe
Bill Bill
Bill Sally
Gil Sally
Gil Bill
Gil David
Say, we want the list of people that match Joe's second column, they would have to match Sally and Bill both. So only Bill matches this criteria because Mary has one, but she doesn't have Bill. Gil has Sally and Bill, but he also has David. So only Bill should be returned. I was thinking something with INTERSECT would work because that returns common fields but that wouldn't account for someone having more, I think. Not sure how to write a SQL query to do what I want.
Clearly stated, the list of names that have the same exact friends as Joe.
Without duplicates
SELECT p2.name
FROM people AS p1
JOIN people AS p2 ON p2.number = p1.number
AND p2.name <> p1.name -- exclude self-join
WHERE p1.name = 'Joe'
AND NOT EXISTS (
SELECT 1
FROM people p3
WHERE p3.name = p2.name
AND p3.number <> p1.number
)
GROUP BY p2.name
HAVING count(*) = (SELECT count(*) FROM people WHERE name = 'Joe')
The last condition AND NOT EXISTS ... is only needed if you want to exclude people that have additional friends Joe does not have.
It excludes Gil in your example from the result.
This is a special case of relational division (with a self-referencing table). You can find a whole arsenal of query techniques in this related answer:
How to filter SQL results in a has-many-through relation
With duplicates
If there can be duplicates (like in your first draft of the question), things get a little more complicated:
WITH p AS (
SELECT name, number, count(*) AS ct
FROM people
GROUP BY name, number
)
SELECT p2.name
FROM p AS p1
JOIN p AS p2 ON p2.number = p1.number
AND p2.ct = p1.ct
AND p2.name <> p1.name -- exclude self-join
WHERE p1.name = 'Joe'
AND NOT EXISTS (
SELECT 1
FROM p p3
WHERE p3.name = p2.name
AND p3.number <> p1.number
)
GROUP BY p2.name
HAVING count(*) = (SELECT count(*) FROM p WHERE name = 'Joe')