FROM
The FROM clause is one of the fundamental building blocks of a SQL SELECT statement. In this article we'll explain how it works.
The FROM clause is formed of the reserved 'from' keyword followed by either:
- A table name (most common)
- A subquery
- A JOIN clause (read more about the JOIN clause)
The sources defined in the FROM clause determine the set of rows that the SELECT statement can operate on, and so is the first part of the query to be executed.
In this article, we'll look at some examples of different FROM clauses.
In this article the code snippets are written in the Google BigQuery Standard SQL syntax.
FROM a table
The simplest FROM clause just lists a single table name - then the entire SELECT statement is operating on a single table.
| strings | numbers |
|---|---|
| a | 1 |
| b | 2 |
| c | 3 |
FROM a subquery
Instead of referring to a table by name, we can also refer to it as the result of a SELECT statement (since SELECT statements return tables). For example, we can rewrite the query above as
| strings | numbers |
|---|---|
| a | 1 |
| b | 2 |
| c | 3 |
or indeed as
| strings | numbers |
|---|---|
| a | 1 |
| b | 2 |
| c | 3 |
These subqueries can be as complex as you'd like, though for the sake of readability you should try to avoid putting too much logic into a subquery. In a Count notebook we encourage splitting complex queries into separate cells for this reason.
Including a JOIN clause
When you want to select rows from multiple tables, you'll need to tell the database how to merge the tables together (read more here). These instructions take the form of one or more JOIN clauses after the initial FROM clause. For example:
| numbers | numbers_2 |
|---|---|
| 1 | 4 |
| 2 | 5 |
| 3 | 6 |
In the query above, our SELECT statement says that:
- We want the
numbersandnumbers_2columns (the database is smart enough to know which tables those columns come from) FROMtable_1andtable_2- The rows of
table_2should be associated with those fromtable_1by comparing the columnsstringsandstrings_2