Find authors who ONLY wrote history books










0















New to SQL, although catching on - stuck on this query.
This works, although Paddy O'Furniture should not be showing up as this author hasn't written any book and his au_id does not appear in the title_authors table. Please help.



Find authors who ONLY wrote history books



select a.au_id, a.au_lname, a.au_fname

from authors a

where not exists(

select ta.au_id

from title_authors ta

join titles t

on t.title_id = ta.title_id

where t.type != 'history'

and a.au_id = ta.au_id

)


Output:



A01 Buchman Sarah



A07 O'Furniture Paddy










share|improve this question


























    0















    New to SQL, although catching on - stuck on this query.
    This works, although Paddy O'Furniture should not be showing up as this author hasn't written any book and his au_id does not appear in the title_authors table. Please help.



    Find authors who ONLY wrote history books



    select a.au_id, a.au_lname, a.au_fname

    from authors a

    where not exists(

    select ta.au_id

    from title_authors ta

    join titles t

    on t.title_id = ta.title_id

    where t.type != 'history'

    and a.au_id = ta.au_id

    )


    Output:



    A01 Buchman Sarah



    A07 O'Furniture Paddy










    share|improve this question
























      0












      0








      0








      New to SQL, although catching on - stuck on this query.
      This works, although Paddy O'Furniture should not be showing up as this author hasn't written any book and his au_id does not appear in the title_authors table. Please help.



      Find authors who ONLY wrote history books



      select a.au_id, a.au_lname, a.au_fname

      from authors a

      where not exists(

      select ta.au_id

      from title_authors ta

      join titles t

      on t.title_id = ta.title_id

      where t.type != 'history'

      and a.au_id = ta.au_id

      )


      Output:



      A01 Buchman Sarah



      A07 O'Furniture Paddy










      share|improve this question














      New to SQL, although catching on - stuck on this query.
      This works, although Paddy O'Furniture should not be showing up as this author hasn't written any book and his au_id does not appear in the title_authors table. Please help.



      Find authors who ONLY wrote history books



      select a.au_id, a.au_lname, a.au_fname

      from authors a

      where not exists(

      select ta.au_id

      from title_authors ta

      join titles t

      on t.title_id = ta.title_id

      where t.type != 'history'

      and a.au_id = ta.au_id

      )


      Output:



      A01 Buchman Sarah



      A07 O'Furniture Paddy







      sql






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 21:53









      Jozef_Jozef_

      51




      51






















          3 Answers
          3






          active

          oldest

          votes


















          0














          While your approach can work if you use another exists to make sure the author wrote at least a history book, here's another approach using conditional aggregation:



          select a.au_id, a.au_lname, a.au_fname
          from authors a
          join title_authors ta on a.au_id = ta.au_id
          join titles t on ta.title_id = t.title_id
          group by a.au_id, a.au_lname, a.au_fname
          having sum(case when t.type != 'history' then 1 else 0 end) = 0


          Online Demo






          share|improve this answer























          • Thank you for your help.

            – Jozef_
            Nov 14 '18 at 21:25


















          1














          Paddy O'Furniture is in your result because no matching rows were discovered in the correlated subquery. i.e. no rows exist for that author and hence where not exists is true.



          select a.au_id, a.au_lname, a.au_fname
          from authors a
          inner join title_authors ta ON a.au_id = ta.au_id
          inner join titles t on ta.title_id = t.title_id
          group by a.au_id, a.au_lname, a.au_fname
          having count(case when t.type <> 'history' then 1 end) = 0


          The approach above uses a case expression within a count() function so that if any books have a non history type this count will be greater than zero. The having clause enables using aggregated values to filter the final result (having is used is AFTER the group by clause, and is not a substitute for a where clause).






          share|improve this answer

























          • Thank you. This explanation helps get me started. I have a lot to learn.

            – Jozef_
            Nov 14 '18 at 21:24



















          0














          You are close. Adding JOIN to title_authors on the outer query will filter out authors who have not written a book.



          select a.au_id, a.au_lname, a.au_fname
          from authors a
          join title_authors ta1 on ta1.au_id = a.au_id
          where not exists(
          select 1
          from title_authors ta
          join titles t on t.title_id = ta.title_id
          where t.type != 'history' and ta1.id = ta.id
          )


          The title_authors in the inner query can actually be removed.



          select a.au_id, a.au_lname, a.au_fname
          from authors a
          join title_authors ta on ta.au_id = a.au_id
          where not exists(
          select 1
          from titles
          where t.type != 'history' and title_id = ta.title_id
          )





          share|improve this answer























          • I like this one, however I do not understand the 'select 1' - maybe I'm not far enough in my course to understand this.

            – Jozef_
            Nov 14 '18 at 21:24











          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%2f53290076%2ffind-authors-who-only-wrote-history-books%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          While your approach can work if you use another exists to make sure the author wrote at least a history book, here's another approach using conditional aggregation:



          select a.au_id, a.au_lname, a.au_fname
          from authors a
          join title_authors ta on a.au_id = ta.au_id
          join titles t on ta.title_id = t.title_id
          group by a.au_id, a.au_lname, a.au_fname
          having sum(case when t.type != 'history' then 1 else 0 end) = 0


          Online Demo






          share|improve this answer























          • Thank you for your help.

            – Jozef_
            Nov 14 '18 at 21:25















          0














          While your approach can work if you use another exists to make sure the author wrote at least a history book, here's another approach using conditional aggregation:



          select a.au_id, a.au_lname, a.au_fname
          from authors a
          join title_authors ta on a.au_id = ta.au_id
          join titles t on ta.title_id = t.title_id
          group by a.au_id, a.au_lname, a.au_fname
          having sum(case when t.type != 'history' then 1 else 0 end) = 0


          Online Demo






          share|improve this answer























          • Thank you for your help.

            – Jozef_
            Nov 14 '18 at 21:25













          0












          0








          0







          While your approach can work if you use another exists to make sure the author wrote at least a history book, here's another approach using conditional aggregation:



          select a.au_id, a.au_lname, a.au_fname
          from authors a
          join title_authors ta on a.au_id = ta.au_id
          join titles t on ta.title_id = t.title_id
          group by a.au_id, a.au_lname, a.au_fname
          having sum(case when t.type != 'history' then 1 else 0 end) = 0


          Online Demo






          share|improve this answer













          While your approach can work if you use another exists to make sure the author wrote at least a history book, here's another approach using conditional aggregation:



          select a.au_id, a.au_lname, a.au_fname
          from authors a
          join title_authors ta on a.au_id = ta.au_id
          join titles t on ta.title_id = t.title_id
          group by a.au_id, a.au_lname, a.au_fname
          having sum(case when t.type != 'history' then 1 else 0 end) = 0


          Online Demo







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 22:11









          sgeddessgeddes

          56.7k53963




          56.7k53963












          • Thank you for your help.

            – Jozef_
            Nov 14 '18 at 21:25

















          • Thank you for your help.

            – Jozef_
            Nov 14 '18 at 21:25
















          Thank you for your help.

          – Jozef_
          Nov 14 '18 at 21:25





          Thank you for your help.

          – Jozef_
          Nov 14 '18 at 21:25













          1














          Paddy O'Furniture is in your result because no matching rows were discovered in the correlated subquery. i.e. no rows exist for that author and hence where not exists is true.



          select a.au_id, a.au_lname, a.au_fname
          from authors a
          inner join title_authors ta ON a.au_id = ta.au_id
          inner join titles t on ta.title_id = t.title_id
          group by a.au_id, a.au_lname, a.au_fname
          having count(case when t.type <> 'history' then 1 end) = 0


          The approach above uses a case expression within a count() function so that if any books have a non history type this count will be greater than zero. The having clause enables using aggregated values to filter the final result (having is used is AFTER the group by clause, and is not a substitute for a where clause).






          share|improve this answer

























          • Thank you. This explanation helps get me started. I have a lot to learn.

            – Jozef_
            Nov 14 '18 at 21:24
















          1














          Paddy O'Furniture is in your result because no matching rows were discovered in the correlated subquery. i.e. no rows exist for that author and hence where not exists is true.



          select a.au_id, a.au_lname, a.au_fname
          from authors a
          inner join title_authors ta ON a.au_id = ta.au_id
          inner join titles t on ta.title_id = t.title_id
          group by a.au_id, a.au_lname, a.au_fname
          having count(case when t.type <> 'history' then 1 end) = 0


          The approach above uses a case expression within a count() function so that if any books have a non history type this count will be greater than zero. The having clause enables using aggregated values to filter the final result (having is used is AFTER the group by clause, and is not a substitute for a where clause).






          share|improve this answer

























          • Thank you. This explanation helps get me started. I have a lot to learn.

            – Jozef_
            Nov 14 '18 at 21:24














          1












          1








          1







          Paddy O'Furniture is in your result because no matching rows were discovered in the correlated subquery. i.e. no rows exist for that author and hence where not exists is true.



          select a.au_id, a.au_lname, a.au_fname
          from authors a
          inner join title_authors ta ON a.au_id = ta.au_id
          inner join titles t on ta.title_id = t.title_id
          group by a.au_id, a.au_lname, a.au_fname
          having count(case when t.type <> 'history' then 1 end) = 0


          The approach above uses a case expression within a count() function so that if any books have a non history type this count will be greater than zero. The having clause enables using aggregated values to filter the final result (having is used is AFTER the group by clause, and is not a substitute for a where clause).






          share|improve this answer















          Paddy O'Furniture is in your result because no matching rows were discovered in the correlated subquery. i.e. no rows exist for that author and hence where not exists is true.



          select a.au_id, a.au_lname, a.au_fname
          from authors a
          inner join title_authors ta ON a.au_id = ta.au_id
          inner join titles t on ta.title_id = t.title_id
          group by a.au_id, a.au_lname, a.au_fname
          having count(case when t.type <> 'history' then 1 end) = 0


          The approach above uses a case expression within a count() function so that if any books have a non history type this count will be greater than zero. The having clause enables using aggregated values to filter the final result (having is used is AFTER the group by clause, and is not a substitute for a where clause).







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 22:12

























          answered Nov 13 '18 at 22:03









          Used_By_AlreadyUsed_By_Already

          23k22038




          23k22038












          • Thank you. This explanation helps get me started. I have a lot to learn.

            – Jozef_
            Nov 14 '18 at 21:24


















          • Thank you. This explanation helps get me started. I have a lot to learn.

            – Jozef_
            Nov 14 '18 at 21:24

















          Thank you. This explanation helps get me started. I have a lot to learn.

          – Jozef_
          Nov 14 '18 at 21:24






          Thank you. This explanation helps get me started. I have a lot to learn.

          – Jozef_
          Nov 14 '18 at 21:24












          0














          You are close. Adding JOIN to title_authors on the outer query will filter out authors who have not written a book.



          select a.au_id, a.au_lname, a.au_fname
          from authors a
          join title_authors ta1 on ta1.au_id = a.au_id
          where not exists(
          select 1
          from title_authors ta
          join titles t on t.title_id = ta.title_id
          where t.type != 'history' and ta1.id = ta.id
          )


          The title_authors in the inner query can actually be removed.



          select a.au_id, a.au_lname, a.au_fname
          from authors a
          join title_authors ta on ta.au_id = a.au_id
          where not exists(
          select 1
          from titles
          where t.type != 'history' and title_id = ta.title_id
          )





          share|improve this answer























          • I like this one, however I do not understand the 'select 1' - maybe I'm not far enough in my course to understand this.

            – Jozef_
            Nov 14 '18 at 21:24
















          0














          You are close. Adding JOIN to title_authors on the outer query will filter out authors who have not written a book.



          select a.au_id, a.au_lname, a.au_fname
          from authors a
          join title_authors ta1 on ta1.au_id = a.au_id
          where not exists(
          select 1
          from title_authors ta
          join titles t on t.title_id = ta.title_id
          where t.type != 'history' and ta1.id = ta.id
          )


          The title_authors in the inner query can actually be removed.



          select a.au_id, a.au_lname, a.au_fname
          from authors a
          join title_authors ta on ta.au_id = a.au_id
          where not exists(
          select 1
          from titles
          where t.type != 'history' and title_id = ta.title_id
          )





          share|improve this answer























          • I like this one, however I do not understand the 'select 1' - maybe I'm not far enough in my course to understand this.

            – Jozef_
            Nov 14 '18 at 21:24














          0












          0








          0







          You are close. Adding JOIN to title_authors on the outer query will filter out authors who have not written a book.



          select a.au_id, a.au_lname, a.au_fname
          from authors a
          join title_authors ta1 on ta1.au_id = a.au_id
          where not exists(
          select 1
          from title_authors ta
          join titles t on t.title_id = ta.title_id
          where t.type != 'history' and ta1.id = ta.id
          )


          The title_authors in the inner query can actually be removed.



          select a.au_id, a.au_lname, a.au_fname
          from authors a
          join title_authors ta on ta.au_id = a.au_id
          where not exists(
          select 1
          from titles
          where t.type != 'history' and title_id = ta.title_id
          )





          share|improve this answer













          You are close. Adding JOIN to title_authors on the outer query will filter out authors who have not written a book.



          select a.au_id, a.au_lname, a.au_fname
          from authors a
          join title_authors ta1 on ta1.au_id = a.au_id
          where not exists(
          select 1
          from title_authors ta
          join titles t on t.title_id = ta.title_id
          where t.type != 'history' and ta1.id = ta.id
          )


          The title_authors in the inner query can actually be removed.



          select a.au_id, a.au_lname, a.au_fname
          from authors a
          join title_authors ta on ta.au_id = a.au_id
          where not exists(
          select 1
          from titles
          where t.type != 'history' and title_id = ta.title_id
          )






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 22:11









          EricEric

          1,72211115




          1,72211115












          • I like this one, however I do not understand the 'select 1' - maybe I'm not far enough in my course to understand this.

            – Jozef_
            Nov 14 '18 at 21:24


















          • I like this one, however I do not understand the 'select 1' - maybe I'm not far enough in my course to understand this.

            – Jozef_
            Nov 14 '18 at 21:24

















          I like this one, however I do not understand the 'select 1' - maybe I'm not far enough in my course to understand this.

          – Jozef_
          Nov 14 '18 at 21:24






          I like this one, however I do not understand the 'select 1' - maybe I'm not far enough in my course to understand this.

          – Jozef_
          Nov 14 '18 at 21:24


















          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%2f53290076%2ffind-authors-who-only-wrote-history-books%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

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo