SQL: Selecting Expressions
Welcome to the Selecting Expressions lesson!
This lesson is shown as static text below. However, it's designed to be used interactively. Click the button below to start!
We can
SELECTmany kinds of expressions: columns, like we've seen already; but also constants, mathematical expressions, and function calls. For example, if weSELECT 1, we get 1 back. If weSELECT 1 + 1, we get 2 back.>
exec(`SELECT 1`);Result:
>
exec(`SELECT 1 + 1`);Result:
The output here is the same as ever: an array of objects. But now, there are no column names. Instead, the properties on the "row" object are the expressions that we queried.
>
exec(`SELECT 2 * 3`);Result:
[{'2 * 3': 6}]We can also query functions that return values dynamically. For example, the current date is written as
DATE('now')in SQLite. This query asks: is the current date afterDATE(0), which was January 1, 1970?>
exec(`SELECT DATE('now') > DATE(0)`);Result:
Here's a code problem:
Select the number 17.
exec(`SELECT 17`);- Goal:
[{'17': 17}]- Yours:
[{'17': 17}]