Search code examples
sqlsql-servert-sqlderived-table

Unfamiliar character in SQL statement


This is sort of SQL newbie question, I think, but here goes.

I have a SQL Query (SQL Server 2005) that I've put together based on an example user-defined function:

SELECT 
    CASEID,
    GetNoteText(CASEID)
FROM 
( 
    SELECT 
        CASEID 
    FROM 
        ATTACHMENTS 
    GROUP BY 
        CASEID 
) i
GO 

the UDF works great (it concatenates data from multiple rows in a related table, if that matters at all) but I'm confused about the "i" after the FROM clause. The query works fine with the i but fails without it. What is the significance of the "i"?

EDIT: As Joel noted below, it's not a keyword


Solution

  • When you use a subquery in the FROM clause, you need to give the query a name. Since the name doesn't really matter to you, something simple like 'i' or 'a' is often chosen. But you could put any name there you wanted- there's no significance to 'i' all by itself, and it's certainly not a keyword.

    If you have a really complex query, you may need to join your sub query with other queries or tables. In that case the name becomes more important and you should choose something more meaningful.