mysql using subquery with no join












0















here are the relevant tables



movie(id, title, relYear, category, runTime, director,
studioName, description, rating)

actor(aID, fName, surname, gender)

stars(movieID, actorID)

movGenre(movieID, genre)


I'm just trying to return movies where 'Michael Fassbender' stars using a subquery ... 'id' in movie refers to the the movie id not the actor id if that was the case I could do



SELECT title, category
FROM movie
WHERE 'id' =(SELECT 'aID'
FROM actor
WHERE fName='Michael' and surname='Fassbender')


Is there a way to do it without using any joins?










share|improve this question




















  • 2





    Yes, there are some way to do it without JOINs but why? There are even ways to do it without MySQL, but why?

    – Alex
    Nov 22 '18 at 18:04
















0















here are the relevant tables



movie(id, title, relYear, category, runTime, director,
studioName, description, rating)

actor(aID, fName, surname, gender)

stars(movieID, actorID)

movGenre(movieID, genre)


I'm just trying to return movies where 'Michael Fassbender' stars using a subquery ... 'id' in movie refers to the the movie id not the actor id if that was the case I could do



SELECT title, category
FROM movie
WHERE 'id' =(SELECT 'aID'
FROM actor
WHERE fName='Michael' and surname='Fassbender')


Is there a way to do it without using any joins?










share|improve this question




















  • 2





    Yes, there are some way to do it without JOINs but why? There are even ways to do it without MySQL, but why?

    – Alex
    Nov 22 '18 at 18:04














0












0








0


1






here are the relevant tables



movie(id, title, relYear, category, runTime, director,
studioName, description, rating)

actor(aID, fName, surname, gender)

stars(movieID, actorID)

movGenre(movieID, genre)


I'm just trying to return movies where 'Michael Fassbender' stars using a subquery ... 'id' in movie refers to the the movie id not the actor id if that was the case I could do



SELECT title, category
FROM movie
WHERE 'id' =(SELECT 'aID'
FROM actor
WHERE fName='Michael' and surname='Fassbender')


Is there a way to do it without using any joins?










share|improve this question
















here are the relevant tables



movie(id, title, relYear, category, runTime, director,
studioName, description, rating)

actor(aID, fName, surname, gender)

stars(movieID, actorID)

movGenre(movieID, genre)


I'm just trying to return movies where 'Michael Fassbender' stars using a subquery ... 'id' in movie refers to the the movie id not the actor id if that was the case I could do



SELECT title, category
FROM movie
WHERE 'id' =(SELECT 'aID'
FROM actor
WHERE fName='Michael' and surname='Fassbender')


Is there a way to do it without using any joins?







mysql join subquery






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 17:42









Madhur Bhaiya

19.6k62236




19.6k62236










asked Nov 22 '18 at 17:41









JadeJade

145




145








  • 2





    Yes, there are some way to do it without JOINs but why? There are even ways to do it without MySQL, but why?

    – Alex
    Nov 22 '18 at 18:04














  • 2





    Yes, there are some way to do it without JOINs but why? There are even ways to do it without MySQL, but why?

    – Alex
    Nov 22 '18 at 18:04








2




2





Yes, there are some way to do it without JOINs but why? There are even ways to do it without MySQL, but why?

– Alex
Nov 22 '18 at 18:04





Yes, there are some way to do it without JOINs but why? There are even ways to do it without MySQL, but why?

– Alex
Nov 22 '18 at 18:04












2 Answers
2






active

oldest

votes


















0














You can utilize Correlated Subquery with Exists()



SELECT m.title, m.category
FROM movie AS m
WHERE EXISTS (SELECT 1
FROM stars AS s
JOIN actor AS a ON a.aID = s.actorID
WHERE a.fName = 'Michael' AND
a.surname = 'Fassbender' AND
s.movieID = m.id)




Above solution still utilize JOIN in some way (inside the subquery). A solution utilizing no Join at all can be:



SELECT title, category
FROM movie
WHERE id IN (SELECT movieID
FROM stars
WHERE actorID IN (SELECT aID
FROM actor
WHERE fName = 'Michael' AND
surname = 'Fassbender'))


P.S. Unless this is some trick assignment/HW, I would seriously recommend against using this query in a real-life application.






share|improve this answer

































    0














    SELECT DISTINCT m.title, m.category
    FROM movie
    INNER JOIN stars s
    ON s.movieID = m.id
    INNER JOIN actors a
    ON s.actorID = a.aID
    AND a.fName='Michael'
    AND a.surname='Fassbender'





    share|improve this answer
























    • OP asking for solution without joins :-) "Is there a way to do it without using any joins?"

      – Madhur Bhaiya
      Nov 22 '18 at 17:52













    • :-) @MadhurBhaiya Do you really believe that your solution has no JOINs? do Ctrl+F ;-) You will be surprised. :-)

      – Alex
      Nov 22 '18 at 17:59













    • I know. check the latest update. Do Ctrl + F5 :D

      – Madhur Bhaiya
      Nov 22 '18 at 18:00











    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%2f53436011%2fmysql-using-subquery-with-no-join%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You can utilize Correlated Subquery with Exists()



    SELECT m.title, m.category
    FROM movie AS m
    WHERE EXISTS (SELECT 1
    FROM stars AS s
    JOIN actor AS a ON a.aID = s.actorID
    WHERE a.fName = 'Michael' AND
    a.surname = 'Fassbender' AND
    s.movieID = m.id)




    Above solution still utilize JOIN in some way (inside the subquery). A solution utilizing no Join at all can be:



    SELECT title, category
    FROM movie
    WHERE id IN (SELECT movieID
    FROM stars
    WHERE actorID IN (SELECT aID
    FROM actor
    WHERE fName = 'Michael' AND
    surname = 'Fassbender'))


    P.S. Unless this is some trick assignment/HW, I would seriously recommend against using this query in a real-life application.






    share|improve this answer






























      0














      You can utilize Correlated Subquery with Exists()



      SELECT m.title, m.category
      FROM movie AS m
      WHERE EXISTS (SELECT 1
      FROM stars AS s
      JOIN actor AS a ON a.aID = s.actorID
      WHERE a.fName = 'Michael' AND
      a.surname = 'Fassbender' AND
      s.movieID = m.id)




      Above solution still utilize JOIN in some way (inside the subquery). A solution utilizing no Join at all can be:



      SELECT title, category
      FROM movie
      WHERE id IN (SELECT movieID
      FROM stars
      WHERE actorID IN (SELECT aID
      FROM actor
      WHERE fName = 'Michael' AND
      surname = 'Fassbender'))


      P.S. Unless this is some trick assignment/HW, I would seriously recommend against using this query in a real-life application.






      share|improve this answer




























        0












        0








        0







        You can utilize Correlated Subquery with Exists()



        SELECT m.title, m.category
        FROM movie AS m
        WHERE EXISTS (SELECT 1
        FROM stars AS s
        JOIN actor AS a ON a.aID = s.actorID
        WHERE a.fName = 'Michael' AND
        a.surname = 'Fassbender' AND
        s.movieID = m.id)




        Above solution still utilize JOIN in some way (inside the subquery). A solution utilizing no Join at all can be:



        SELECT title, category
        FROM movie
        WHERE id IN (SELECT movieID
        FROM stars
        WHERE actorID IN (SELECT aID
        FROM actor
        WHERE fName = 'Michael' AND
        surname = 'Fassbender'))


        P.S. Unless this is some trick assignment/HW, I would seriously recommend against using this query in a real-life application.






        share|improve this answer















        You can utilize Correlated Subquery with Exists()



        SELECT m.title, m.category
        FROM movie AS m
        WHERE EXISTS (SELECT 1
        FROM stars AS s
        JOIN actor AS a ON a.aID = s.actorID
        WHERE a.fName = 'Michael' AND
        a.surname = 'Fassbender' AND
        s.movieID = m.id)




        Above solution still utilize JOIN in some way (inside the subquery). A solution utilizing no Join at all can be:



        SELECT title, category
        FROM movie
        WHERE id IN (SELECT movieID
        FROM stars
        WHERE actorID IN (SELECT aID
        FROM actor
        WHERE fName = 'Michael' AND
        surname = 'Fassbender'))


        P.S. Unless this is some trick assignment/HW, I would seriously recommend against using this query in a real-life application.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 22 '18 at 17:59

























        answered Nov 22 '18 at 17:45









        Madhur BhaiyaMadhur Bhaiya

        19.6k62236




        19.6k62236

























            0














            SELECT DISTINCT m.title, m.category
            FROM movie
            INNER JOIN stars s
            ON s.movieID = m.id
            INNER JOIN actors a
            ON s.actorID = a.aID
            AND a.fName='Michael'
            AND a.surname='Fassbender'





            share|improve this answer
























            • OP asking for solution without joins :-) "Is there a way to do it without using any joins?"

              – Madhur Bhaiya
              Nov 22 '18 at 17:52













            • :-) @MadhurBhaiya Do you really believe that your solution has no JOINs? do Ctrl+F ;-) You will be surprised. :-)

              – Alex
              Nov 22 '18 at 17:59













            • I know. check the latest update. Do Ctrl + F5 :D

              – Madhur Bhaiya
              Nov 22 '18 at 18:00
















            0














            SELECT DISTINCT m.title, m.category
            FROM movie
            INNER JOIN stars s
            ON s.movieID = m.id
            INNER JOIN actors a
            ON s.actorID = a.aID
            AND a.fName='Michael'
            AND a.surname='Fassbender'





            share|improve this answer
























            • OP asking for solution without joins :-) "Is there a way to do it without using any joins?"

              – Madhur Bhaiya
              Nov 22 '18 at 17:52













            • :-) @MadhurBhaiya Do you really believe that your solution has no JOINs? do Ctrl+F ;-) You will be surprised. :-)

              – Alex
              Nov 22 '18 at 17:59













            • I know. check the latest update. Do Ctrl + F5 :D

              – Madhur Bhaiya
              Nov 22 '18 at 18:00














            0












            0








            0







            SELECT DISTINCT m.title, m.category
            FROM movie
            INNER JOIN stars s
            ON s.movieID = m.id
            INNER JOIN actors a
            ON s.actorID = a.aID
            AND a.fName='Michael'
            AND a.surname='Fassbender'





            share|improve this answer













            SELECT DISTINCT m.title, m.category
            FROM movie
            INNER JOIN stars s
            ON s.movieID = m.id
            INNER JOIN actors a
            ON s.actorID = a.aID
            AND a.fName='Michael'
            AND a.surname='Fassbender'






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 22 '18 at 17:50









            AlexAlex

            14.5k11938




            14.5k11938













            • OP asking for solution without joins :-) "Is there a way to do it without using any joins?"

              – Madhur Bhaiya
              Nov 22 '18 at 17:52













            • :-) @MadhurBhaiya Do you really believe that your solution has no JOINs? do Ctrl+F ;-) You will be surprised. :-)

              – Alex
              Nov 22 '18 at 17:59













            • I know. check the latest update. Do Ctrl + F5 :D

              – Madhur Bhaiya
              Nov 22 '18 at 18:00



















            • OP asking for solution without joins :-) "Is there a way to do it without using any joins?"

              – Madhur Bhaiya
              Nov 22 '18 at 17:52













            • :-) @MadhurBhaiya Do you really believe that your solution has no JOINs? do Ctrl+F ;-) You will be surprised. :-)

              – Alex
              Nov 22 '18 at 17:59













            • I know. check the latest update. Do Ctrl + F5 :D

              – Madhur Bhaiya
              Nov 22 '18 at 18:00

















            OP asking for solution without joins :-) "Is there a way to do it without using any joins?"

            – Madhur Bhaiya
            Nov 22 '18 at 17:52







            OP asking for solution without joins :-) "Is there a way to do it without using any joins?"

            – Madhur Bhaiya
            Nov 22 '18 at 17:52















            :-) @MadhurBhaiya Do you really believe that your solution has no JOINs? do Ctrl+F ;-) You will be surprised. :-)

            – Alex
            Nov 22 '18 at 17:59







            :-) @MadhurBhaiya Do you really believe that your solution has no JOINs? do Ctrl+F ;-) You will be surprised. :-)

            – Alex
            Nov 22 '18 at 17:59















            I know. check the latest update. Do Ctrl + F5 :D

            – Madhur Bhaiya
            Nov 22 '18 at 18:00





            I know. check the latest update. Do Ctrl + F5 :D

            – Madhur Bhaiya
            Nov 22 '18 at 18:00


















            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%2f53436011%2fmysql-using-subquery-with-no-join%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

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            Origin of the phrase “under your belt”?