O'Reilly logo

Facebook Cookbook by Jay Goldman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

FQL Query Structure

Problem

What’s the basic structure of an FQL query?

Solution

FQL mimics SQL directly here, so the basic structure is:

SELECT [fields] FROM [table] WHERE [conditions]

In addition, you can also make use of SQL-like ORDER BY and LIMIT clauses:

SELECT [fields] FROM [table] WHERE [conditions] ORDER BY
 [field] LIMIT [offset], [rowcount]

Discussion

Here are a few noteworthy differences between SQL and FQL that might trip you up if you’re used to the former and not the latter:

  • Most significantly, the FROM clause in FQL can include only a single table, so there’s no official support for joins of any kind. You can sort of get around this by using subqueries, but note that the subqueries can’t reference variables from the outer query’s scope. An example of a very useful subquery: requesting all of the friends of the current user who have already installed your application so that you can exclude them from an fb:multi-friend-selector control:

    SELECT uid FROM user WHERE has_added_app=1 and uid IN
     (SELECT uid2 FROM friend WHERE uid1 = $user)
  • In order for Facebook to provide direct database access for apps, all queries have to be indexable so that they don’t impose huge performance hits on the server. Facebook maintains a relatively short list of 17 tables that you can do queries on; see Indexed Facebook Tables and Fields for details.

Note

Indexing is a technique in which a lookup index is created on a specific column in a table in order to speed up queries where that column is in the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required