Retrieving data from tables on a web page

In order to query tables within web pages, you have a number of options. The examples illustrated below are querying some sample tables on this page. Each example links back to the demo sandbox for you to try yourself. By default, the first table on the page is retrieved, but you can query for specific tables within a page with four different methods. So if a web page only has one table you can simply supply the web page's URL as shown below.

SELECT * FROM "https://www.usesql.com/samples/tables" [Example In Sandbox]

Method 1: Table Index

The easiest way to explore a page likely will be with this method. This is a zero-based index so the first table will be 0. You just need to append a hashtag, and the index enclosed with square brackets. Below is an example using this method.

SELECT * FROM "https://www.usesql.com/samples/tables#[0]" [Example In Sandbox]

Here is another example of getting the second table on the webpage.

SELECT * FROM "https://www.usesql.com/samples/tables#[1]" [Example In Sandbox]

Method 2: Table Class

You can also select a table using its class attribute as defined in its html. In order to this you just need to append a hashtag, and a period preceding the class name. Below is an example using this method.

SELECT * FROM "https://www.usesql.com/samples/tables#.sample-table" [Example In Sandbox]

This method can be combined with using the table index so if there are two tables with the class sample-table you would use the query below.

SELECT * FROM "https://www.usesql.com/samples/tables#.sample-table[1]" [Example In Sandbox]

Method 3: Table String Match

Another way to choose a table that you want to query is to append #+ to the end of the website URL with a string that you see in the table.

SELECT * FROM "https://www.usesql.com/samples/tables#+Weekend" [Example In Sandbox]

Like with the html class attribute selector this method can be combined with selecting a table index as more than one table may have the string that's being supplied. The example below illustrates this.

SELECT * FROM "https://www.usesql.com/samples/tables#+Title[1]" [Example In Sandbox]

Method 4: Table Id

If you own the page the most reliable way to query a specific table is likely to be this method. You can use a table's html attribute id to indicate to UseSQL which table you want to query. An example can be shown for a table that has an id="top-titles".

SELECT * FROM "https://www.usesql.com/samples/tables#top-titles" [Example In Sandbox]