Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server : SELECT n FROM (VALUES(0),(0),(0),(0)) t(n)

Tags:

sql-server

Am seeing this in a script

SELECT n FROM (VALUES(0),(0),(0),(0)) t(n)

I know what it does. It is returning a column called 'n' with 4 rows, each of whose value is 0. But, what does this mean? What is t(n), and what is 'n'?

like image 863
Jean Avatar asked Mar 09 '15 15:03

Jean


People also ask

How do you select a range of numbers in SQL?

The SQL BETWEEN Operator The BETWEEN operator selects values within a given range. The values can be numbers, text, or dates. The BETWEEN operator is inclusive: begin and end values are included.

How do you use values from previous or next rows in a SQL Server query?

SQL Server LAG() is a window function that provides access to a row at a specified physical offset which comes before the current row. In other words, by using the LAG() function, from the current row, you can access data of the previous row, or the row before the previous row, and so on.


1 Answers

It's a table value constructor. t being the alias for the table, n the column name. A better example might make it clearer;

SELECT 
    MyData.Column1, 
    MyData.Column2 
FROM (VALUES(1, 'Tom'),
            (2, 'Dick'),
            (3, 'Harry'),
            (4, 'Ermintrude')
      ) MyData(Column1, Column2)

returns

Column1     Column2
----------- ----------
1           Tom
2           Dick
3           Harry
4           Ermintrude
like image 153
Rhys Jones Avatar answered Sep 19 '22 09:09

Rhys Jones