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

  • Home
  • SEARCH
  • 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

Given that I have three tables (Customer, Orders, and OrderLines) in a Linq To
I have two tables, Customer and CustomerConfiguration that should have a one-to-one mapping. What
Given that two branches have diverged and a specific commit from one branch (and
I have two tables that were built for two disparate systems. I have records
I have two history tables that track changes in database values, using a revision
We have two tables, ActivityForm and Field which are given a many-to-many relationship via
I have two tables, one is a Product Customer mapping and another is a
I have two tables, that relate via a one-to-many relationship i.e tableOne (1)----------(*) tableTwo
I have two tables in my DB - products & orders. An order can
I have two tables that have identical columns. One table houses imported data, the

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.