Related
Imagine dance teams (of varying size) can choose one or more outfits and each outfit can have one or more items. All members of a team must have all same outfits and all the items of each outfit, i.e., incomplete outfits are bad -- we want to find those bad outfits.
The tables below define two teams: APPLE and BANANA. Team APPLE has 3 members, team BANANA two members. The APPLEs have chosen a single outfit which has a single item, the YELLOW PANTSUIT outfit, naturally a crowd favorite. Team BANANA have two outfits: RED, and BLUE; but, BAILEY is missing the BANDANNAS item for the BLUE outfit. Awkward.
Let's get BAILEY out of trouble. Create a query to find missing items in the outfits chosen by the teams.
Thanks to #Brits for the SQL below:
Teams and team-members:
CREATE TABLE team (
id int unique,
name text
);
INSERT INTO team (id, name)
VALUES (1, 'APPLE'),
(2, 'BANANA');
CREATE TABLE team_member (
id int unique,
name text,
team_id int references team (id)
);
INSERT INTO team_member (id, name, team_id)
VALUES (1, 'ADAM', 2),
(2, 'BAILEY', 2),
(3, 'CATE', 1),
(4, 'DAVE', 1),
(5, 'ERIN', 1);
Outfits and outfit items:
CREATE TABLE outfit (
id int unique,
name text
);
INSERT INTO outfit (id, name)
VALUES (1, 'RED'),
(2, 'YELLOW'),
(3, 'BLUE');
CREATE TABLE outfit_item (
id int unique,
name text,
outfit_id int references outfit (id)
);
INSERT INTO outfit_item (id, name, outfit_id)
VALUES (1, 'SHORTS', 1),
(2, 'SHIRT', 1),
(3, 'PANTSUIT', 2),
(4, 'BANDANNA', 3),
(5, 'HAT', 3);
Team member outfit items:
CREATE TABLE member_item (
member_id int references team_member (id),
item_id int references outfit_item (id)
);
INSERT INTO member_item (member_id, item_id)
VALUES (1, 1),
(1, 2),
(1, 4),
(1, 5),
(2, 1),
(2, 2),
(2, 5),
(3, 3),
(4, 3),
(5, 3);
Team APPLE's members all have the YELLOW PANTSUIT outfit so the APPLEs are ready to rumble.
Team BANANA chose the RED and BLUE outfits; sadly, I failed to give team BANANA the YELLOW PANTSUIT outfit - they would have killed it, but whatever. Team BANANA's ADAM and BAILEY have the items for the RED outfit, but BAILEY does not have the BANDANNA for the BLUE outfit; let's not let a bandanna get in the way to team BANANA so we need a query to return just:
BANANA BAILEY BLUE BANDANNAS
To find the number of items in an outfit:
SELECT
o.name
, count(*) AS "number of items"
FROM
outfit_item i
, outfit o
WHERE
i.outfit_id = o.id
GROUP BY
i.outfit_id
, o.name
name | number of items
--------+-----------------
RED | 2
BLUE | 2
YELLOW | 1
Similarly, the number of members in a team:
SELECT
t.name
, count(*) AS "number of members"
FROM
team_member m
, team t
WHERE
m.team_id = t.id
GROUP BY
m.team_id
, t.name
name | number of members
--------+-------------------
BANANA | 2
APPLE | 3
That's all well and good, but how do we combine this information so we can bail BAILEY out of this BANANA blunder?
Taking on board the comment "If any member has an outfit item then all team members must have all items in that outfit" I believe the following will do what you are looking for (I'm using a CTE to work out what outfits are associated with each team).
with teamoutfit as (
-- If any mmber of a team has any part of an outfit then that team is linked to that outfit
select distinct te.id as team_id, oft.id as outfit_id from
team te
inner join team_member tm on tm.team_id = te.id
inner join member_item mi on mi.member_id = tm.id
inner join outfit_item oi on oi.id = mi.item_id
inner join outfit oft on oft.id = oi.outfit_id
)
select te.name as team, tm.name as member, of.name as outfit, oi.name as item from
team te
inner join team_member tm on tm.team_id = te.id
inner join teamoutfit tof on tof.team_id = tm.team_id
inner join outfit of on of.id = tof.outfit_id
inner join outfit_item oi on tof.outfit_id = oi.outfit_id
left join member_item mi on oi.id = mi.item_id and tm.id = mi.member_id
where
mi.member_id is null
Output:
| team | member | outfit | item |
|--------|--------|--------|----------|
| BANANA | BAILEY | BLUE | BANDANNA |
I setup a SQL Fiddle you can use to play with this (and perhaps clarify your question if I misunderstood what you are trying to do).
Confirmed to work on postgres, albeit not pretty and am 85% sure this can be simplified. Also, I was a little confused on how outfit and outfit_item tables are related. In the way you laid out the tables, bandanas and hats are are always blue, pantsuits are always yellow and shorts/shirts are always red when you join outfit to outfit_item (outfit.id = outfit_item.outfit_id). The test data and query has been written with that understanding.
select distinct
tm.name as member_name,
t.name as team_name,
o.name as outfit_color,
oi.name as outfit_item
from team_member tm
join team t
on t.id = tm.team_id
join member_items mi
on mi.member_id = tm.id
join(
with outfit_item_count as
(SELECT outfit_id, count(*) AS items
FROM outfit_items
GROUP BY 1)
select mi.member_id, oi.outfit_id, oic.items as items_needed, count(mi.item_id) as member_item_count
from member_items mi
join outfit_items oi on oi.id = mi.item_id
join outfit_item_count oic on oic.outfit_id = oi.outfit_id
group by 1,2,3)z
on z.member_id = tm.id
join outfit_items oi
on oi.outfit_id = z.outfit_id
join outfit o
on o.id = oi.outfit_id
where z.items_needed > z.member_item_count
and oi.id not in (select item_id from member_items)
I'm wondering if it is possible to inner join an inner join with another inner join.
I have a database of 3 tables:
people
athletes
coaches
Every athlete or coach must exist in the people table, but there are some people who are neither coaches nor athletes.
What I am trying to do is find a list of people who are active (meaning play or coach) in at least 3 different sports. The definition of active is they are either coaches, athletes or both a coach and an athlete for that sport.
The person table would consist of (id, name, height)
the athlete table would be (id, sport)
the coaching table would be (id, sport)
I have created 3 inner joins which tell me who is both a coach and and an athlete, who is just a coach and who is just an athlete.
This is done via inner joins.
For example,
1) who is both a coach and an athlete
select
person.id,
person.name,
coach.sport as 'Coaches and plays this sport'
from coach
inner join athlete
on coach.id = athlete.id
and coach.sport = athlete.sport
inner join person
on athlete.id = person.id
That brings up a list of everyone who both coaches and plays the same sport.
2) To find out who only coaches sports, I have used inner joins as below:
select
person.id,
person.name,
coach.sport as 'Coaches this sport'
from coach
inner join person
on coach.id = person.id
3) Then to find out who only plays sports, I've got the same as 2) but just tweaked the words
select
person.id,
person.name,
athlete.sport as 'Plays this sport'
from athlete
inner join person
on athlete.id = person.id
The end result is now I've got:
1) persons who both play and coach the same sport
2) persons who coach a sport
3) persons who play a sport
What I would like to know is how to find a list of people who play or coach at least 3 different sports? I can't figure it out because if someone plays and coaches a sport like hockey in table 1, then I don't want to count them in table 2 and 3.
I tried using these 3 inner joins to make a massive join table so that I could pick the distinct values but it is not working.
Is there an easier way to go about this without making sub-sub-queries?
What I would like to know is how to find a list of people who play /
coach at least 3 different sports? I can't figure it out because if
someone plays and coaches a sport like hockey in table 1, then I don't
want to count them in table 2 and 3.
you can do something like this
select p.id,min(p.name) name
from
person p inner join
(
select id,sport from athlete
union
select id,sport from coach
)
ca
on ca.id=p.id
group by p.id
having count(ca.sport)>2
CREATE TABLE #person (Id INT, Name VARCHAR(50));
CREATE TABLE #athlete (Id INT, Sport VARCHAR(50));
CREATE TABLE #coach (Id INT, Sport VARCHAR(50));
INSERT INTO #person (Id, Name) VALUES(1, 'Bob');
INSERT INTO #person (Id, Name) VALUES(2, 'Carol');
INSERT INTO #person (Id, Name) VALUES(2, 'Sam');
INSERT INTO #athlete (Id, Sport) VALUES(1, 'Golf');
INSERT INTO #athlete (Id, Sport) VALUES(1, 'Football');
INSERT INTO #coach (Id, Sport) VALUES(1, 'Tennis');
INSERT INTO #athlete (Id, Sport) VALUES(2, 'Tennis');
INSERT INTO #coach (Id, Sport) VALUES(2, 'Tennis');
INSERT INTO #athlete (Id, Sport) VALUES(2, 'Swimming');
-- so Bob has 3 sports, Carol has only 2 (she both coaches and plays Tennis)
SELECT p.Id, p.Name
FROM
(
SELECT Id, Sport
FROM #athlete
UNION -- this has an implicit "distinct"
SELECT Id, Sport
FROM #coach
) a
INNER JOIN #person p ON a.Id = p.Id
GROUP BY p.Id, p.Name
HAVING COUNT(*) >= 3
-- returns 1, Bob
I have created a SQL with some test data - should work in your case:
Connecting the two results in the subselect with UNION:
UNION will return just non-duplicate values. So every sport will be just counted once.
Finally just grouping the resultset by person.Person_id and person.name.
Due to the HAVING clause, just persons with 3 or more sports will be returned-
CREATE TABLE person
(
Person_id int
,name varchar(50)
,height int
)
CREATE TABLE coach
(
id int
,sport varchar(50)
)
CREATE TABLE athlete
(
id int
,sport varchar(50)
)
INSERT INTO person VALUES
(1,'John', 130),
(2,'Jack', 150),
(3,'William', 170),
(4,'Averel', 190),
(5,'Lucky Luke', 180),
(6,'Jolly Jumper', 250),
(7,'Rantanplan ', 90)
INSERT INTO coach VALUES
(1,'Football'),
(1,'Hockey'),
(1,'Skiing'),
(2,'Tennis'),
(2,'Curling'),
(4,'Tennis'),
(5,'Volleyball')
INSERT INTO athlete VALUES
(1,'Football'),
(1,'Hockey'),
(2,'Tennis'),
(2,'Volleyball'),
(2,'Hockey'),
(4,'Tennis'),
(5,'Volleyball'),
(3,'Tennis'),
(6,'Volleyball'),
(6,'Tennis'),
(6,'Hockey'),
(6,'Football'),
(6,'Cricket')
SELECT person.Person_id
,person.name
FROM person
INNER JOIN (
SELECT id
,sport
FROM athlete
UNION
SELECT id
,sport
FROM coach
) sports
ON sports.id = person.Person_id
GROUP BY person.Person_id
,person.name
HAVING COUNT(*) >= 3
ORDER BY Person_id
The coaches & athletes, ie people who are coaches or athletes, are relevant to your answer. That is union (rows in one or another), not (inner) join rows in one and another). (Although outer join involves a union, so there is a complicated way to use it here.) But there's no point in getting that by unioning only-coaches, only-athletes & coach-athletes.
Idiomatic is to group & count the union of Athletes & Coaches.
select id
from (select * from Athletes union select * from Coaches) as u
group by id
having COUNT(*) >= 3
Alternatively, you want ids of people who coach or play a 1st sport and coach or play a 2nd sport and coach or play a 3rd sport where the sports are all different.
with u as (select * from Athletes union select * from Coaches)
select u1.id
from u u1
join u u2 on u1.id = u2.id
join u u3 on u2.id = u3.id
where u1.sport <> u2.sport and u2.sport <> u3.sport and u1.sport <> u3.sport
If you wanted names you would join that with People.
Is there any rule of thumb to construct SQL query from a human-readable description?](https://stackoverflow.com/a/33952141/3404097)
I've just started using SQL and I bumped into this problem. There must be a very easy solution I assume. All relations are saved in the relations table. Then there is a parent and a child. The parent being a company and the child being a contact of this company. Every relation has a RelID but the relation_relation table is used to split company and contact.
The problem I'm having is that I can't seem to get the company and the contact into one row like so:
| nameCompany | nameContact |
-----------------------------
|random B.V. | emmma |
|random B.V. | jason |
I have two tables which I want to query. These are simplified versions with a
few example values:
CREATE TABLE relations_relations (parentRelID INT, childRelID INT);
INSERT INTO `relations_relations` VALUES (1, 1);
INSERT INTO `relations_relations` VALUES (1, 2);
INSERT INTO `relations_relations` VALUES (1, 3);
and
CREATE TABLE relations (RelID, nameContact, nameCompany);
INSERT INTO `relations` VALUES (1, NULL, random B.V.);
INSERT INTO `relations` VALUES (2, emma, NULL);
INSERT INTO `relations` VALUES (3, jason, NULL);
You need to JOIN the relation table to itself via the relations_relations table:
SELECT p.nameCompany
,c.nameContact
FROM relations p
INNER JOIN relations_relations rr
ON p.RelID = rr.parentRelID
INNER JOIN relations c
ON c.RelID = rr.childRelID
i figured it out myself.
you're supposed to join two time and use a different condition
both times.
in this problem it should be:
Select *
from Test_Relations_Relations a
inner join Test_relations b
on a.childrelID = b.RelID
inner join Test_relations c
on a.parentrelID = b.relID
I have a table "Cars" and a table "Person". A Person drives many Cars and a Car can be driven by many People so I have another table "Person_Car" which has both id's per row.
Car(id, name)
Person(id, name)
Person_Car(car_id, person_id)
How can I get a list of all people with the cars it drives (car names concatenated), something like this:
("John", "Car 1, Car 2, Car 3")
("Kate", "Car 2, Car 4, Car 5")
Example is here: http://sqlfiddle.com/#!15/ba949/1
Test data:
Create table Car(id int, name text);
Create table Person(id int, name text);
Create table Person_Car(car_id int, person_id int);
INSERT INTO Car VALUES (1, 'Car 1'),
(2, 'Car 2'),
(3, 'Car 3'),
(4, 'Car 4'),
(5, 'Car 5');
INSERT INTO Person VALUES(1, 'John'), (2, 'Kate');
INSERT INTO Person_Car VALUES (1,1), (2,1), (3,1), (2,2), (4,2), (5,2);
Your desired code:
SELECT p.name, array_to_string(array_agg(c.name), ',') FROM Person p
INNER JOIN Person_Car pc ON p.id=pc.person_id
INNER JOIN Car c ON c.id=pc.car_id
GROUP by p.name
Output:
John Car 1,Car 2,Car 3
Kate Car 2,Car 4,Car 5
Just in case you want to avoid the GROUP BY
Option 1
WITH forienKeyTable AS
(
SELECT pc.person_id, c.name
FROM Car c
JOIN Person_Car pc ON pc.car_id = c.id
)
SELECT p.name
, array_to_string
(ARRAY(
SELECT fkt.name
FROM forienKeyTable fkt
WHERE fkt.person_id = p.id
)::text[], ','::text, 'empty'::text)
FROM Person p;
Option 2
SELECT p.name
, array_to_string
(ARRAY(
SELECT c.name
FROM Car c
JOIN Person_Car pc ON pc.car_id = c.id
WHERE pc.person_id = p.id
)::text[], ','::text, 'empty'::text)
FROM Person p;
From sample data below, assuming Julie (1) has friends Adam, David, John (2, 3, 4).
Adam (2) has friends Julie, David, John (1, 3, 4).
ID Name
1 Julie
2 Adam
3 David
4 John
5 Sam
This make a self join and many-to-many relationship within ONE table.
In addition to the above problem, say Julie (1) added Sam (5) as friends, technically and practically speaking, Sam (5) is now friend of Julie (1) as well. This make things more complicated because the relationship bi-directional.
So I'm wondering:
How do I design the database?
How do I make a query that will return all friends of every users?
Thank you!
Example Data:
PEOPLE
PERS_ID PERS_NAME
1 Julie
2 Adam
3 David
4 John
5 Sam
FRIENDSHIPS
PERS_ID FRIEND_ID
1 2
1 3
1 4
2 3
2 4
Query:
select people.pers_id as person,
people.pers_name as person_name,
peoplef.pers_id as friend_id,
peoplef.pers_name as friend_name
from people
join friendships
on people.pers_id = friendships.pers_id
or people.pers_id = friendships.friend_id
join people peoplef
on (peoplef.pers_id = friendships.pers_id and
peoplef.pers_id <> people.pers_id)
or (peoplef.pers_id = friendships.friend_id and
peoplef.pers_id <> people.pers_id)
order by 2, 4
SQL Fiddle demo: http://sqlfiddle.com/#!2/97b41/6/0
This will work regardless of whether or not you record both directions on the friendships table.
Pretty much agree with the others. You need a link table. I'll give a bit more detail.. some examples of keys and indexes and the query you wanted (bi-directional).
CREATE TABLE dbo.tblUser
(
ID int identity(0,1),
name varchar(20)
CONSTRAINT PK_tblUser PRIMARY KEY (ID)
)
-- Many to many link table with FKs
CREATE TABLE dbo.tblFriend
(
ID1 int not null constraint FK_tblUser_ID1 foreign key references dbo.tblUser(ID),
ID2 int not null constraint FK_tblUser_ID2 foreign key references dbo.tblUser(ID)
CONSTRAINT PK_tblFriend PRIMARY KEY (ID1, ID2)
)
-- Add index (So you can get an index seek if using ID2)
CREATE INDEX IX_tblFriend_ID2 ON dbo.tblFriend (ID2)
-- Test data
INSERT INTO dbo.tblUser(name)
VALUES ('Julie'),('Adam'),('David'),('John'),('Sam');
Insert INTO dbo.tblFriend (ID1, ID2)
values(0, 1),(2, 0)
-- Get bi-directional friend to friend relationships
SELECT U1.Name as 'User1', U2.Name as 'User2' FROM dbo.tblFriend F
INNER JOIN dbo.tblUser U1 ON U1.ID = F.ID1
INNER JOIN dbo.tblUser U2 ON U2.ID = F.ID2
UNION
SELECT U2.Name as 'User1', U1.Name as 'User2' FROM dbo.tblFriend F
INNER JOIN dbo.tblUser U1 ON U1.ID = F.ID1
INNER JOIN dbo.tblUser U2 ON U2.ID = F.ID2
ORDER BY User1, User2
One approach could be that you create second table that stores the person and friend ids. In this scenario, consider the following tables.
CREATE TABLE User
(
id int auto_increment primary key,
name varchar(20)
);
CREATE TABLE Friend
(
user_id int ,
friend_id int
);
INSERT INTO User
(name)
VALUES
('Julie'),
('Adam'),
('David'),
('John'),
('Sam');
Insert INTO Friend
(user_id, friend_id)
values(1, 5),
(3, 1);
Now the Friend table will store the user_id and his/her friend_id. For getting the list of friends for a particular user, you can search the id matching in either of these two columns. Below are sample queries.
-- Get Friends of Julie
select 1 AS user_id, IF(user_id = 1, friend_id, user_id) AS friend_id
FROM Friend
WHERE user_id=1 OR friend_id=1;
-- Get Friends of David
select 3 AS user_id, IF(user_id = 3, friend_id, user_id) AS friend_id
FROM Friend
WHERE user_id=3 OR friend_id=3
I hope you get idea with this and can play around.
I tried whatever you written in your query:
declare #table table
(
id int,
name varchar(40)
)
insert into #table values
(1, 'Julie'),
(2, 'Adam'),
(3, 'David'),
(4, 'John'),
(5, 'Sam')
select
t1.name ,
t2.name as friend
from #table t1, #table t2 where t1.id <> t2.id
and t1.id in (1,2) and t2.id <> 5
order by t1.id