Insert and Query Data in the Temporary Table
Now that you have created the temporary table temp_employees
, you can insert data into it and query the data, just like you would with a regular table. This step will demonstrate how to populate the temporary table and retrieve information from it.
You should still be connected to the MySQL server from the previous step, with the mysql>
prompt visible.
Let's insert a few rows of data into the temp_employees
table using the INSERT INTO
statement.
INSERT INTO temp_employees (name, department) VALUES ('Alice Smith', 'Sales');
INSERT INTO temp_employees (name, department) VALUES ('Bob Johnson', 'Marketing');
INSERT INTO temp_employees (name, department) VALUES ('Charlie Brown', 'IT');
These commands add three records to your temporary table. Each record includes a name and a department.
To see the data you just inserted, use the SELECT
statement to query the temp_employees
table.
SELECT * FROM temp_employees;
This command retrieves all columns (*
) from all rows in the temp_employees
table. The output should show the data you inserted:
+----+---------------+------------+
| id | name | department |
+----+---------------+------------+
| 1 | Alice Smith | Sales |
| 2 | Bob Johnson | Marketing |
| 3 | Charlie Brown | IT |
+----+---------------+------------+
3 rows in set (0.00 sec)
You can also use a WHERE
clause to filter the results. For example, to find employees in the 'IT' department:
SELECT name FROM temp_employees WHERE department = 'IT';
The output will show only the name of the employee in the IT department:
+---------------+
| name |
+---------------+
| Charlie Brown |
+---------------+
1 row in set (0.00 sec)
This demonstrates that you can perform standard data manipulation and querying operations on temporary tables within your session.