Sunday, May 4, 2025

Create Table in Sql using Select Statement

In SQL Server, one can create a table using SELECT statement. Let's learn how we do it.

Let's take a scenario where you are ordering your food in restaurant. But you are not asking what you need rather you are asking give me exactly the same food as you gave it to the person in table no 5.

SELECT * INTO NewTable FROM OldTable

This is the basic query we are going to try in this tutorial. Let's do it step by step.

We already have a table named tblUser as shown below:


Let's create a new table with exactly same columns and data type using the above query.



This statement makes a replica of old table and copies data (from old table) to the new table.

Here, * tells server to copy all the columns whereas we can mention name of only those columns that are required.

SELECT Column1, Column2, column3 INTO newTable FROM OldTable

Copy only table structure but not data

we can use some false condition after where clause. e.g.

SELECT * INTO newTable FROM OldTable WHERE 1= 2

Copy table structure and some data that satisfies our conditions

SELECT * INTO newTable FROM OldTable WHERE some_conditions

e.g.

SELECT * INTO newTable FROM OldTable WHERE ID > 10

This Is The Newest Post


EmoticonEmoticon

Create Table in Sql using Select Statement

In SQL Server, one can create a table using SELECT statement. Let's learn how we do it. Let's take a scenario where you are ordering...