Where clause on right side table of a left join

I was told by someone that a where clause on the right side table of a left join is bad SQL.  I have looked around and found a great article for DB2.  I am not sure if this is 100% correct for SQL Server so would like to find a good detailed
article on the topic.  
Thank  you

I was told by someone that a where clause on the right side table of a left join is bad SQL.  I have looked around and found a great article for DB2.  I am not sure if this is 100% correct for SQL Server so would like to find a good detailed
article on the topic.  
Thank  you
I'm going to be blunt here so if you get offended easily please stop reading now.
That has got to be some of the dumbest advice I've seen in a while.  Such a query serves a specific purpose, there's no good or bad to it.  Following is a classic example:
select distinct
CustomerID
from Customer as c
left join Orders as o
on c.CustomerID = o.CustomerID
and o.OrderDate > dateadd(day, -90, getdate())
where o.CustomerID is null
The above hypothetical query, which includes a where clause which targets the right table in a left join, returns all customers which have not placed an order in the past 90 days.  It's a perfectly valid query which serves a key business purpose and
I challenge anyone to justify why they claim it is "bad SQL".
Such a query will execute efficiently given appropriate indices.  There's simply no justification for such a blanket statement about where clauses and outer joins.

Similar Messages

  • Why does my text in Indesign start on the right side instead of the left?

    I have done something to the newsletter that I am working on, but I don't know what I did.
    Whenever I try to write a caption on a picture or try to type a piece of text for the newsletter, the cursor starts on the right side instead of the left and makes weird spacing and never lines up correctly.
    The only way I can get text to work now is to use a snipping tool and snip text from another program!

    With just the text tool selected, but no text boxes selected (or made), any change to the style of paragraphs or fonts etc is 'set' for the document. If you have clicked on the right alignment tool with nothing selected, then everything from then onwards will default to right align.
    If every document you've worked on is doing this, then open InDesign with no document open (so just the panels showing, but no file/doc open) and select left align with the text tool in use. Any setting made with InDesign like this, is default for any new document created. Colours, alignment, optical kerning, fonts, sizes, leading etc etc changed in this state, is the new default.

  • The speaker is used when im using my phone in speaker phone mode or listen to music.This speaker is only the right  side hole  and the left one is mic

    The speaker is used when iam using my phone in speaker phone mode or listen to music.This speaker is only the right side hole and the left one is micophone?

    Correct. As indicated in response to your duplicate post.
    Stedman

  • My ipad screen is black / darker on the right side, but light on left.. What should I do? I try to reset it, but it can't workat all..

    My ipad screen is black / darker on the right side, but light on left.. What should I do? I try to reset it, but it can't workat all..

    I suggest that you take the MBP into an Apple store genius bar for a free evaluation.  There are liquid sensors inside and they should check if any have been activated.  This may give you a clue as to what you may expect regarding future reliability.
    Ciao.

  • My iPad fell one night from one side (left) and now its border from the inside or the main screen  border (left side- portrait) is thicker on the other side (right side) like it moved left when it fell :(( so sad this is a gift from my mother. please help

    my iPad fell one night from one side (left) and now its border from the inside or the main screen  border (left side- portrait) is thicker on the other side (right side) like it moved left when it fell :(( so sad this is a gift from my mother. please help

    thank you. it just annoys me and im scared that my mom would notice it coz she bought it for me..

  • Improve the performance in stored procedure using sql server 2008 - esp where clause in very big table - Urgent

    Hi,
    I am looking for inputs in tuning stored procedure using sql server 2008. l am new to performance tuning in sql,plsql and oracle. currently facing issue in stored procedure - need to increase the performance by code optmization/filtering the records using where clause in larger table., the requirement is Stored procedure generate Audit Report which is accessed by approx. 10 Admin Users typically 2-3 times a day by each Admin users.
    It has got CTE ( common table expression ) which is referred 2  time within SP. This CTE is very big and fetches records from several tables without where clause. This causes several records to be fetched from DB and then needed processing. This stored procedure is running in pre prod server which has 6gb of memory and built on virtual server and the same proc ran good in prod server which has 64gb of ram with physical server (40sec). and the execution time in pre prod is 1min 9seconds which needs to be reduced upto 10secs or so will be the solution. and also the exec time differs from time to time. sometimes it is 50sec and sometimes 1min 9seconds..
    Pl provide what is the best option/practise to use where clause to filter the records and tool to be used to tune the procedure like execution plan, sql profiler?? I am using toad for sqlserver 5.7. Here I see execution plan tab available while running the SP. but when i run it throws an error. Pl help and provide inputs.
    Thanks,
    Viji

    You've asked a SQL Server question in an Oracle forum.  I'm expecting that this will get locked momentarily when a moderator drops by.
    Microsoft has its own forums for SQL Server, you'll have more luck over there.  When you do go there, however, you'll almost certainly get more help if you can pare down the problem (or at least better explain what your code is doing).  Very few people want to read hundreds of lines of code, guess what's it's supposed to do, guess what is slow, and then guess at how to improve things.  Posting query plans, the results of profiling, cutting out any code that is unnecessary to the performance problem, etc. will get you much better answers.
    Justin

  • Where clause in BMM (logical table Source) is not working

    Hello Folks ,
    I felt I ask here before I ask Siebel(oops Oracle!) support, by which I could save significant time NOT restart box multiple times , change log level , upload RPD , making multiple screen shots etc.. etc..
    Here is an example the scenario
    I have a custom field in the activity called the Activity source with two digit abbreviation
    TX – Text message
    LT – Letter
    EM – Email so forth
    The LOV table W_LOV_D has the type as CUST_SRC_ACT
    Whoever implemented analytics they did an alias table for W_LOV_D and did a physical join between W_ACTICITY_F to W_LOV_D through field VAL
    W_ACTICITY_F.X_ACT_SRC--> W_LOV_D.VAL and they did not do a where clause in BMM to retrieve only W_LOV_D.TYPE =’CUST_SRC_ACT’
    So in fact the query pulls TX for CUST_SRC_ACT as well as TX for State abbreviation
    I went ahead and added the where clause filter
    "Siebel Data Warehouse".Catalog.dbo."Dim_W_LOV_D_Acty_Src".TYPE = 'CUST_SRC_ACT'
    I saved the repository and started the server (I was doing offline) spooled the SQL and the results are the same. Looks like the filter is not being applied.
    I did reload the server meta data etc..
    Would anyone knows what is going on with the filter (or what I am doing wrong)?
    Thanks in advance,
    AP

    hi Ap,
    Pull only Dim_W_LOV_D_Acty_Src column into answers and check whether u r able to see CUST_SRC_ACT or not .Of course, you need to check sql query too
    Thanks,
    Saichand.v

  • Variable in where clause of the logical table

    Hi,
    is it possible to define a variable in "where" clause of rpd's physical table (table type - select)? If yes, pls elaborate the steps including how to refresh it with every end user's request?
    Thanks and Regards,
    Igor
    P.S.
    If not, is there any workaround?

    Nico
    thanks, but I am afraid that is not my case. I need to refresh variable value interactively with every query by a dashboard prompt. To illustrate my need just imagine a recursive SQL (defined as a view/logical table in rpd):
    SELECT ....
    FROM ...
    WHERE ...
    START WITH .... columnA=variable1
    CONNECT BY PRIOR .... and columnB=varariable2;
    or
    SELECT c1,c2,c3,c4 from tabA where c1='A' and c2=variable1
    union
    SELECT c1,c2,c3,c4 from tabA where c1='A" and c2='D' and c1 NOT IN ( SELECT c1 FROM tabA where c1='A' and c2=variable1 );
    So, what I want to do is to pass value(s) to my variable(s) every time when respective select (view) is executed. I see above selects are not typical ones the OBI EE is intended for but nevertheless I wonder if such kind of reports are possible to do in OBI EE.
    Thanks and Regards,
    Igor

  • The website is not centered in the middle of my screen, right side is gone and left side is white. Only in safetymode the screen is centered in the middle

    Websites are dislayed in the middle of the screen. Half the website is gone on the right side. on the left side it is white. Only when i startup in safetymode the websites are displayed in the center of the screen

    When going to a website the picture was small - tried changing the Zoom size , tried Zoom Reset - the text did change size but the layout distorts - found under View - Zoom - Zoom Text Only was checked --- Removed this check mark and the website returned to normal size and Zoom worked normally again. I am 99.9999% sure I did NOT check this myself and there is no one else on the computer to cause it. Maybe this will help.

  • Container's right Side scroll-bar to left side !!!

    Hi folks,
    I have something like this in my app:
    <s:Scroller width="500" height="500">
         <s:Group>
              <s:RichEditableText editable="false" id="RET"/>
         </s:Group>
    </s:Scroller>
    But the problem is here, the location of scrollbar is in the right side of container... I want it in the left side of my container. How can I do this?!
    and also after that, How can I costumize my scrollbar? change colours and use some specific pics instead of my scrollbar arrow objects?!
    cheers

    I found the way, myself. For my simple code above, It will be something like this:
    <s:Group id="myGroup"
         width="500" height="500"
         x="10" y="10"
         clipAndEnableScrolling="true">         
              <s:RichEditableText editable="false" id="RET"/>
    </s:Group>
    <s:VScrollBar height="500"
         viewport="{myGroup}" x="{myGroup.x}" y="{myGroup.y}"/>
    Cheers
    BornA

  • Counting number of records using where clause in a large table

    SQL SERVER 2008
    I've to find the count of the rows matching some conditions. The table size is very large with around 50 columns. And every day around 3000 rows are adding. 
    My business logic is to count the number of payments made by a particular user, also if the count exceeds 10 then the user is GoldUser else NormalUser.
    here:
    select count(payment_id) from payments_master_table where payment_user = @payment_user;
    here payment_id is primary key. This query is very slow, since it has to scan the whole table. Is there any other optimized way to find the number of counts. 
    Also, one way I can do is, if the count of payment_id exceeds 10, stop the count,
    but I do not how to do this.  
    Please can anyone help me. 
    Thanks!

    not top 10, but suppose 
    if the count(payment_id) reached 10 for a particular user before scanning the whole
    table, why to proceed ahead. My condition is to apply any user as GoldUser if the count is 10 or more than that. So stop the count if it reaches 10.
    Then in that case you need to implement it as a correlated query
    ie like
    SELECT t.User,COUNT(*)
    FROM table t
    CROSS APPLY (SELECT TOP 10 payment_id
    FROM table
    WHERE payment_user = t.payment_user
    )t1
    GROUP BY t.User
    It will try to fetch random 10 rows for the user and then get count. But even in this case you cant tell it will stop at exactly 10 record for every user as it depends on few other factors too.
    In any case the index on the payment_user column will really help
    Please Mark This As Answer if it solved your issue
    Please Vote This As Helpful if it helps to solve your issue
    Visakh
    My Wiki User Page
    My MSDN Page
    My Personal Blog
    My Facebook Page

  • Where Clause in Logical Table Source

    I have a dimension table joined to a fact table in BMM Layter. I would like to apply a filter in the where clause of the dimension table. Does the fact table also require the same exact filter to be applied in the where clause.

    It would be nice if you add that filter on Fact's where clause part, that would be for entire fact level(start schema) instead of only on table level
    You might need to map the dim to fact to do so.
    mark if helps

  • Clob in where clause

    Hi
    I'm trying to query simple select where in clause is Clob but it's not working.
    set serveroutput on
    declare
    l_clob clob := '''SYS''';
    l_clob2 clob :=null;
    vzm varchar2(15);
    begin
    dbms_output.put_line( to_char(l_clob) );
    select username into l_clob2 from dba_users where username = to_char(l_clob);
    dbms_output.put_line( to_char(l_clob2) );
    end;
    so l_clob is returned as string 'SYS'
    but l_clob2 show errors :
    ORA-01403: no data found
    ORA-06512: at line 9
    01403. 00000 - "no data found"
    #which is not right because there is user SYS in dba_users.
    so my question is how to use clob in where clause to query some table? this is just a easiest example ;

    ok thanks in one value it's working but how with this example
    when I have a list of values to IN() clause so clob like 'SYS','SYSTEM'.etc
    set serveroutput on
    declare
    l_clob clob := '''SYS'',';
    l_clob2 clob :=null;
    vzm varchar2(15);
    begin
    vzm:='''SYSTEM''';
    dbms_lob.writeappend( l_clob, length(vzm),vzm );
    dbms_output.put_line( to_char(l_clob) );
    select username into l_clob2 from dba_users where username IN(to_char(l_clob));
    dbms_output.put_line( to_char(l_clob2) );
    end;
    l_clob is 'SYS','SYSTEM' so it should be fit to IN('SYS','SYSTEM') but it not:
    l_clob2 ORA-01403: no data found
    ORA-06512: at line 9
    01403. 00000 - "no data found"
    *Cause:   
    how to do it when the list of user is really long more then 600 and i have to use IN clause with ' ,' in it?

  • Cardinality estimator 2014 is off with OR in where clause

    Here is my test setup on SQL Server 2014.
    -- Create big table
    CREATE TABLE [dbo].[Store](
    Id int IDENTITY(1,1) NOT NULL,
    City int NOT NULL,
    Size int NOT NULL,
    Name varchar(max) NULL,
    CONSTRAINT [PK_Store] PRIMARY KEY CLUSTERED ([Id] ASC)
    GO
    CREATE NONCLUSTERED INDEX [IX_Store] ON [dbo].[Store] (City ASC, Size ASC)
    GO
    -- Fill with 100k rows
    INSERT Store
    SELECT i % 101, i % 11, 'Store ' + CAST(i AS VARCHAR)
    FROM
    (SELECT TOP 100000 ROW_NUMBER() OVER (ORDER BY s1.[object_id]) AS i
    FROM sys.all_objects s1, sys.all_objects s2) numbers
    GO
    -- Create small table
    CREATE TABLE #StoreRequest (City int NOT NULL, Size int NOT NULL)
    GO
    INSERT #StoreRequest values (55, 1)
    INSERT #StoreRequest values (66, 2)
    Now I execute the following query (I force the index to show statistics estimates)
    SELECT s.City
    FROM #StoreRequest AS r
    INNER JOIN Store AS s WITH(INDEX(IX_Store), FORCESEEK)
    ON s.City = r.City AND s.Size = r.Size
    WHERE s.Size <> 1 OR r.City <> 55
    Here are the estimates that I get (I'm not allowed to upload pictures):
    Index Seek IX_Store
    Actual Number of Rows: 90
    Estimated Number of Rows: 50000
    Fixing WHERE clause to use one table not two makes the estimate perfect:
    SELECT s.City
    FROM #StoreRequest AS r
    INNER JOIN Store AS s WITH(INDEX(IX_Store), FORCESEEK)
    ON s.City = r.City AND s.Size = r.Size
    WHERE s.Size <> 1 OR s.City <> 55
    Index Seek IX_Store
    Actual Number of Rows: 90
    Estimated Number of Rows: 89.74
    Switching to 2012 compatibility mode gives estimate of 1 in both cases:
    Index Seek IX_Store
    Actual Number of Rows: 90
    Estimated Number of Rows: 1
    Could anyone explain the first result? I'm a bit worried about it. The fix in this case is trivial, but this problem gave us quite some headache in more complex real life queries with multiple joins.
    Thank you!

    But not full statistics on a field basis, just sometimes some default stats like total row count that some plans will build.  Even your StoreRequest table only has one two-field index that will have a full histogram.
    But I've seen SQL Server make massively bad plans on two-field indexes.
    I've seen SQL Server go wrong one-column indexes, so that is not a very relevant point.
    Temp tables or not, the estimate here is clearly incorrect. SQL Server knows the density of Size and City. It knows the cardinality of the temp table. The density information gives how many rows the the join will produce. The WHERE clause will then remove
    a certain number of rows. With no statistics for the temp table, it does not now how many, but it will apply some standard guess.
    50000 is a completely bogus number, because the join cannot produce that many rows, and SQL Server is able to compute the join with out the WHERE clause decently. (Well, it estimates 90, when the number is 180.) No, this is obviously a case of the cardinality
    estimator giving up completely.
    It is worth noting that both these WHERE clauses gives reasonable estimates:
     WHERE r.Size <> 11 OR r.City <> 550
     WHERE s.Size <> 11 OR s.City <> 550
    Whereas these two gives the spooky 50000:
     WHERE s.Size <> 11 OR r.City <> 550
     WHERE r.Size <> 11 OR s.City <> 550
    Erland Sommarskog, SQL Server MVP, [email protected]

  • Passing check box values to WHERE clause

    Hi,
    I have created a Data block - 'CONTACTS' (Database data block)
    and has database item - 'Code', 'Descr'
    The number of records displayed is set to 5.
    Value When checked - 'Y'
    Value When Unchecked - 'N'
    Check box mapping of other values - 'unchecked'
    I am writing the code inside 'WHEN BUTTON PRESSED'. My main objective is to return the count of records based
    based on several conditions and one among them is CODE which is can be single or multiple based on the check box checked.
    The requirement is when i check one or multiple checkboxes, i should pass the 'Code' item values to the WHERE clause.
    Right now whenver i am trying to do so, only the current record value is copied to the WHERE clause.
    I have tried using basic loop but things havnt worked.
    Logic tried with basic LOOP
    BEGIN
    GO_BLOCK('CONTACT');
    IF :contact.cb = 'Y' THEN
    LOOP
    IF p_where is null then
    p_where := :contact.code;
    else
    p_where := p_where ||','||:contact.code;
    end if;
    exit when :system.last_record = 'TRUE';
    next_record;
    END LOOP;
    end if;
    MESSAGE ( 'p_where :'||p_where);
    MESSAGE (' ');
    END;
    And Even if i write the LOOP before the first IF, it return me the current record value and move to the last record.
    please guide me where am i wrong.
    Regards.
    Anoop.

    Try something like this:
    FIRST_RECORD;
    LOOP
      IF :contact.cb = 'Y' THEN
        IF p_where is null then
           p_where := :contact.code;
         else
           p_where := p_where ||','||:contact.code;
         end if;
       END IF;
      exit when :system.last_record = 'TRUE';
      next_record;
    END LOOP;
    -- END LOOP;
    MESSAGE ( 'p_where :'||p_where);
    MESSAGE (' ');
    END;

Maybe you are looking for

  • SSO SDK Java Sample Application

    I have configured the sample application (servlet-based), shown in SSO SDK Developer guide. I followed all the steps very carefully. When I try to access the partner application, it redirect me to the login form of the Login Server, and then loop bet

  • Bug report: ube_ipa: internal error when compiling with -xO5

    When compiling imlib2-1.1.2, one source file causes compilation to fail with the following error message: ube_ipa: internal error cc: ube_ipa failed for loader_tiff.c The command which fails is (probably wrapped): cc -DHAVE_CONFIG_H -I. -I. -I.. -I.

  • How can I have the most recent message in a thread shown first and not last

    Thunderbird (used in conjunction with Gmail - I believe the term is mail client) message threads commence with the oldest; how can I make Thunderbird messages commence with the latest message?

  • My Macbook pro keeps freezing while playing videos

    experience frequent stalls/hangs, accompanied by the spinning beach ball progress indicator every time I play video's. weither it be on youtube or VLC player or any video player for that matter. It happens during games I have on here and also happens

  • Exit in inbound processing pgm for material master

    hi, i m writing a check for E1MARAM segment for IDOC_INPUT_MATMAS. i m supposed to use EXIT_SAPLMV02_002. in this i dont have IDOC_DATA available but a structure F_MARA_UEB which gives same info as E1MARAM. but values of ( NTGEW, BRGEW ) look differe