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 5999951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:37:27+00:00 2026-05-23T00:37:27+00:00

Given that I have two tables Customer (id int, username varchar) Order (customer_id int,

  • 0

Given that I have two tables

  • Customer (id int, username varchar)
  • Order (customer_id int, order_date datetime)

Now I want to insert into Order table based on customer information which is available in Customer table.

There are a couple of ways I can approch this problem.

First – I can query the customer information into a variable and then use it in an INSERT statement.

DECLARE @Customer_ID int
SELECT @Customer_ID = id FROM Customer where username = 'john.smith'
INSERT INTO Orders (customer_id, order_date) VALUES (@Customer_ID, GETDATE())

Second Approach is to use a combination of INSERT and SELECT query.

INSERT INTO Orders (customer_id, order_date)
SELECT id, GETDATE() FROM Customers
WHERE username = 'john.smith'

So my question is that which is a better way to proceed in terms of speed and overhead and why ? I know if we have a lot of information getting queried from Customer table then the second approach is much better.

p.s. I was asked this question in one of the technical interviews.

  • 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-05-23T00:37:28+00:00Added an answer on May 23, 2026 at 12:37 am

    The second approach is better.

    The first approach will fail if the customer is not found. No check is being done to make sure the customer id has been returned.

    The second approach will do nothing if the customer is not found.

    From an overhead approach why create variables if they are not needed. Set based sql is usually the better approach.

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

Sidebar

Related Questions

I have two tables: AgeMilestones //Represents available timespans Id int Description varchar //(newborn, 1
I have been given two different Microsoft Word document that my virus scanner has
I have string that I want to chop to array of substrings of given
I have two tables that have identical columns. One table houses imported data, the
Given that I only have one monitor, what's the best way to debug a
What alternatives are there to GAE, given that I already have a good bit
I have just started migrating my homegrown persistence framework to JPA. Given that the
I have a <table/> in a ViewUserControl that I have given the tag a
I have noticed that regardless of a given script's execution time, every date() call
Given a situation where you have 2 projects that in total will provide enough

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.