Can a different SubQuery replace a Set function Minus?

I'm a student in an Oracle SQL class using 10g. We are nearing the end of our class.
We are working on a chapter on Subqueries.
In one of the homework problems I have a solution that uses a Subquery.
But it also uses a Minus. We studied the Set functions earlier. So my solution
does use things we have already studied. I'm just wondering if the whole
problem could be solved with a different use of Subqueries.
And eliminating the use of the Minus.
Here is the question - 'List the title of all books in the same category as books previously
purchased by customer 1007. Do not include books already purchased by this customer.'
And here is my solution:
SELECT InitCap(Title) AS "Book Title",
Category
FROM Books
WHERE Category IN
( SELECT DISTINCT(Category)
FROM Books JOIN OrderItems USING (ISBN)
JOIN Orders USING (Order#)
JOIN Customers USING (Customer#)
WHERE Customer# = 1007 )
Minus
SELECT InitCap(Title),
Category
FROM Books JOIN OrderItems USING (ISBN)
JOIN Orders USING (Order#)
JOIN Customers USING (Customer#)
WHERE Customer# = 1007
ORDER BY Category;
There is nothing tricky about the tables.
Customers has Customer# which Joins to Orders via the Customer#.
Orders Joins to a table called OrderItems via the Order#.
OrderItems is also joined to Books via the ISBN.
So to get the details of an Order (like which specific books and quantities)
we have to get to Orderitems which has the ISBN and the Quantity.
But every time I go back and look at this question I keep seeing the answer as
'Find the large group, take out the part we don't need, leaving the answer'.
Well, I hope I gave enough explanation here.
Thanks for any thoughts or advice.

Hi,
To understand the problem better, let's do the join that finds the given customer's books only once, by putting it in a WITH clause. That has the additional advantage of keeping it away from the rest of the query.
Since you understand the issues with INITCAP (Title), let's further simplify by not doing INITCAP.
Now we can concentrate on alternatives to MINUS.
MINUS, which you are already doing, can thus be written like this:
WITH  this_customers_orders  AS
     SELECT     Title
     ,     Category
     FROM     Books
     JOIN     OrderItems     USING (ISBN)
     JOIN     Orders          USING (Order#)
     WHERE     Customer#     = 1007
SELECT     Title
,     Category
FROM     Books
WHERE     Category     IN (
                  SELECT  category
                  FROM        this_customers_orders
MINUS
SELECT     *
FROM     this_customers_orders;You should always format code, so you can easily see where sub-queries begin and end, and what the principal parts of each query are. This is especially important when you are learning.
To get the same results using NOT EXISTS:
WITH  this_customers_orders  AS
     SELECT     Title     ...     -- as shown above
SELECT     Title
,     Category
FROM     Books     b
WHERE     Category     IN (
                  SELECT  category
                  FROM        this_customers_orders
AND     NOT EXISTS (
                 SELECT  NULL
             FROM        this_customers_orders
             WHERE   Title   = b.Title
                 );Note that the NOT EXISTS sub-query is corellated to the main query. Perhaps 99% of the uncorellated EXISTS (and NOT EXISTS) sub-queries that I've seen have been errors.
You can also get the same results using NOT IN:
WITH  this_customers_orders  AS
     SELECT     Title     ...     -- as shown above
SELECT     Title
,     Category
FROM     Books     b
WHERE     Category     IN (
                  SELECT  category
                  FROM        this_customers_orders
AND     Title      NOT IN (
               SELECT  book_title
               FROM    this_customers_orders
               WHERE   book_title     IS NOT NULL
               );Notice the condition "WHERE book_title     IS NOT NULL" in the NOT IN sub-query. "x NOT IN (sub_query_y)" will never be TRUE if sub_query_y has even one NULL value.
You can also get the same results by doing an outer join:
WITH  this_customers_orders  AS
     SELECT     INITCAP (Title)     AS Title     ...     -- as shown above
SELECT     b.Title
,     b.Category
FROM          Books               b
LEFT OUTER JOIN     this_customers_orders     t     ON b.Title = t.Title
WHERE     Category     IN (
                  SELECT  category
                  FROM        this_customers_orders
AND     t.Title        IS NULL;An outer join is almost certainly the least efficient way of doing this. The reason I mentioned it here is so that, if you ever see yourself writing a outer join like this, you'll have a model for writing it miore efficiiently as a MINUS, NOT EXISTS or NOT IN query instead.

Similar Messages

  • Can I get a replacement iphone set or servicing in Hong Kong for my iphone which is purchased in Singapore?

    Can I get a replacement iphone set or servicing in Hong Kong for my iphone which is purchased in Singapore?

    Hello Hokk,
    After reviewing your post, I have located an article that can help in this situation. It contains helpful information about iPhone service options in various countries:
    Apple - Support - Service Answer Center
    Thank you for contributing to Apple Support Communities.
    Cheers,
    BobbyD

  • I have a macbook pro4.1 and the sound chip in it failed. My speakers and microphone no longer work. Does anyone know of an external speaker and microphone set that plugs into the USB port so I can continue to use both of these functions?

    I have a macbook pro4.1 and the sound chip in it failed. My speakers and microphone no longer work. Does anyone know of an external speaker and microphone set that plugs into the USB port so I can continue to use both of these functions and not have to pay to have the entire main board replaced. Preferably something under $100.

    I don't think you're likely to find a set of external speakers that come with a microphone and plug directly into a USB port. There are USB audio cards that look like tiny flash drives but have microphone and headphone ports in them, to which you can connect a typical two-plug Windows PC headset with a microphone, or else you can connect external speakers and a stand-alone microphone. Those adapters typically cost under $10. I don't know anything about the quality of sound that can be expected from them.
    I have a Griffin iMic, which is much the same idea but larger, a bit more expensive (retail~$40), and perhaps delivers somewhat better sound quality (I hope so, but I don't know because I've never had occasion to try one of the little ones).
    http://www.google.com/products/catalog?oe=UTF-8&hl=en&client=safari&q=usb+sound+ card&um=1&ie=UTF-8&tbm=shop&cid=9618463009759678039&sa=X&ei=tgQoT5zYOMfZ0QHkiICw Ag&ved=0CHcQ8gIwAQ
    http://compare.ebay.com/like/320779457703?var=lv&ltyp=AllFixedPriceItemTypes&var =sbar

  • I do not see where to enter IP addresses in the Open VPN setup. Also, how can I set it up so that I can choose different servers in the same way as I can currently choose them with my VPN app but for PPTP?

    I think I have it working on my iPhone 5. But, I do not see how I can control the exit point that I would like for the VPN. Are all the exit points shown in the VPN setting now going to work with Open VPN, or do they remain PPTP? If I am reading correctly, they look like they remain PPTP. If I cannot control the exit point for open VPN, which exit point is the default in the profile you provided me?I note that Open VPN Connect does not work with any of the new 64 bit devices like the iPhone 5S, the iPad Air, and the new iPad MIni. Is there any chance that you guys will come up with an update for your app so that open VPN can be made to work on all iOS devices? That would be nice, particularly if the Open VPN Connect app does not give me a choice of exit points.Thanks,
    I do not see where to enter IP addresses in the Open VPN setup. Also, how can I set it up so that I can choose different servers in the same way as I can currently choose them with my VPN app but for PPTP?
    Just a quick note to tell you that Open VPN has updated their app so that it is compatible with 64 bit ARM devices like the iPhone 5S, the iPad Air, and the iPad Mini Retina.That does not resolve the problem of how to easily choose among the various possibilities for the exit server. We need to find an easy way to choose.

    Thank you for trying the new Firefox. I'm sorry that you’re unhappy with the new design.
    I understand your frustration and surprise at the removal of these features but I can't undo these changes. I'm just a support volunteer and I do not work for Mozilla. But you can send any feedback about these changes to http://input.mozilla.org/feedback. Firefox developers collect data submitted through there then present it at the weekly Firefox meeting
    I recommend you try to adjust to 29 and see if you can't make it work for you before you downgrade to a less secure and soon outdated version of Firefox.
    Here are a few suggestions for restoring the old design. I hope you’ll find one that works for you:
    *Use the [https://addons.mozilla.org/en-US/firefox/addon/classicthemerestorer/ Classic Theme Restorer] to bring back the old design. Learn more here: [[How to make the new Firefox look like the old Firefox]]
    *Use the [https://addons.mozilla.org/en-US/firefox/addon/the-addon-bar/ Add-on Bar Restored] to bring back the add-on bar. Learn more here: [[What happened to the Add-on Bar?]]

  • Can two different Apple ID's share itunes content? if so , how do i set this up?

    Can two different Apple ID's share itunes content? If so , how do i set this up?

    Hello mqg13,
    Thanks for using Apple Support Communities.
    How do you mean "share iTunes content?" You are able to purchase and download from one Apple ID on multiple devices, simply follow the steps in the article below:
    iTunes Store: Associating a device or computer to your Apple ID
    http://support.apple.com/kb/HT4627
    Take care,
    Alex H.

  • How can i reset my app store setting? i have signed in and out with my own apple id but i used a different one at first and that one is not going away

    How can i reset my app store setting?
    I have signed in and out with my own apple id but i used a different one at first a few years ago and that one is not going away how do i change it to my own apple id?

    Content and Apple IDs -
    Content is forever tied to the Apple ID that bought it. Apple does not transfer content from one Apple ID to another. Apple does not merge Apple IDs. You will never be able to access your content bought with one Apple ID with a new Apple ID. Your only option is to delete the content and buy it again with the new Apple ID.

  • Using Configurator, you can install paid apps to a set of iPads. How do you recover the app license if one of the iPad "dies" and needs to be replaced?

    Using Configurator, you can install paid apps to a set of iPads. How do you recover the app license if one of the iPad "dies" and needs to be replaced?

    Used iMac iLife issues: Apple Support Communities

  • When replacing an accidentally damaged idevice can you opt to replace it with a different model, ie broken 4th gen ipad for new iPad mini?

    When replacing an accidentally damaged idevice can you opt to replace it with a different model, ie broken 4th gen ipad for new iPad mini?

    No. the replacement is always the same as the item being replaced.

  • I bought an iPhone 4s in u.s....and accidentally it got wet inthe rain now it does function at all..i gave it in apple india and they checked and said that it cant be repaired..can i get a replacement in u.s..how much will it cost??

    I bought an iPhone 4s from Chicago last year and it got wet in the rain.I gave it here in india to apple..they said it can't be repairedand replaced can I get a replacement in u.s.  how much will it cost?will they refuse because the phone was opened?

    http://www.apple.com/support/service/
    Out-of-Warranty Service
    If you own an iPhone that is ineligible for warranty service but is eligible for Out-of-Warranty (OOW) Service, Apple will service your iPhone for the Out-of-Warranty Service fee listed below.
    iPhone model
    Out-of-Warranty Service
    iPhone 5
    $229
    iPhone 4S
    $199
    iPhone 4, iPhone 3GS,
    iPhone 3G, Original iPhone
    $149
    A $6.95 shipping fee will be added if service is arranged through Apple and requires shipping. All fees are in U.S. dollars and are subject to local tax. "

  • Difficulties replacing some BC functionality with XI (refer to example)

    <b>Difficulties replacing some BC functionality with XI (refer to example)</b>
    I am transferring all interface processing from BC to XI and am having difficulties replacing some of the current BC functionality.  Please refer to my example below
    <b>Current process</b>
    <b>SAP R/3</b>
    SAP R/3 executes an abap that extracts certain data.  This data is passed via a remote function which is configured to point to Business Connector
    <b>Business Connector</b>
    Business Connector takes control and the relevant package does some internal processing then calls the remote enabled function BAPI_ACC_BILLING_POST
    <b>SAP R/3</b>
    Control is returned to SAP R/3 to post the data and on completion control is returned to Business Connector
    <b>Business Connector</b>
    Business Connector again takes control where some more processing is done to generate an xml version of the data prior to returning control back to SAP R/3
    <b>SAP R/3</b>
    Control is returned to the abap immediately following the call to the remote function and processing completes
    The initial synchronous call from SAP R/3 to Business Connector remains open whilst Business Connector executes a subsequent synchronous call to SAP R/3.
    Finally the initial synchronous call in SAP R/3 completes
    <b>Proposed Process</b>
    I am transferring all interface processing from BC to XI and am having difficulties replacing some of the current BC functionality.  In this instance I can set up the initial synchronous call via an XI Integration Process (sync/async bridge).  This allows me to perform subsequent asynchronous calls prior to closing the synchronous bridge
    I have successfully performed synchronous calls to SAP R/3 from XI for different scenarios and have set up the XI design and config in the same manner
    When I try to perform the synchronous posting back to SAP R/3 from within the SA bridge I get the message ‘Message is incomplete. No Sender found’
    Unlike BC, it appears that XI does not allow me to perform the synchronous posting back to SAP R/3 from within the SA bridge.
    <b>Refer to</b> http://help.sap.com/saphelp_nw04/helpdata/en/83/d2a84028c9e469e10000000a1550b0/frameset.htm <b>for the SAP description</b> To enable the communication between a synchronously calling business system (synchronous outbound interface) and an asynchronously called business system (asynchronous inbound and outbound interface), you can define a sync/async bridge in an integration process. You can only define one sync/async bridge for each integration process.
    How can I replace the current BC functionality using XI?
    Regards,
    Mike

    Hi all,
    I cannot replicate the current BC functionality in XI
    So I will take this opportunity to simplify and improve the design for XI --> R/3
    Regards,
    Mike

  • How can i find start line of any functions or procedures stored in package body?

    hi
    how can i find start line of any functions or procedures stored in package body?
    is there any way to write a query from for example user_source?
    thanks

    how can i find start line of any functions or procedures stored in package body?
    Why? What will you do differently if a procedure starts on line 173 instead of line 254?
    Tell us what PROBLEM you are trying to solve so we can help you find the best way to solve it.
    If you use PL_SCOPE that info is available in the *_IDENTIFIERS views. See 'Using PL/Scope in the Advanced Dev Doc
    http://docs.oracle.com/cd/B28359_01/appdev.111/b28424/adfns_plscope.htm
    Try this simple sample code. The query is modified from that doc sample:
    -- tell the compiler to collect the info
    ALTER SESSION SET PLSCOPE_SETTINGS='IDENTIFIERS:ALL';
    -- recompile the package
    CREATE OR REPLACE package SCOTT.pack1 as
    PROCEDURE proc1;
    PROCEDURE proc2;
    END;
    CREATE OR REPLACE package BODY SCOTT.pack1 as
    PROCEDURE proc1 IS
    BEGIN
      NULL;
    END;
    PROCEDURE proc2 IS
    BEGIN
      proc1;
    END;
    PROCEDURE proc3 IS
    BEGIN
      proc1;
      proc2;
    END;
    END;
    -- query the info for the package spec
    WITH v AS (
      SELECT    Line,
                Col,
                INITCAP(NAME) Name,
                LOWER(TYPE)   Type,
                LOWER(USAGE)  Usage,
                USAGE_ID,
                USAGE_CONTEXT_ID
        FROM USER_IDENTIFIERS
          WHERE Object_Name = 'PACK1'
            AND Object_Type = 'PACKAGE'
    SELECT LINE, RPAD(LPAD(' ', 2*(Level-1)) ||
                     Name, 20, '.')||' '||
                     RPAD(Type, 20)||
                     RPAD(Usage, 20)
                     IDENTIFIER_USAGE_CONTEXTS
      FROM v
      START WITH USAGE_CONTEXT_ID = 0
      CONNECT BY PRIOR USAGE_ID = USAGE_CONTEXT_ID
      ORDER SIBLINGS BY Line, Col
    LINE,IDENTIFIER_USAGE_CONTEXTS
    1,Pack1............... package             declaration        
    2,  Proc1............. procedure           declaration        
    3,  Proc2............. procedure           declaration        
    -- query the info for the package body - change 'PACKAGE' to 'PACKAGE BODY' in the query above
    LINE,IDENTIFIER_USAGE_CONTEXTS
    1,Pack1............... package             definition         
    2,  Proc1............. procedure           definition         
    6,  Proc2............. procedure           definition         
    8,    Proc1........... procedure           call               
    10,  Proc3............. procedure           declaration        
    10,    Proc3........... procedure           definition         
    12,      Proc1......... procedure           call               
    13,      Proc2......... procedure           call               

  • How can I transfer a variable to regexp_replace function

    Hi,
    In addition to my question from yesterday, I went up one (little) level for the next question.
    How can I transfer a variable to regexp_replace function?
    I am getting the value of the variables from from APEX Items.
    The value of item :P105_OLD_NAME should be replaced with the value from :P105_NAME APEX item.
    The projects in PROJECT_NAME field are separated by “:”
    select * from infoux_proj;
    HOSTNAME PROJECT_NAME
    host1 proj2:proj1
    host3 proj1
    host4 proj12:proj1
    host5 proj3
    host2 proj1:proj3:sunproj1
    this is my code:
    declare
    v_old_proj_list varchar(100);
    v_new_proj_list varchar(100);
    begin
    for host in (select a.hostname, project_name
    from infoux_proj a,
    (select hostname
    from PROJECT_NAMES_WITH_HOSTNAMES
    where name = :P105_OLD_NAME ) b
    where a.HOSTNAME=b.hostname)
    loop
    select project_name ,
    regexp_replace(project_name,'(^|:)(:P105_OLD_NAME)(:|$)','\1:P105_NAME \3') new_project
    into v_old_proj_list, v_new_proj_list
    from infoux_proj
    where hostname=host.hostname;
    update infoux_proj
    set project_name=v_new_proj_list
    where hostname=host.hostname;
    end loop;
    end;
    Thanks,
    Sheli

    Hi, Sheli
    Inside quotes, :p105_old_name will not be taken as a variable name. If you want to use the value of :p105_old_name in a string which its otherwise a literal, then you can concatenate the variable to the literal parts, using the || operator.
    You can do soemthing like this:
    REGEXP_REPLACE ( project_name
                , '(^|:)(' || :P105_OLD_NAME
                             || ')(:|$)'
                , '\1'       || :P105_NAME
                             || ' \3'
                )               AS new_projectI'll bet there's a much simpler way to do what you want. Instead of having two SELECTs, a cursor FOR loop and an UPDATE, you can probably do what you need to with just a single UPDATE or MERGE. It would be more efficient, too. If you'd like help, post CREATE TABLE and INSERT statements for all relevant tables and columns azs the exist before this code is run, a couple of sets of values for the bind variables, and the results you'd like to see (that is, the contents of the changed table) for each set, given the same sample data.
    Always say which version of Oracle you're using.

  • Can`t open password tab to set a supervisor password in security menu of BIOS. BIOS freezes

    few days ago I bought one Thinkpad T60. All is fully functional excepting of one issue with BIOS password. It seems there is no password set previously since I can entry the BIOS menu and set all particular entries w/o any problem. Hovewer when I come to the point to set a BIOS password, the BIOS freezes suddently. Can anybody know what could be the reason for that? Is there some solution how to fix it? Thanks
    Solved!
    Go to Solution.

    Also just to clarify, reflashing a bios chip won't change the password or remove it, but if the chip is reburnned, which is often done when remanufacturing a board the entire chip's data is replaced. I don't know if this is possible or not, but since you suspected the system was "hacked", it is possible someone improperly reburned the wrong data onto the chip making the supervisor password option non operational. If (and I do stress the "if") this is the problem, then you can get the board repaired. This type of repair is often required if a bios flash is interrupted and the system becomes unusable, and usually costs $100-200. 
    Your best option might be to just use the system for what it is, or just replace the system board and be done with it.
    Good Luck
    ThinkPad W-510 i7-820QM(1.73-3.06GHz) Quad Core... ThinkPad T500, T9900, 8gb SSD...FrankNpad T-60p/61p (X9000 2.8ghz) 8gb SSD ips FlexView...ThinkPad T-61p (T9300 2.5ghz) 8gb ram...Thinkpad X-61 Tablet 4gb ram...ThinkPad A-31 (1.9ghz P4 1.5gb ram)

  • Can two different apple id's use one iTunes account?

    When we first purchase apple products (me and hubby!) we used one apple id account and i downloaded all our music and purchased a lot more. Since then i have purchased my own iPhone and set it up with a different apple id - trouble is i cannot access any of the music   When i tried to sync with our mac all that happened is that our messages, contacts, emails, photos, notes and diaries etc all transferred and became a right pickle - we kept getting each others texts?!  Can i access it if i set up iTunes match?  And if i do that will it copy everything else across as it did before?  Please help, all i want to do is access all the music i organised!

    Read this.
    iTunes Store: Associating a device or computer to your Apple ID
    http://support.apple.com/kb/ht4627
     Cheers, Tom

  • Can someone tell me how to set my name so when I send a text it shows up instead of my phone number

    Can someone tell me how to set my name so when I send a text it shows up instead of my phone number

    You can't do that. Only the sending number is transmitted by the carrier with an SMS message. If the recipient has you in their address book, with most smart phones and some others, the name will display, but that's a function of the phone on the receiving end.

Maybe you are looking for

  • SAP MII 14.0 - Calling Transactions using SOAP Runner fails

    Hello All, We are calling transactions in SAP MII 14.0 from external apps using SOAP Runner and we are passing the login info in the payload. But we are getting the following error. We are passing the following in the payload. <xmii:LoginName>Usernam

  • Re: BIP Scheduling Error msg

    Hi all, I have scheduled reports in BI Publisher but it is failing with this below errors. why it si giving like that, if any one knows plz help me it is very Urgent. 1 Report oracle.apps.xdo.servlet.scheduler.ProcessingException: oracle.apps.xdo.XDO

  • Update statement

    I have an update statement within a sproc as follows UPDATE temp t            SET t.AFTER = 'william st',                t.updated = SYSDATE,                t.submitted = SYSDATE          WHERE t.session= '100'            AND t.hip= '1234'           

  • Installed Memory not showing up in a late 2009 Dual Core Mac Pro

    I have a late 2009 Dual Core Mac Pro. I restarted the my computer today, an a utility popped up saying something about I need to re-arrange the memory in my mac because it was not optimized. I thought this was weird because all the slots are filled w

  • SQl Developer Map View

    So how does on change the size of a displayed point that is the result of and sql query that is a geom. Example result provided below: SQL> select geom from nodes where rownum <= 10 ; GEOM(SDO_GTYPE, SDO_SRID, SDO_POINT(X, Y, Z), SDO_ELEM_INFO, SDO_O