MySQL is an open-source Relational Database Management System that stores data in a structured format using rows and columns. MySQL is a platform-independent language and can be compiled on any platform. MySQL is generally used for storing, manipulating, and retrieving data in RDBMS by using the SQL (Structured Query Language).
In this article, we will learn about the SELECT INTO statement in MySQL which serves several purposes in MySQL such as copying data from a new table, copying particular columns from a table, creating a backup, and so on.
SELECT INTO Statement
The SELECT INTO statement in SQL retrieves data from a database table and stores it into variables or a new table. It's commonly used to copy data from one table to another or to assign values from a query result to variables. This statement helps streamline data manipulation tasks by simplifying the process of retrieving and storing data.
Syntax:
SELECT column1, column2
INTO new_table
FROM old_table;
The parameters used in the SELECT INTO statement are as follows:
- column1, column2: Columns from the old_table that you want to select data from.
- new_table: The name of the new table where you want to store the selected data.
- old_table: The name of the existing table from which you want to select data.
Example of SELECT INTO in MySQL
In the given example we created our first table "students" with columns id, name, department, and salary, and inserted some data into the table, then we created the "students_archive" table with the same structure using the "LIKE" keyword. After this, we selected data from the "students" table using the "SELECT INTO" statement under the condition where salary is greater than 55000 and inserted it into the "students_archive" table.
Example 1: Archiving Data from the students
table into the students_archive
Table
-- Creating students table
CREATE TABLE students (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
department VARCHAR(100),
salary DECIMAL(10, 2)
);
-- Inserting some sample data into students table
INSERT INTO students(id, name, department, salary)
VALUES
(1, 'Manvi', 'Engineering', 60000),
(2, 'Juhi', 'Marketing', 55000),
(3, 'Navya', 'HR', 50000);
students_archive table:
-- Creating students_archive table with the same structure
CREATE TABLE students_archive LIKE students;
-- Archiving data from studentstable into students_archive table
INSERT INTO students_archive (id, name, department, salary)
SELECT id, name, department, salary
FROM students
WHERE salary > 55000;
Output:
| id | name | department | salary |
|----|-------|-------------|--------|
| 1 | Manvi | Engineering | 60000 |
Example 2: Selecting Data into Variables from the Class
The table where the department is 'Engineering'.
The SELECT INTO statement can also be used to select the data from the table and insert it to store it into the variable.
In this, we will be creating a table and inserting some data into it by the 'VALUE' clause and then we will create two variables "var_id" and "var_name". Then we will use the "SELECT INTO" statement to select the data from the table and store it in the variables.
-- Create class table
CREATE TABLE class (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
department VARCHAR(100),
salary DECIMAL(10, 2)
);
-- Insert sample data into class table
INSERT INTO class (id, name, department, salary)
VALUES
(1, 'Prakhar', 'Engineering', 60000),
(2, 'Navya', 'Marketing', 55000),
(3, 'Manvi', 'HR', 50000);
Declare variables to store data:
-- Declare variables to store data
DECLARE var_id INT;
DECLARE var_name VARCHAR(100);
-- Select data into variables
SELECT id, name
INTO var_id, var_name
FROM class
WHERE department = 'Engineering'
LIMIT 1;
-- Output the selected data
SELECT var_id, var_name;
Output: Values Stored in the variables
| var_id | var_name |
|--------|----------|
| 1 | Prakhar |
Conclusion
In MySQL, the "SELECT INTO" statement is generally used to select the data from the table and insert it into another table but it can also be used in many other ways. In this article we have used the "SELECT INTO" statement to select the data and insert it into another table also we have stored the selected data into the variables.
Similar Reads
SQL Interview Questions Are you preparing for a SQL interview? SQL is a standard database language used for accessing and manipulating data in databases. It stands for Structured Query Language and was developed by IBM in the 1970's, SQL allows us to create, read, update, and delete data with simple yet effective commands.
15+ min read
SQL Tutorial SQL is a Structured query language used to access and manipulate data in databases. SQL stands for Structured Query Language. We can create, update, delete, and retrieve data in databases like MySQL, Oracle, PostgreSQL, etc. Overall, SQL is a query language that communicates with databases.In this S
11 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
SQL Joins (Inner, Left, Right and Full Join) SQL joins are fundamental tools for combining data from multiple tables in relational databases. Joins allow efficient data retrieval, which is essential for generating meaningful observations and solving complex business queries. Understanding SQL join types, such as INNER JOIN, LEFT JOIN, RIGHT JO
6 min read
Normal Forms in DBMS In the world of database management, Normal Forms are important for ensuring that data is structured logically, reducing redundancy, and maintaining data integrity. When working with databases, especially relational databases, it is critical to follow normalization techniques that help to eliminate
7 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
ACID Properties in DBMS In the world of DBMS, transactions are fundamental operations that allow us to modify and retrieve data. However, to ensure the integrity of a database, it is important that these transactions are executed in a way that maintains consistency, correctness, and reliability. This is where the ACID prop
8 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read