SQL Commands


SQL SELECT INTO Tutorial

The SELECT INTO statement copies data from one table and inserts it into a new table.

The number of columns and data type of column must be same.

Syntax of a SELECT INTO STATEMENT

SELECT column_name(s)
INTO new_table
FROM table_name;

Note: WHERE condition is optional to use along with SELECT INTO Clause.

SQL SELECT INTO STATEMENT Example:

Database table “Employee”

Employee IDEmployee NameAgeGenderLocationSalary
1001Henry54MaleNew York100000
1002Tina36FemaleMoscow80000
1003John24MaleLondon40000
1004Mile31MaleLondon70000
1005Tara26FemaleMoscow50000
1006Sohpie29FemaleLondon60000

Below is an example of a sql statement to create a copy of “Employee” table, use of * selects all rows.

If you want to copy certain columns then declare the column name(s)
The user can alternatively specify number of rows or % of rows

SELECT *
INTO Copy_Employee
FROM Employee;

Command will create a table similar to “Employee” table. This SELECT INTO statement is often used for creating a backup copy of a table.

SELECT INTO Statement with IN Clause

One can use SELECT INTO cmmand in conjunction with the IN clause for copying the table into another database.

SELECT *
INTO Copy_Employee IN 'Backup.mdb'
FROM Employee;

This will create a copy of Employee table in MS Access database extension.

SELECT INTO Statement: Copy Selected Coloums

If one wants to copy only a few columns of table, then below is an example.

SELECT EmployeeID, EmployeeName
INTO Copy_Employee
FROM Employee;

SELECT INTO Statement: Select Specific Rows

One can also use WHERE clause to select specific rows of a table. Suppose in the above example one wants to display Employee ID & Employee Name of all male employees.

SQL TOP CLAUSE Output

All these three commands would give the following results in respective database.

SELECT EmployeeID, EmployeeName
INTO Copy_Employee
FROM Employee
WHERE Gender='Male';

Note: If the condition in the WHERE clause returns no data, then an empty new table is created.

BookMark This Page





SQL TRANSLATE:
100 + Languages

EnglishFrenchGermanItalianPortugueseRussianSpanish