Results 1 to 3 of 3

Thread: [RESOLVED] Are these two simple queries identical?

  1. #1

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Resolved [RESOLVED] Are these two simple queries identical?

    Both in terms of results produced and performance?
    Code:
    SELECT	 sab_stuc AS StudentID
    	   , sab_seq2 AS SequenceNo
    	   , (SELECT raa_name FROM srs_raa WHERE raa_code=sab_raac) AS RecordReason
    	   , sab_begd AS [Date]
    	   , sab_udf1 AS AMPM 
    FROM srs_sab
    
    
    SELECT	 sab_stuc AS StudentID
    	   , sab_seq2 AS SequenceNo
    	   , srs_raa.raa_name  AS RecordReason
    	   , sab_begd AS [Date]
    	   , sab_udf1 AS AMPM 
    FROM srs_sab
    INNER JOIN srs_raa ON raa_code=sab_raac

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Are these two simple queries identical?

    The results should be identical, as long as every record in srs_sab has a matching record in srs_raa. If they don't, the first query will probably (depending on the database system) return the srs_sab records with Null for RecordReason, but the second query wont return those records at all.


    The performance is not so easy to determine, even when the results are identical... depending on the query parser in the database system, they might be exactly the same, but they might differ quite a bit (and if so, there are too many factors to predict which would be faster).

  3. #3

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: Are these two simple queries identical?

    Thanks, in terms of point one there should always be values to join on so am happy that the results will be the same.

    In terms of performance, using SQL Server 2012 against 100k records in the main table both queries run in between 1-2 seconds. So unless as the records grow and the time becomes slow I may revisit and look at the plans.

    I just wanted to be sure there wasnt an obvious difference as a new developer used the first one which is against my norm.

Posting Permissions

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



Click Here to Expand Forum to Full Width