Today a post on FQL (Facebook Query Language). I will give you some examples that hopefully show the power of this SQL-like querying interface of the Facebook API.

So in short: FQL lets you query Facebook's Graph API (what is the Graph API?).

Some use cases:

1. get all comments for a specific URL

For example to see the comments on this book that was added to My Reading List:


SELECT post_fbid, fromid, object_id, text, time FROM comment
WHERE object_id IN (SELECT comments_fbid FROM link_stat WHERE url =

This returns:

   {"post_fbid": "10150238413182479",
      "fromid": 628517118,
      "object_id": 10150208342007479,
      "text": "worth every penny, one of the best css titles I have read.",
      "time": 1310773947

(more info on comments and replies per comment)

2. get all members of my BobsBlog Facebook Group


SELECT uid, name, pic_square FROM user WHERE uid = me()
OR uid IN (select uid from group_member where gid = 118232854902671)
order by name

3. get all the music your friends like - I used this one in

(requires extended permission: friends_likes)


SELECT uid, name, pic_square,music FROM user WHERE uid = me()
OR uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) order by name

!! Note the batching of multiple queries into a single call !!

Snippet output:

... you can do the same for movies - query:

SELECT uid,name,pic_small,movies FROM user WHERE uid = me()
OR uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) order by name

4. get all birthdays of my friends of this month

(requires extended permission: friends_birthday)


SELECT uid, name,pic_square, birthday_date FROM user
WHERE (substr(birthday_date, 0, 2) = "07") AND uid IN
(SELECT uid2 FROM friend WHERE uid1 = me()) order by name

!! Note the use of substr in this query !!
Other functions that are available are now(), strlen(), substr() and strpos().

Fetch URL to execute the query

XML and JSON are supported formats. I am using JSON for this example.
$oauth_token is obtained by the Facebook's Authentication: you need to create a Facebook App first and get the user to login and grant permission to your app (I will do a followup post ...)

$query = "";
$query .= urlencode("<above query>"); 

$query .= "&access_token=".$oauth_token."&format=json";
$out = file_get_contents($query);
$response = json_decode($out, true);

echo '<pre>';print_r($response);echo '</pre>';

FQL Test Console

There is a great test console where you can play with the queries. With another user account FQL console gave me "Something went wrong. We're working on getting it fixed as soon as we can."  It seems you need to have an Application setup and the test user might need to have granted the extra permissions him/herself.

A handy trick is to click on the URL in the FQL console, it will open a new tab with the URL to fetch. For example 1 that would be:

FQL Tables

Of course a lot more is possible, see all Tables. The only limitation is that you can only query indexable properties (columns)

What's next?

In the next post I will iterate over example 4. (birthday calendar) to show you how to use FQL as a workhorse for the input of your Facebook app. I will also cover some more aspects of the Facebook API like authorization and dialogs ...

From here on ...

You can subscribe to this blog, join my Facebook group, comment on this post below, or find me on twitter @bbelderbos.

By the way, if you have interesting topics you would like to see covers, just drop me a message, I am open to anything web-related.

Bob Belderbos

Software Developer, Pythonista, Data Geek, Student of Life. About me