SQL SELECT INTO Statement

SQL SELECT INTO Statement

SQL SELECT INTO Statement

The SQL SELECT INTO statement is used to create a new table and insert the result set of a SELECT statement into that table. It is often used to create backup tables, temporary tables, or to export data from one database to another.

The syntax for the SELECT INTO statement is as follows:

				
					SELECT column1, column2, ...
INTO new_table_name
FROM source_table_name
WHERE condition;

				
			

In this syntax, the new_table_name is the name of the new table that will be created, and the source_table_name is the name of the table from which the data will be retrieved. The SELECT statement specifies the columns that will be included in the new table, and the WHERE clause is used to filter the result set if necessary.

Here’s an example of using the SELECT INTO statement to create a backup table:

				
					SELECT *
INTO employees_backup
FROM employees;

				
			

In this example, a new table named employees_backup is created, and all the data from the employees table is inserted into the new table.

Note that the SELECT INTO statement is specific to certain database management systems such as Microsoft SQL Server and Microsoft Access. In other database management systems such as MySQL, PostgreSQL, and Oracle, the equivalent statement is called CREATE TABLE AS SELECT.

Here’s an example of using the CREATE TABLE AS SELECT statement in MySQL:

				
					CREATE TABLE employees_backup
AS SELECT *
FROM employees;

				
			

In this example, a new table named employees_backup is created, and all the data from the employees table is inserted into the new table.

Join To Get Our Newsletter
Spread the love