#

Jump to Table of Contents

# YQL Query

The Yahoo! Query Language (YQL) is an expressive SQL-like language that lets you query, filter, and join data across Web services. With YQL, apps run faster with fewer lines of code and a smaller network footprint.

Yahoo! and other websites across the Internet make much of their structured data available to developers, primarily through Web services. To access and query these services, developers traditionally endure the pain of locating the right URLs and documentation to access and query each Web service.

With YQL, developers can access and shape data across the Internet through one simple language, eliminating the need to learn how to call different APIs.

## Getting Started

To include the source files for YQL Query and its dependencies, first load the YUI seed file if you haven't already loaded it.

    <script src="http://yui.yahooapis.com/3.10.3/build/yui/yui-min.js"></script>

Next, create a new YUI instance for your application and populate it with the modules you need by specifying them as arguments to the `YUI().use()` method. YUI will automatically load any dependencies required by the modules you specify.

    <script>
    // Create a new YUI instance and populate it with the required modules.
    YUI().use('yql', function (Y) {
        // YQL Query is available and ready for use. Add implementation
        // code here.
    });
    </script>

For more information on creating YUI instances and on the `use()` method, see the documentation for the YUI Global Object.

## Making a Query

After you find the query you want to run in the Developer Console, just plug it in:

The YQL Developer Console is a nice place to start playing with YQL queries. You can test and inspect queries before you actually use them.

    YUI().use('yql', function(Y) {

        Y.YQL('select * from weather.forecast where location=90210', function(r) {
            //r now contains the result of the YQL Query
    		//use the YQL Developer console to learn
    		//what data is coming back in this object
    		//and how that data is structured.
        });

    });

## Re-Using A Query

Modifying the example above to make it reusable is simple, just save the result of the query and call `send` on it to query for the results again.

    YUI().use('yql', function(Y) {

        var q = Y.YQL('select * from weather.forecast where location=90210', function(r) {
            //r now contains the result of the YQL Query
        });

        //Sometime later

        q.send();

    });

## Changing A Re-Used Query

Changing a query without creating a new instance can be beneficial for queries that involve the same request but different parameters (like an AutoComplete query).

To do this, we need to modify the private param `q` on the YQL instance.

    YUI().use('yql', function(Y) {

        var q = Y.YQL('select * from weather.forecast where location=90210', function(r) {
            //r now contains the result of the YQL Query
        });

        //Sometime later
        q._params.q = 'select * from weather.forecast where location=62959';
        q.send();

    });

## Open Data Tables

Open Data Tables enable developers to add tables for any data on the Web to YQL's stable of API-specific tables. Using Open Data Tables, anyone can make their data YQL-accessible. If you would like to create an Open Data Table, visit the community page at http://datatables.org.

By default, the `YQL` module will include the environment file needed to use all of the publicly available Open Data Tables.

## Advanced Options

The default configuration for the `YQL` module is designed for the most basic use cases. However, the `YQLRequest` class is designed to give the developer more control over the query, parameters and options used to make the YQL Request.

    YUI().use('yql', function(Y) {

        var q = new Y.YQLRequest('select * from weather.forecast where location=90210', function(r) {
            //r now contains the result of the YQL Query
        }, {
            //Optional URL Parameters to add to the request
            foo: 'bar',
            another: 'option'
        }, {
            //Options
            base: '://query.yahooapis.com/v1/yql?', //Different base URL for private data
            proto: 'https' //Connect using SSL
        });
        q.send();

    });