Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f66c2a6

Browse files
authored
Create tempral_table_example.md
1 parent b63fd92 commit f66c2a6

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

‎advanced/tempral_table_example.md‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
This script demonstrates the creation and usage of a temporary table in SQL.
2+
3+
It first creates a temporary table named `TempEmployees` and inserts some employee data into it.
4+
5+
Then it performs a query to calculate the average salary for each department. Finally, it drops the temporary table to clean up the database.
6+
7+
́ ́ ́sql
8+
-- Create a temporary table
9+
-- This command creates a new temporary table named TempEmployees with five columns.
10+
CREATE TEMPORARY TABLE TempEmployees
11+
(
12+
EmployeeID INT,
13+
FirstName VARCHAR(50),
14+
LastName VARCHAR(50),
15+
DepartmentID INT,
16+
Salary DECIMAL(10, 2)
17+
);
18+
19+
-- Insert data into the temporary table
20+
-- This command inserts 10 rows of employee data into the TempEmployees table.
21+
INSERT INTO TempEmployees (EmployeeID, FirstName, LastName, DepartmentID, Salary)
22+
VALUES
23+
(1, 'John', 'Doe', 1, 50000),
24+
(2, 'Jane', 'Doe', 2, 60000),
25+
(3, 'Jim', 'Smith', 1, 55000),
26+
(4, 'Jill', 'Johnson', 3, 70000),
27+
(5, 'Jake', 'Williams', 2, 65000),
28+
(6, 'Judy', 'Brown', 3, 75000),
29+
(7, 'Jack', 'Davis', 1, 80000),
30+
(8, 'Julia', 'Miller', 2, 85000),
31+
(9, 'Joe', 'Wilson', 3, 90000),
32+
(10, 'Jennifer', 'Moore', 1, 95000);
33+
34+
-- Perform a query on the temporary table
35+
-- This command selects the DepartmentID and the average Salary from the TempEmployees table, groups the results by DepartmentID, and orders the results by the average Salary in descending order.
36+
SELECT
37+
DepartmentID,
38+
AVG(Salary) AS AverageSalary
39+
FROM
40+
TempEmployees
41+
GROUP BY
42+
DepartmentID
43+
ORDER BY
44+
AverageSalary DESC;
45+
46+
-- Drop the temporary table
47+
-- This command removes the TempEmployees table from the database.
48+
DROP TABLE TempEmployees;
49+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /