INTO
INTO is a keyword in SQL that is used to insert new data into a database table. It specifies the table where the inserted rows should go. This keyword is primarily employed in the INSERT INTO statement.
Example
CREATE TABLE test_table (number INT, name VARCHAR(20));INSERT INTO test_table VALUES (1, 'Tom'), (2, 'Jerry');SELECT * FROM test_table;Output
+--------+-------+| number | name |+--------+-------+| 1 | Tom || 2 | Jerry |+--------+-------+Explanation
The above example demonstrates the use of the INTO keyword in SQL, specifically MySQL. A new table, test_table, is first created with two columns, number and name. Then, two rows of data are inserted using the INSERT INTO statement. The SELECT * FROM statement is used to display all rows from test_table.
Example
SELECT *INTO NewTableFROM OldTableWHERE Condition;Output
Command(s) completed successfully.Explanation
The example query duplicates an existing table named ‘OldTable’ and creates a new table named ‘NewTable’ with the same data structure and values - only for records that satisfy the given ‘Condition’. The result of the command isn’t a data set but a completion notification.
Example
DECLARE text_var VARCHAR2(100);BEGIN SELECT 'Hello, Oracle!' INTO text_var FROM dual;
DBMS_OUTPUT.PUT_LINE(text_var);END;Output
Hello, Oracle!Explanation
In the example, a variable text_var is declared to store text data. The SELECT INTO statement retrieves the string ‘Hello, Oracle!’ and assigns it to text_var. The DBMS_OUTPUT.PUT_LINE procedure prints the value of text_var, resulting in the output ‘Hello, Oracle!’. The FROM dual clause is a Oracle-specific syntax to select from a dummy table.
Example
CREATE TABLE Employee (Name STRING, Age INT);INSERT INTO Employee (Name, Age)VALUES ('John Doe', 30);Output
sqlite> SELECT * FROM Employee;John Doe|30Explanation
The INSERT INTO statement is used to insert new records into a table in SQLite. In the example, a new record is created for ‘John Doe’ with age ‘30’ in the ‘Employee’ table.