Sunday, April 17, 2011

FROM clause necessary in every SELECT statement

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

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

    Examples from here:

    mysql> SELECT 1 + 1; -> 2

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

    SELECT 1+1
    
  • 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;
    
    KM : FYI, sql server allows SELECT x as well
  • (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
    
  • 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'
    
    Gamecat : insert != select ;-)
    kristof : yep, but what you can use the select part separately, I just gave a practical example where it can be used
  • 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)

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.