Generating Data
Learn to generate data using SQL.
Generating data is very handy. Sometimes, we need to generate data for practice. Sometimes, we want to run performance simulations and need a big table to work with. Other times we might need to generate a sequence such as a time dimension in a data warehouse, or an axis table to join to in an anomaly detection system.
There are many reasons to generate data in SQL, and in this chapter we’ll learn several ways to do that.
The SELECT
clause
The most straightforward way to to generate data in SQL is using the SELECT
clause:
SELECT 1 AS id, 'haki' AS name
This query will produce a single row with the id
column and the name
column. Notice that this query is not fetching from any table. In fact, in SQL, the only required clause is SELECT
.
Generating just one row is useful, but what if we need to generate more than one row?
The UNION ALL
command
To combine the results of multiple queries we can use UNION ALL
:
SELECT 1 AS id, 'haki' AS nameUNION ALLSELECT 2, 'benita'
We used UNION ALL
to concatenate the output of two queries.
To ...