Execute Program

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 SELECT many kinds of expressions: columns, like we've seen already; but also constants, mathematical expressions, and function calls. For example, if we SELECT 1, we get 1 back. If we SELECT 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}]Pass Icon
  • 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 after DATE(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}]Pass Icon