MYSQL - Self Join with Junction Table










0















Having trouble with doing a self-join on a homework assignment. The self-join is also pulling from a junction table rec_artist. Here is the code I'm running, and returning an empty set. Why do I return an empty set?



SELECT 
a1.name,
a2.name,
ra.rec_id
FROM
artists a1, artists a2, rec_artist ra1, rec_artist ra2, rec_artist ra
WHERE
ra1.rec_id = ra2.rec_id
AND
ra1.artist_id = a1.id
AND
ra2.artist_id = a2.id
AND
ra1.artist_id > ra2.artist_id;


Table Schema:



recordings
rec_title (varchar)
rec_id (Primary Key)
sales (dec)
genre_id (Foreign Key)

genres
id (primary key)
name (varchar)

artists
id (primary key)
name (varchar)

rec_artist (junction table)
artist_id (primary key)
rec_id (primary key)


The question is:



List pairs of artists that have at least one recording in common.



 Your result set must have 3 columns: both artists' names and the recording_id they have in
common (use only one SQL statement).



 Only unique pairs should be displayed.



 Do not pair an artist with himself/herself.










share|improve this question






















  • Drop the old ansi comma join syntax which is over 25 years old and learn proper JOIN syntax.. dev.mysql.com/doc/refman/8.0/en/join.html ,.. That makes the query alot eazier the read and understand besides no mistakes into running a cartesian product when you didn't define the correct where statement..

    – Raymond Nijland
    Nov 14 '18 at 22:44












  • Okay, I understand that the JOIN syntax makes things clearer and safer. Our teacher has been showing us the comma method so it is hard to break out of as all our examples are done this way.

    – Tony
    Nov 14 '18 at 22:46











  • "Our teacher has been showing us the comma method so it is hard to break out of as all our examples are done this way." Show the teacher the better way and teach the teacher

    – Raymond Nijland
    Nov 14 '18 at 22:47












  • Get rid of rec_artist ra.

    – Barmar
    Nov 14 '18 at 22:48











  • This query should not return an empty result. It should list all the pairs of artists that have a recording in common, and create a cross product with ALL rec_artist.rec_id values.

    – Barmar
    Nov 14 '18 at 22:51















0















Having trouble with doing a self-join on a homework assignment. The self-join is also pulling from a junction table rec_artist. Here is the code I'm running, and returning an empty set. Why do I return an empty set?



SELECT 
a1.name,
a2.name,
ra.rec_id
FROM
artists a1, artists a2, rec_artist ra1, rec_artist ra2, rec_artist ra
WHERE
ra1.rec_id = ra2.rec_id
AND
ra1.artist_id = a1.id
AND
ra2.artist_id = a2.id
AND
ra1.artist_id > ra2.artist_id;


Table Schema:



recordings
rec_title (varchar)
rec_id (Primary Key)
sales (dec)
genre_id (Foreign Key)

genres
id (primary key)
name (varchar)

artists
id (primary key)
name (varchar)

rec_artist (junction table)
artist_id (primary key)
rec_id (primary key)


The question is:



List pairs of artists that have at least one recording in common.



 Your result set must have 3 columns: both artists' names and the recording_id they have in
common (use only one SQL statement).



 Only unique pairs should be displayed.



 Do not pair an artist with himself/herself.










share|improve this question






















  • Drop the old ansi comma join syntax which is over 25 years old and learn proper JOIN syntax.. dev.mysql.com/doc/refman/8.0/en/join.html ,.. That makes the query alot eazier the read and understand besides no mistakes into running a cartesian product when you didn't define the correct where statement..

    – Raymond Nijland
    Nov 14 '18 at 22:44












  • Okay, I understand that the JOIN syntax makes things clearer and safer. Our teacher has been showing us the comma method so it is hard to break out of as all our examples are done this way.

    – Tony
    Nov 14 '18 at 22:46











  • "Our teacher has been showing us the comma method so it is hard to break out of as all our examples are done this way." Show the teacher the better way and teach the teacher

    – Raymond Nijland
    Nov 14 '18 at 22:47












  • Get rid of rec_artist ra.

    – Barmar
    Nov 14 '18 at 22:48











  • This query should not return an empty result. It should list all the pairs of artists that have a recording in common, and create a cross product with ALL rec_artist.rec_id values.

    – Barmar
    Nov 14 '18 at 22:51













0












0








0








Having trouble with doing a self-join on a homework assignment. The self-join is also pulling from a junction table rec_artist. Here is the code I'm running, and returning an empty set. Why do I return an empty set?



SELECT 
a1.name,
a2.name,
ra.rec_id
FROM
artists a1, artists a2, rec_artist ra1, rec_artist ra2, rec_artist ra
WHERE
ra1.rec_id = ra2.rec_id
AND
ra1.artist_id = a1.id
AND
ra2.artist_id = a2.id
AND
ra1.artist_id > ra2.artist_id;


Table Schema:



recordings
rec_title (varchar)
rec_id (Primary Key)
sales (dec)
genre_id (Foreign Key)

genres
id (primary key)
name (varchar)

artists
id (primary key)
name (varchar)

rec_artist (junction table)
artist_id (primary key)
rec_id (primary key)


The question is:



List pairs of artists that have at least one recording in common.



 Your result set must have 3 columns: both artists' names and the recording_id they have in
common (use only one SQL statement).



 Only unique pairs should be displayed.



 Do not pair an artist with himself/herself.










share|improve this question














Having trouble with doing a self-join on a homework assignment. The self-join is also pulling from a junction table rec_artist. Here is the code I'm running, and returning an empty set. Why do I return an empty set?



SELECT 
a1.name,
a2.name,
ra.rec_id
FROM
artists a1, artists a2, rec_artist ra1, rec_artist ra2, rec_artist ra
WHERE
ra1.rec_id = ra2.rec_id
AND
ra1.artist_id = a1.id
AND
ra2.artist_id = a2.id
AND
ra1.artist_id > ra2.artist_id;


Table Schema:



recordings
rec_title (varchar)
rec_id (Primary Key)
sales (dec)
genre_id (Foreign Key)

genres
id (primary key)
name (varchar)

artists
id (primary key)
name (varchar)

rec_artist (junction table)
artist_id (primary key)
rec_id (primary key)


The question is:



List pairs of artists that have at least one recording in common.



 Your result set must have 3 columns: both artists' names and the recording_id they have in
common (use only one SQL statement).



 Only unique pairs should be displayed.



 Do not pair an artist with himself/herself.







mysql






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 22:38









Tony Tony

103




103












  • Drop the old ansi comma join syntax which is over 25 years old and learn proper JOIN syntax.. dev.mysql.com/doc/refman/8.0/en/join.html ,.. That makes the query alot eazier the read and understand besides no mistakes into running a cartesian product when you didn't define the correct where statement..

    – Raymond Nijland
    Nov 14 '18 at 22:44












  • Okay, I understand that the JOIN syntax makes things clearer and safer. Our teacher has been showing us the comma method so it is hard to break out of as all our examples are done this way.

    – Tony
    Nov 14 '18 at 22:46











  • "Our teacher has been showing us the comma method so it is hard to break out of as all our examples are done this way." Show the teacher the better way and teach the teacher

    – Raymond Nijland
    Nov 14 '18 at 22:47












  • Get rid of rec_artist ra.

    – Barmar
    Nov 14 '18 at 22:48











  • This query should not return an empty result. It should list all the pairs of artists that have a recording in common, and create a cross product with ALL rec_artist.rec_id values.

    – Barmar
    Nov 14 '18 at 22:51

















  • Drop the old ansi comma join syntax which is over 25 years old and learn proper JOIN syntax.. dev.mysql.com/doc/refman/8.0/en/join.html ,.. That makes the query alot eazier the read and understand besides no mistakes into running a cartesian product when you didn't define the correct where statement..

    – Raymond Nijland
    Nov 14 '18 at 22:44












  • Okay, I understand that the JOIN syntax makes things clearer and safer. Our teacher has been showing us the comma method so it is hard to break out of as all our examples are done this way.

    – Tony
    Nov 14 '18 at 22:46











  • "Our teacher has been showing us the comma method so it is hard to break out of as all our examples are done this way." Show the teacher the better way and teach the teacher

    – Raymond Nijland
    Nov 14 '18 at 22:47












  • Get rid of rec_artist ra.

    – Barmar
    Nov 14 '18 at 22:48











  • This query should not return an empty result. It should list all the pairs of artists that have a recording in common, and create a cross product with ALL rec_artist.rec_id values.

    – Barmar
    Nov 14 '18 at 22:51
















Drop the old ansi comma join syntax which is over 25 years old and learn proper JOIN syntax.. dev.mysql.com/doc/refman/8.0/en/join.html ,.. That makes the query alot eazier the read and understand besides no mistakes into running a cartesian product when you didn't define the correct where statement..

– Raymond Nijland
Nov 14 '18 at 22:44






Drop the old ansi comma join syntax which is over 25 years old and learn proper JOIN syntax.. dev.mysql.com/doc/refman/8.0/en/join.html ,.. That makes the query alot eazier the read and understand besides no mistakes into running a cartesian product when you didn't define the correct where statement..

– Raymond Nijland
Nov 14 '18 at 22:44














Okay, I understand that the JOIN syntax makes things clearer and safer. Our teacher has been showing us the comma method so it is hard to break out of as all our examples are done this way.

– Tony
Nov 14 '18 at 22:46





Okay, I understand that the JOIN syntax makes things clearer and safer. Our teacher has been showing us the comma method so it is hard to break out of as all our examples are done this way.

– Tony
Nov 14 '18 at 22:46













"Our teacher has been showing us the comma method so it is hard to break out of as all our examples are done this way." Show the teacher the better way and teach the teacher

– Raymond Nijland
Nov 14 '18 at 22:47






"Our teacher has been showing us the comma method so it is hard to break out of as all our examples are done this way." Show the teacher the better way and teach the teacher

– Raymond Nijland
Nov 14 '18 at 22:47














Get rid of rec_artist ra.

– Barmar
Nov 14 '18 at 22:48





Get rid of rec_artist ra.

– Barmar
Nov 14 '18 at 22:48













This query should not return an empty result. It should list all the pairs of artists that have a recording in common, and create a cross product with ALL rec_artist.rec_id values.

– Barmar
Nov 14 '18 at 22:51





This query should not return an empty result. It should list all the pairs of artists that have a recording in common, and create a cross product with ALL rec_artist.rec_id values.

– Barmar
Nov 14 '18 at 22:51












1 Answer
1






active

oldest

votes


















1














This query should not return an empty result. It should actually return a huge result. You're correctly finding the pairs of artists that have recordings in common, but each pair will then be duplicated with every rec_id in the rec_artists table, because you have no conditions on the rec_artists ra table in the WHERE clause.



You don't need a third join with rec_artists. You can simply select the rec_id column from either of the rec_artists tables in the self-join; it doesn't matter which you use, since ra1.rec_id = ra2.rec_id.



The correct query is:



SELECT a1.name, a2.name, ra1.rec_id
FROM artists a1, artists a2, rec_artist ra1, rec_artist ra2
WHERE ra1.rec_id = ra2.rec_id
AND ra1.artist_id = a1.id
AND ra2.artist_id = a2.id
AND ra1.artist_id > ra2.artist_id;


Or using ANSI JOIN syntax:



SELECT a1.name, a2.name, ra1.rec_id
FROM rec_artist AS ra1
JOIN rec_artist AS ra2 ON ra1.rec_id = ra2.rec_id AND ra1.artist_id > ra2.artist_id
JOIN artists AS a1 ON ra1.artist_id = a1.id
JOIN artists AS a2 ON ra2.artist_id = a2.id


DEMO






share|improve this answer

























  • Really appreciate your help! I am still returning an empty set with both of these queries.

    – Tony
    Nov 14 '18 at 23:05











  • Then you must not have any artists who share a recording. I've added a demo showing that it works.

    – Barmar
    Nov 14 '18 at 23:06











  • That may be the case... it shouldn't be, but I see that it works correctly in your demo, so I'll just have to submit the query and results I get. Thank you again, sir!

    – Tony
    Nov 14 '18 at 23:11











  • Yeah, looks like it. It's a great site, but kind of flaky.

    – Barmar
    Nov 14 '18 at 23:11










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53309778%2fmysql-self-join-with-junction-table%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














This query should not return an empty result. It should actually return a huge result. You're correctly finding the pairs of artists that have recordings in common, but each pair will then be duplicated with every rec_id in the rec_artists table, because you have no conditions on the rec_artists ra table in the WHERE clause.



You don't need a third join with rec_artists. You can simply select the rec_id column from either of the rec_artists tables in the self-join; it doesn't matter which you use, since ra1.rec_id = ra2.rec_id.



The correct query is:



SELECT a1.name, a2.name, ra1.rec_id
FROM artists a1, artists a2, rec_artist ra1, rec_artist ra2
WHERE ra1.rec_id = ra2.rec_id
AND ra1.artist_id = a1.id
AND ra2.artist_id = a2.id
AND ra1.artist_id > ra2.artist_id;


Or using ANSI JOIN syntax:



SELECT a1.name, a2.name, ra1.rec_id
FROM rec_artist AS ra1
JOIN rec_artist AS ra2 ON ra1.rec_id = ra2.rec_id AND ra1.artist_id > ra2.artist_id
JOIN artists AS a1 ON ra1.artist_id = a1.id
JOIN artists AS a2 ON ra2.artist_id = a2.id


DEMO






share|improve this answer

























  • Really appreciate your help! I am still returning an empty set with both of these queries.

    – Tony
    Nov 14 '18 at 23:05











  • Then you must not have any artists who share a recording. I've added a demo showing that it works.

    – Barmar
    Nov 14 '18 at 23:06











  • That may be the case... it shouldn't be, but I see that it works correctly in your demo, so I'll just have to submit the query and results I get. Thank you again, sir!

    – Tony
    Nov 14 '18 at 23:11











  • Yeah, looks like it. It's a great site, but kind of flaky.

    – Barmar
    Nov 14 '18 at 23:11















1














This query should not return an empty result. It should actually return a huge result. You're correctly finding the pairs of artists that have recordings in common, but each pair will then be duplicated with every rec_id in the rec_artists table, because you have no conditions on the rec_artists ra table in the WHERE clause.



You don't need a third join with rec_artists. You can simply select the rec_id column from either of the rec_artists tables in the self-join; it doesn't matter which you use, since ra1.rec_id = ra2.rec_id.



The correct query is:



SELECT a1.name, a2.name, ra1.rec_id
FROM artists a1, artists a2, rec_artist ra1, rec_artist ra2
WHERE ra1.rec_id = ra2.rec_id
AND ra1.artist_id = a1.id
AND ra2.artist_id = a2.id
AND ra1.artist_id > ra2.artist_id;


Or using ANSI JOIN syntax:



SELECT a1.name, a2.name, ra1.rec_id
FROM rec_artist AS ra1
JOIN rec_artist AS ra2 ON ra1.rec_id = ra2.rec_id AND ra1.artist_id > ra2.artist_id
JOIN artists AS a1 ON ra1.artist_id = a1.id
JOIN artists AS a2 ON ra2.artist_id = a2.id


DEMO






share|improve this answer

























  • Really appreciate your help! I am still returning an empty set with both of these queries.

    – Tony
    Nov 14 '18 at 23:05











  • Then you must not have any artists who share a recording. I've added a demo showing that it works.

    – Barmar
    Nov 14 '18 at 23:06











  • That may be the case... it shouldn't be, but I see that it works correctly in your demo, so I'll just have to submit the query and results I get. Thank you again, sir!

    – Tony
    Nov 14 '18 at 23:11











  • Yeah, looks like it. It's a great site, but kind of flaky.

    – Barmar
    Nov 14 '18 at 23:11













1












1








1







This query should not return an empty result. It should actually return a huge result. You're correctly finding the pairs of artists that have recordings in common, but each pair will then be duplicated with every rec_id in the rec_artists table, because you have no conditions on the rec_artists ra table in the WHERE clause.



You don't need a third join with rec_artists. You can simply select the rec_id column from either of the rec_artists tables in the self-join; it doesn't matter which you use, since ra1.rec_id = ra2.rec_id.



The correct query is:



SELECT a1.name, a2.name, ra1.rec_id
FROM artists a1, artists a2, rec_artist ra1, rec_artist ra2
WHERE ra1.rec_id = ra2.rec_id
AND ra1.artist_id = a1.id
AND ra2.artist_id = a2.id
AND ra1.artist_id > ra2.artist_id;


Or using ANSI JOIN syntax:



SELECT a1.name, a2.name, ra1.rec_id
FROM rec_artist AS ra1
JOIN rec_artist AS ra2 ON ra1.rec_id = ra2.rec_id AND ra1.artist_id > ra2.artist_id
JOIN artists AS a1 ON ra1.artist_id = a1.id
JOIN artists AS a2 ON ra2.artist_id = a2.id


DEMO






share|improve this answer















This query should not return an empty result. It should actually return a huge result. You're correctly finding the pairs of artists that have recordings in common, but each pair will then be duplicated with every rec_id in the rec_artists table, because you have no conditions on the rec_artists ra table in the WHERE clause.



You don't need a third join with rec_artists. You can simply select the rec_id column from either of the rec_artists tables in the self-join; it doesn't matter which you use, since ra1.rec_id = ra2.rec_id.



The correct query is:



SELECT a1.name, a2.name, ra1.rec_id
FROM artists a1, artists a2, rec_artist ra1, rec_artist ra2
WHERE ra1.rec_id = ra2.rec_id
AND ra1.artist_id = a1.id
AND ra2.artist_id = a2.id
AND ra1.artist_id > ra2.artist_id;


Or using ANSI JOIN syntax:



SELECT a1.name, a2.name, ra1.rec_id
FROM rec_artist AS ra1
JOIN rec_artist AS ra2 ON ra1.rec_id = ra2.rec_id AND ra1.artist_id > ra2.artist_id
JOIN artists AS a1 ON ra1.artist_id = a1.id
JOIN artists AS a2 ON ra2.artist_id = a2.id


DEMO







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 23:06

























answered Nov 14 '18 at 22:59









BarmarBarmar

427k36250352




427k36250352












  • Really appreciate your help! I am still returning an empty set with both of these queries.

    – Tony
    Nov 14 '18 at 23:05











  • Then you must not have any artists who share a recording. I've added a demo showing that it works.

    – Barmar
    Nov 14 '18 at 23:06











  • That may be the case... it shouldn't be, but I see that it works correctly in your demo, so I'll just have to submit the query and results I get. Thank you again, sir!

    – Tony
    Nov 14 '18 at 23:11











  • Yeah, looks like it. It's a great site, but kind of flaky.

    – Barmar
    Nov 14 '18 at 23:11

















  • Really appreciate your help! I am still returning an empty set with both of these queries.

    – Tony
    Nov 14 '18 at 23:05











  • Then you must not have any artists who share a recording. I've added a demo showing that it works.

    – Barmar
    Nov 14 '18 at 23:06











  • That may be the case... it shouldn't be, but I see that it works correctly in your demo, so I'll just have to submit the query and results I get. Thank you again, sir!

    – Tony
    Nov 14 '18 at 23:11











  • Yeah, looks like it. It's a great site, but kind of flaky.

    – Barmar
    Nov 14 '18 at 23:11
















Really appreciate your help! I am still returning an empty set with both of these queries.

– Tony
Nov 14 '18 at 23:05





Really appreciate your help! I am still returning an empty set with both of these queries.

– Tony
Nov 14 '18 at 23:05













Then you must not have any artists who share a recording. I've added a demo showing that it works.

– Barmar
Nov 14 '18 at 23:06





Then you must not have any artists who share a recording. I've added a demo showing that it works.

– Barmar
Nov 14 '18 at 23:06













That may be the case... it shouldn't be, but I see that it works correctly in your demo, so I'll just have to submit the query and results I get. Thank you again, sir!

– Tony
Nov 14 '18 at 23:11





That may be the case... it shouldn't be, but I see that it works correctly in your demo, so I'll just have to submit the query and results I get. Thank you again, sir!

– Tony
Nov 14 '18 at 23:11













Yeah, looks like it. It's a great site, but kind of flaky.

– Barmar
Nov 14 '18 at 23:11





Yeah, looks like it. It's a great site, but kind of flaky.

– Barmar
Nov 14 '18 at 23:11



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53309778%2fmysql-self-join-with-junction-table%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線