Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2010
    Posts
    3

    Unanswered: Newbie help - UNION operator

    Hey guys, working on a simple query here, but I'm not getting the results I expect. Here is the exact question I am to work:

    Find the book code and book title for each book whose price is more than $10 or that was published in Boston.
    Here is what I came up with:

    Code:
    SELECT B.BookCode, B.Title
    FROM Book B
    WHERE B.Price > '10'
    UNION
    SELECT B.BookCode, B.Title
    FROM Book B, Publisher P
    WHERE B.PublisherCode = P.PublisherCode;
    However, this returns all the Books, even though I shouldn't. What did I do wrong? The chapter I am studying is specifically going over the SET operators (UNION, INTERSECT & MINUS), and the ANY and ALL operators. I figured UNION was closest to the example in the book, so thats why I choose that.

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    your UNION approach was very good, but it was incomplete

    in the second of the SELECTs, you did nothing to restrict the rows to those books published in boston
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Apr 2010
    Posts
    3
    whoa, whoops.

    Round 2:

    Code:
    SELECT B.BookCode, B.Title, price
    FROM Book B
    WHERE B.Price > '10'
    UNION
    SELECT B.BookCode, B.Title, price
    FROM Book B, Publisher P
    WHERE P.City = 'Boston'
    AND B.PublisherCode = P.PublisherCode;
    However, this code only displays books with prices higher than 10, it doesn't include the Boston books.

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by harofreak00 View Post
    ... it doesn't include the Boston books.
    oh yes it does

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  5. #5
    Join Date
    Apr 2010
    Posts
    3
    I guess its kind of a stupid question, because after further looking, ALL of the Boston books (only 2 of them), are both more then $10. I guess I have found the solution! Thanks for the help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •