tags:

views:

495

answers:

6

is the FROM clause necessary in every SELECT statement?Thanks..

+12  A: 

Not really - you can use SELECT to initialize a variable.

Examples from here:

mysql> SELECT 1 + 1; -> 2

Otávio Décio
I didn't think about variable initialization.
TheTXI
Didn't Oracle require you to SELECT FROM DUAL?
Paul Tomblin
@Paul - he's asking about mysql, I believe you are correct about Oracle
Otávio Décio
+1  A: 

No. you can very easily do

SELECT 1+1
Ali Kazmi
+7  A: 

Not in MySQL, no. You could do this:

SELECT 1 + 1;

It will be in some DBMSs though - Oracle for example would require you to do:

SELECT 1 + 1 FROM DUAL;
David M
FYI, sql server allows SELECT x as well
KM
A: 

(EDIT: Missed the the MYSQL tab.)

This depends on the database.

In Oracle, IIRC, the from is required. But Oracle has a table DUAL which always returns one row for cases where all the work is being done in the SELECT clause.

On the other hand, SQL Server does not require the FROM, to return the value of a variable just select it:

SELECT @myVar
Richard
A: 

As mentioned by ocedecio FROM is not required in every statement.

For example I tend to use the following syntax to initialise a table with data

insert into status(code, description)
select 'O', 'Open'
union all select 'C', 'Closed'
union all select 'P', 'Parked'

etc..

EDIT (following comments by gamecat)

The example above demonstrates when SELECT without FROM may have a practical use. You can of course just call:

select 'O', 'Open'
union all select 'C', 'Closed'
union all select 'P', 'Parked'
kristof
insert != select ;-)
Gamecat
yep, but what you can use the select part separately, I just gave a practical example where it can be used
kristof
A: 

Basically in SQL server 2005 FROM keyword is not required for Scalar value functions and system functions but it mandatory for table valued functions.

Scalar valued functions can executable like below syntax

Select functionname(arguments)

Table valued functions can executable like below syntax

Select * from functionname(arguments)

else

Select col1,col2 from functionname(argument1,argument2)

Kthevar