Is it possible to have a tableless select with multiple rows? Is it possible to have a tableless select with multiple rows? sql sql

Is it possible to have a tableless select with multiple rows?


You can use Table Value Constructors for this if supported by your RDBMS. Here's an example from Mr Celko

SELECT X.*FROM   (VALUES (1, 3.07766, 6.31371, 12.7062, 63.65600),               (2, 1.88562, 2.91999, 4.30265, 9.92482),               (3, 1.63774, 2.35336, 3.18243, 5.84089)) AS X (A, B, C, D, E); 


Use a UNION:

SELECT 1UNIONSELECT 2

Looks like this in MySQL:

+---+| 1 |+---+| 1 | | 2 | +---+

Use UNION ALL to avoid the loss of non-unique rows.


T-SQL's UNPIVOT can transpose data from rows to columns. Multiple unpivots is equivalent to the Cartesian product of each unpivoted column.

SELECT N, S  FROM (SELECT 1 aa, 2 ab, 3 ac, 'a' ba, 'b' bb, 'c' bc) s  UNPIVOT (N for Num in (aa,ab,ac)) AS t  UNPIVOT (S for Str in (ba,bb,bc)) AS u

Result:

N | S--+--1 | a1 | b1 | c2 | a2 | b2 | c3 | a3 | b3 | c