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 ...

Get Facebook Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.