SQL SELECT FROM statement

Projection

The weather table stores information about the weather in London for the past five days and looks like this:

A query that selects only the basic info to be displayed on a mobile phone screen, for example, day, hour, weather phenomena, temperature, feels like, and wind speed, will look like this:

SELECT
    day, 
    hour,
    phenomena,
    temperature AS "temperature in Celsius",
    feels_like AS "feels like in Celsius",
    w_speed AS "wind speed in m/s"
FROM
    weather
;

After the SELECT keyword, list the columns to select and specify aliases where needed.

The query evaluation results in the following table:

The type of data extraction selected for a subset of table columns is called projection.

The general layout for such queries: the SELECT statement, list of column names with optional aliases, FROM, table name, and a semicolon to mark the end of the statement:

SELECT
    col1 [AS alias1], ..., colN [AS aliasN]  
FROM
    table_name
;

Expressions

To have different results based on the same data, add columns that state the place and show the temperature in Fahrenheit:

The query below does this:

SELECT
    'London' AS place,
    day, 
    hour,
    phenomena,
    temperature*9/5+32 AS "temperature in Fahrenheit",
    feels_like < temperature AS "feels colder",
    wind_speed AS "wind speed in m/s"
FROM
    weather
;

When the data management system executes the query, it will substitute the column names with the corresponding attribute value for each processed row.

Logical table

A data management system hides how the data is physically stored behind an abstract concept of a logical table. To run a query, it is required to know the database schema—table names, column names, and types—and appropriate access permissions. Internally, the query processor maps table and column references from queries to physical data such as files, network connections, and even the results of executing other queries.

SQL SELECT FROM template

The overall template for statements that extract data from a table and evaluate expressions in it consists of the SELECT keyword, list of expressions with optional aliases, FROM, table name, and a semicolon to mark the end of the statement.

SELECT
    exp1 [AS alias1], ..., expN [AS aliasN]  
FROM
    table_name
;

Create a free account to access the full topic

“It has all the necessary theory, lots of practice, and projects of different levels. I haven't skipped any of the 3000+ coding exercises.”
Andrei Maftei
Hyperskill Graduate