What does "foo" mean in this SQL Server Query?

John picture John · Jun 1, 2010 · Viewed 13.1k times · Source

for eg...

SELECT * 
  FROM ( SELECT RANK() OVER (ORDER BY stud_mark DESC) AS ranking,
                stud_id, 
                stud_name, 
                stud_mark 
           FROM tbl_student ) AS foo 
 WHERE ranking = 10

Here foo is present...actually what it does ?..

Answer

OMG Ponies picture OMG Ponies · Jun 1, 2010

In this example, foo is a table alias. Here's how you'd use it:

SELECT foo.* 
  FROM ( SELECT RANK() OVER (ORDER BY ts.stud_mark DESC) AS ranking,
                ts.stud_id, 
                ts.stud_name, 
                ts.stud_mark 
           FROM tbl_student ts) AS foo 
 WHERE foo.ranking = 10

SQL Server (and MySQL for that matter) will throw an error if you do not specify a table alias for a derived table (AKA inline view).