Home > Database > Mysql Tutorial > How to Generate a Number Sequence from 1 to 100 in SQL using DUAL?

How to Generate a Number Sequence from 1 to 100 in SQL using DUAL?

Barbara Streisand
Release: 2024-12-31 08:55:13
Original
165 people have browsed it

How to Generate a Number Sequence from 1 to 100 in SQL using DUAL?

Generating a Number Sequence in SQL: From 1 to 100

Question:

Can you provide a SQL query that generates a list of numbers from 1 to 100 using the DUAL table?

Answer:

Certainly! Here's a SQL query that can generate the desired sequence:

Select Rownum r
From dual
Connect By Rownum <= 100
Copy after login

In this query, we use the DUAL table, which is a built-in table in most SQL databases that always returns one row with no columns. We connect this table to itself using the CONNECT BY clause, which allows us to create a hierarchical tree-like structure. The ROWNUM pseudo-column is used to assign a unique number to each row, which we then use to create the sequence. By specifying that ROWNUM should be less than or equal to 100 in the CONNECT BY clause, we limit the query to return only the first 100 numbers.

This query should return the following output:

| r |
|---|---|
| 1 |
| 2 |
| 3 |
| ... |
| 100 |
Copy after login

The above is the detailed content of How to Generate a Number Sequence from 1 to 100 in SQL using DUAL?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template