Facebook Developers
DocumentationSupportBlogAppsLog In
  • Getting Started
  • Core Concepts
  • Advanced Topics
    • Dialogs
    • FQL
    • Internationalization
    • Ads API
    • Credits
    • Chat API
    • Legacy REST API
    • Legacy FBML
    • Legacy FBJS
    • Legacy Javascript SDK
  • SDK Reference
  • Tools
  • Tables
    • album
    • application
    • apprequest
    • checkin
    • comment
    • comments_info
    • connection
    • cookies
    • developer
    • domain
    • domain_admin
    • event
    • event_member
    • family
    • friend
    • friend_request
    • friendlist
    • friendlist_member
    • group
    • group_member
    • insights
    • like
    • link
    • link_stat
    • location_post
    • mailbox_folder
    • message
    • note
    • notification
    • object_url
    • offer
    • page
    • page_admin
    • page_blocked_user
    • page_fan
    • page_milestone
    • permissions
    • permissions_info
    • photo
    • photo_src
    • photo_tag
    • place
    • privacy
    • privacy_setting
    • profile
    • profile_view
    • question
    • question_option
    • question_option_votes
    • review
    • standard_friend_info
    • standard_user_info
    • status
    • stream
    • stream_filter
    • stream_tag
    • subscription
    • thread
    • translation
    • unified_message
    • unified_thread
    • unified_thread_action
    • unified_thread_count
    • url_like
    • user
    • video
    • video_tag

Facebook Query Language (FQL)

Advanced Topics › Facebook Query Language (FQL)

The FQL object enables running FQL queries using the Graph API. Facebook Query Language, or FQL, enables you to use a SQL-style interface to query the data exposed by the Graph API. It provides for some advanced features not available in the Graph API, including batching multiple queries into a single call.

Example

Runs the query "SELECT uid2 FROM friend WHERE uid1=me()"

GET /fql?q=SELECT+uid2+FROM+friend+WHERE+uid1=me()&access_token=...

Read

You can issue a HTTP GET request to /fql?q=query where query can be a single fql query or a JSON-encoded dictionary of queries.

Query

Queries are of the form SELECT [fields] FROM [table] WHERE [conditions]. Unlike SQL, the FQL FROM clause can contain only a single table. You can use the IN keyword in SELECT or WHERE clauses to do subqueries, but the subqueries cannot reference variables in the outer query's scope. Your query must also be indexable, meaning that it queries properties that are marked as indexable in the documentation below.

FQL can handle simple math, basic boolean operators, AND or NOT logical operators, and ORDER BY and LIMIT clauses.

For any query that takes a uid, you can pass me() to return the logged-in user. For example:

SELECT name FROM user WHERE uid = me() 

Other functions that are available are now(), strlen(), substr() and strpos().

Here's an example of a subquery that fetches all user information for the active user and friends:

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

Multi-query

Evaluates a series of FQL (Facebook Query Language) queries in one call and returns the data at one time.

This method takes a JSON-encoded dictionary called ''queries'' where the individual queries use the exact same syntax as a simple query. However, this method allows for more complex queries to be made. You can fetch data from one query and use it in another query within the same call. The WHERE clause is optional in the latter query, since it references data that’s already been fetched. To reference the results of one query in another query within the same call, specify its name in the FROM clause, preceded by #.

For example, say you want to get some data about a user attending an event. Normally, you’d have to perform two queries in a row, waiting for the results of the first query before running the second query, since the second query depends on data from the first one. But with fql.multiquery, you can run them at the same time, and get all the results you need, giving you better performance than running a series of fql.query calls. First, you need to get the user ID and RSVP status of each attendee, so you’d formulate the first query – query1 – like this:

"query1":"SELECT uid, rsvp_status FROM event_member WHERE eid=12345678"

Then to get each attendee’s profile data (name, URL, and picture in this instance), you’d make a second query – query2 – which references the results from query1. You formulate query2 like this:

"query2":"SELECT name, url, pic FROM profile WHERE id IN (SELECT uid FROM #query1)"

This method also has better performance than running a series of fql.query calls with batch.run.

Sample

Here's a sample for running fql query

<?php
  $app_id = 'YOUR_APP_ID';
  $app_secret = 'YOUR_APP_SECRET';
  $my_url = 'POST_AUTH_URL';

  $code = $_REQUEST["code"];
 
 //auth user
 if(empty($code)) {
    $dialog_url = 'https://www.facebook.com/dialog/oauth?client_id=' 
    . $app_id . '&redirect_uri=' . urlencode($my_url) ;
    echo("<script>top.location.href='" . $dialog_url . "'</script>");
  }

  //get user access_token
  $token_url = 'https://graph.facebook.com/oauth/access_token?client_id='
    . $app_id . '&redirect_uri=' . urlencode($my_url) 
    . '&client_secret=' . $app_secret 
    . '&code=' . $code;
  $access_token = file_get_contents($token_url);
 
  // Run fql query
  $fql_query_url = 'https://graph.facebook.com/'
    . '/fql?q=SELECT+uid2+FROM+friend+WHERE+uid1=me()'
    . '&' . $access_token;
  $fql_query_result = file_get_contents($fql_query_url);
  $fql_query_obj = json_decode($fql_query_result, true);

  //display results of fql query
  echo '<pre>';
  print_r("query results:");
  print_r($fql_query_obj);
  echo '</pre>';

  // Run fql multiquery
  $fql_multiquery_url = 'https://graph.facebook.com/'
    . 'fql?q={"all+friends":"SELECT+uid2+FROM+friend+WHERE+uid1=me()",'
    . '"my+name":"SELECT+name+FROM+user+WHERE+uid=me()"}'
    . '&' . $access_token;
  $fql_multiquery_result = file_get_contents($fql_multiquery_url);
  $fql_multiquery_obj = json_decode($fql_multiquery_result, true);

  //display results of fql multiquery
  echo '<pre>';
  print_r("multi query results:");
  print_r($fql_multiquery_obj);
  echo '</pre>';
?>

    Tables

    album

    Query this table to return information about a photo album.

    application

    Query this table to return read-only properties about an application.

    apprequest

    An FQL table containing the requests sent via an app to a user.

    checkin

    Query this table to return information about a checkin. By default, this query returns the last 20 checkins and returns a maximum of 500 checkins.

    comment

    Query this table to obtain comments associated with one or more [fb:comments]

    comments_info

    The comments_info FQL table. Query this table to obtain XIDs for fb:comments objects associated with an application ID.

    connection

    Query this table to return a user's friends and the Facebook Pages to which the user is connected.

    cookies

    Query this table to return information about a cookie.

    developer

    Query this table to return the application IDs for which the specified user is listed as a developer in the Developer application.

    domain

    The domain table provides a read-only mapping between domain names and ids.

    domain_admin

    Query this table to return information about the admin of a domain.

    event

    Query this table to return information about an event.

    event_member

    Query this table to return information about a user's status for an event or see a list of events for a user.

    family

    Query this table to return detailed information about a user's family.

    friend

    Query this table to determine whether two users are linked together as friends.

    friend_request

    Query this table either to determine which users have sent friend requests to the logged-in user or to query whether a friend request has been sent from the logged-in user to a specific user.

    friendlist

    Query this table to return any friend lists owned by the specified user.

    friendlist_member

    Query this table to determine which users are members of a friend list.

    group

    Query this table to return information about a group.

    group_member

    Query this table to return information about the members of a group, or retrieve a list of groups of which a user is a member

    insights

    The insights table contains statistics about Applications, Pages and Domains

    like

    Query this table to return the user IDs of users who like a given Facebook object (video, note, link, photo, or album).

    link

    Query this table to return the links a user has posted.

    link_stat

    The link_stat table contains counts that show how users on Facebook are interacting with a given link.

    location_post

    An FQL table representing posts that are tagged with a location.

    mailbox_folder

    The mailbox_folder table contains information about a user's mailbox folders.

    message

    Query this table to return information about messages in a thread.

    note

    Query this table to return the notes the current user has written or to return details for a particular note.

    notification

    Query this table to get the notifications for the current session user, that is, any notification that appears on http://www.facebook.com/notifications.php.

    object_url

    Query this table to return information about a URL in the Open Graph

    offer

    Query this table to return information about an offer.

    page

    Query this table to return information about a Facebook Page.

    page_admin

    Query this table to return information about which Facebook Pages the user Admins.

    page_blocked_user

    An FQL table that can be used to return a list of a users that are blocked from a Facebook Page.

    page_fan

    Query this table to return information about the user who likes a Facebook Page.

    page_milestone

    Query this table to return information about a Page's milestones.

    permissions

    Query this table to return the permissions the current user has granted to the app.

    permissions_info

    Query this table to return more descriptive information about extended permissions.

    photo

    Query this table to return information about a photo.

    photo_src

    Query this table to return information about photo source url.

    photo_tag

    Query this table to return information about a photo tag.

    place

    Query this table to return information about a place.

    privacy

    Query this table to return a user's privacy setting for a given object_id.

    privacy_setting

    Query default privacy settings for a user for a particular app

    profile

    Query this table to return certain (typically publicly) viewable information for a profile.

    profile_view

    Query this table to return information a Page's apps.

    question

    A Question as represented in FQL.

    question_option

    An option for a question, as represented in FQL.

    question_option_votes

    The votes on a particular option for a question, as represented in FQL.

    review

    Query this table to obtain reviews associated with an application, a user or both.

    standard_friend_info

    Query this table to determine whether two users are linked together as friends.

    standard_user_info

    Query this table to return standard information about a user, for use when you need analytic information only.

    status

    Query this table to return one or more of a user's statuses.

    stream

    Query this table to return posts from a user's stream or the user's profile.

    stream_filter

    Query this table to return a filter_key that can be used to query the stream FQL table, as seen through any content filters the user has available on Facebook.

    stream_tag

    Query this table to return associations between users or Facebook Pages and the items they tag in status posts.

    subscription

    Query this table to determine whether two users are linked together in a subscriber/subscribee relationship and to retrieve a list of users another user subscribes to or is subscribed to by.

    thread

    Query this table to return information about message threads in a user's Inbox.

    translation

    Query this table to return the native strings (original, untranslated text in your application interface) and the translated strings for your application.

    unified_message

    This table can be used to access information about messages in the new Facebook messaging system.

    unified_thread

    This table can be used to access information about threads in the new Facebook messaging system.

    unified_thread_action

    This table should be used to access information about subscribe and unsubscribe actions performed on a thread in the new Facebook messaging system.

    unified_thread_count

    This table should be used to access information about the number of threads in a folder in the new Facebook messaging system.

    url_like

    An FQL table containing the Open Graph URLs that the current session user has Liked.

    user

    Query this table to return detailed information from a user's profile.

    video

    The video table contains information about videos.

    video_tag

    The video_tag table contains information about users tagged in videos.

    Updated about 4 months ago
    Facebook © 2012 · English (US)
    AboutCareersPlatform PoliciesPrivacy Policy