title: Basic SQL Insert statement approach
body: <p>Given that I have two tables</p>
  
  <ul>
  <li>Customer (id int, username varchar)</li>
  <li>Order (customer_id int, order_date datetime)</li>
  </ul>
  
  <p>Now I want to insert into Order table based on customer information which is available in Customer table.</p>
  
  <p>There are a couple of ways I can approch this problem.</p>
  
  <p>First - I can query the customer information into a variable and then use it in an INSERT statement.</p>
  
  <pre><code>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())
  </code></pre>
  
  <p>Second Approach is to use a combination of INSERT and SELECT query.</p>
  
  <pre><code>INSERT INTO Orders (customer_id, order_date)
  SELECT id, GETDATE() FROM Customers
  WHERE username = 'john.smith'
  </code></pre>
  
  <p>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>
  
  <p>p.s. I was asked this question in one of the technical interviews.</p>