Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7833395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:41:19+00:00 2026-06-02T12:41:19+00:00

I have a table SpecialOffers and table for special offer items called SOItems And

  • 0

I have a table SpecialOffers and table for special offer items called SOItems
And i want to get the special offer for a specific item if i found it so first i did this :

IF EXISTS(SELECT * FROM SOTtems WHERE ItemType = 2 AND Itemid = @id)
BEGIN
      INSERT INTO #SO
      SELECT * FROM SpecialOffers so 
      INNER JOIN SOItems soi ON so.Id = soi.SpecialOfferID
      WHERE soi.ItemType = 2 AND soi.Itemid = @id
END

But then to get rid of INNER JOIN because i thought it’s better for performance i did this :

DECLARE @specialOfferID INT

SET @specialOfferID = (SELECT SpecialOfferID FROM SOTtems WHERE ItemType = 2 AND Itemid = @id)

IF @specialOfferID IS NOT NULL
BEGIN
     INSERT INTO #SO
     SELECT * FROM SpecialOffers so 
     WHERE ID = @specialOfferID
END

So which is more efficient and better for performance to execute more sql queries or use join for this example and in general

Note : in the stored procedure i am writing i have to this more than 6 times that is why i asked u 🙂

thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T12:41:26+00:00Added an answer on June 2, 2026 at 12:41 pm

    This hand optimization is likely unnecessary, since the optimizer is more than capable of handling this.

    You can put both into SSMS and run them together and look at relative costs in the the execution plan.

    Avoiding joins is not usually a first step in optimizing.

    My first step is usually to look at the indexing strategy and ensure I haven’t left out the basic indexes and then look at execution plans and see if there are any glaring problems.

    Then I don’t optimize until I have an actual performance problem – and then only after understanding what’s really causing the performance issue.

    I would actually simplify it down to only:

    INSERT INTO #SO
    SELECT *
    FROM SpecialOffers so 
    INNER JOIN SOItems soi
        ON so.Id = soi.SpecialOfferID
        AND soi.ItemType = 2
        AND soi.Itemid = @id
    

    That’s it – no EXISTS check or anything else – the inner join means that the check for existence is redundant, because this won’t insert anything otherwise. This makes for more maintainable code in one sense, because you won’t need to duplicate the condition if it changes, and code in a join is not likely to be altered accidentally. On the other hand, if the join does get altered, it could have more serious repercussions.

    Note, you can still write this as a WHERE version without using the EXISTS check as well.

    Less code typically means less places for bugs to hide.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have table structure with 3 colums (column1, column2, column3) and i want to
I have table with 3 columns A B C. I want to select *
I have table called Direccion other called Cliente each one defined like this public
I have table with data 1/1 to 1/20 in one column. I want the
I have a table in which comma seprated id of another table i want
I have table view controller with me. What I want is that I want
I have table with Timestamp, temperature and humidity. I want to have the average
I have table called product review with columns productreviewid , productid , shopperid ,
i have table A and table B. I have a bridge table called tableC
i have table student,fields are student_id, student_name, year_joining ... now i want to rename

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.