INTERSECT 2

  • mbova407 (6/19/2012)


    no riots?

    The five days we were in Athens, right up to the 15th, there was nada. We did see a gathering of about 500 people for a speech. The media has sensationalized the situation and given an overdramatic picture of what is going on.

    The result: we went to a beach, about 10 km long, on the western coast of the Peloponnese, and there were 6 people on the beach at noon!

    [font="Verdana"]Please don't go. The drones need you. They look up to you.[/font]
    Connect to me on LinkedIn

  • mbova407 (6/19/2012)


    it really helped that #2 and #3 are exactly the same, then i knew about the distinct so by process of elimination.... 1 pt.

    +1 😀

  • Thomas Abraham (6/19/2012)


    Appreciate the question. Had to think for a minute, as I almost never use OUTER joins.

    I suspect you actually use outer joins far more frequently than you think.

    A left join is the same thing as a left outer join. The outer keyword is optional. 😛

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Sean Lange (6/19/2012)


    Thomas Abraham (6/19/2012)


    Appreciate the question. Had to think for a minute, as I almost never use OUTER joins.

    I suspect you actually use outer joins far more frequently than you think.

    A left join is the same thing as a left outer join. The outer keyword is optional. 😛

    Of course you are correct. My mind must still be on that beach. Thanks for correcting me.

    [font="Verdana"]Please don't go. The drones need you. They look up to you.[/font]
    Connect to me on LinkedIn

  • Thomas Abraham (6/19/2012)


    Sean Lange (6/19/2012)


    Thomas Abraham (6/19/2012)


    Appreciate the question. Had to think for a minute, as I almost never use OUTER joins.

    I suspect you actually use outer joins far more frequently than you think.

    A left join is the same thing as a left outer join. The outer keyword is optional. 😛

    Of course you are correct. My mind must still be on that beach. Thanks for correcting me.

    I wish my mind was still on the beach...so jealous. Sounds you had a great time.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Sean Lange (6/19/2012)


    Thomas Abraham (6/19/2012)


    Appreciate the question. Had to think for a minute, as I almost never use OUTER joins.

    I suspect you actually use outer joins far more frequently than you think.

    A left join is the same thing as a left outer join. The outer keyword is optional. 😛

    And in addition, any NOT EXISTS or NOT IN with a subquery is also essentially a special kind of outer join. The query optimizer will rewrite the query internally to an outer join. (Shown in the execution plan as a (left or right) "Anti Semi Join").


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • Sean Lange (6/19/2012)


    Thomas Abraham (6/19/2012)


    Of course you are correct. My mind must still be on that beach. Thanks for correcting me.

    I wish my mind was still on the beach...so jealous. Sounds you had a great time.

    I might have had a better time had I not had to take my wife along. :w00t:

    [font="Verdana"]Please don't go. The drones need you. They look up to you.[/font]
    Connect to me on LinkedIn

  • Thanks for the question, Ron.

    "El" Jerry.

    "El" Jerry.

    "A watt of Ottawa" - Gerardo Galvan

    To better understand your help request, please follow these best practices.[/url]

  • Thomas Abraham (6/19/2012)


    Sean Lange (6/19/2012)


    Thomas Abraham (6/19/2012)


    Of course you are correct. My mind must still be on that beach. Thanks for correcting me.

    I wish my mind was still on the beach...so jealous. Sounds you had a great time.

    I might have had a better time had I not had to take my wife along. :w00t:

    Yikes!!! I am going to let that one lie. 😉

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • great question and discussion again today - cheers!

  • Richard Warr (6/19/2012)


    . . . Any ideas on the performance differences between the INTERSECT and INNER JOIN methods?

    Yes, there is difference: DISTINCT implies that JOIN will do pre-sorting and a stream aggregate. Sort is typically costly, although in this case it will be done in memory, so fast that it would be difficult to measure the diff.

    Thanks for the question, Ron!

  • Thanks Ron.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • Sean Lange (6/19/2012)


    Thomas Abraham (6/19/2012)


    Sean Lange (6/19/2012)


    Thomas Abraham (6/19/2012)


    Of course you are correct. My mind must still be on that beach. Thanks for correcting me.

    I wish my mind was still on the beach...so jealous. Sounds you had a great time.

    I might have had a better time had I not had to take my wife along. :w00t:

    Yikes!!! I am going to let that one lie. 😉

    Me and... *cough* my wife *cough* are living a hundred meters from one of the most beautiful beaches in my country which is inside a city with around 40 beaches. I see tourists speaking a hundred tongues here each day. I can't help but recommend living in a place like this. It's food for the soul.

    Sorry for being offtopic. I couldn't resist. 😉

    Best regards,

    Andre Guerreiro Neto

    Database Analyst
    http://www.softplan.com.br
    MCITPx1/MCTSx2/MCSE/MCSA

  • Actually, I don't think that any of the responses are technically correct. While #4 returns similar results to #1, they are not the same. Specifically, #1 returns integers (as clarified by the one link in the discussion), but #4 returns strings. This can be seen by the following queries.

    SELECT [Select #1] + ' Select #1'

    FROM (

    SELECT x AS [Select #1]

    FROM #A

    INTERSECT

    SELECT M

    FROM #B

    ) AS s1

    GO

    SELECT [Select #4] + ' Select #4'

    FROM (

    SELECT DISTINCT(x) AS [Select #4]

    FROM #A

    INNER JOIN #B

    ON #A.x = #B.M

    ) AS s4

    The first query will raise an error, the second query will work.

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • Nice One.. Can anyone tell y we are not using Intersect. we always prefering JOINs.

    --
    Dineshbabu
    Desire to learn new things..

Viewing 15 posts - 16 through 29 (of 29 total)

You must be logged in to reply to this topic. Login to reply