AS

Calculated columns and AS

SQLite in your browser
Lesson concept What you'll do
1 / 4

Work out a new column from the others, and give it a name.

Write the sum in the SELECT list, like a formula, and name it with AS.

The database
9 rows
book_idtitleauthorgenreyearpagescopieson_loan
1Pride and PrejudiceJane AustenRomance181343222
2The Great GatsbyF. Scott FitzgeraldFiction192518066
3DraculaBram StokerHorror189741855
4MockingjaySuzanne CollinsScience Fiction201039066
5The Time MachineH.G. WellsScience Fiction189511810
6Angels & DemonsDan BrownThriller200061662
7Charlotte's WebE.B. WhiteChildren195218463
8The Fault in Our StarsJohn GreenRomance201231364
9The Fellowship of the RingJ.R.R. TolkienFantasy195442311
Click a name to type it
Your query
 
Hint: click the green edit button on the board, or type it yourself
Query output Ready to run

Run your query to see the result table.

Press the green Run button or Ctrl+Enter.

Study notes

How AS works

  • Can a column be worked out?

    Yes. Anything after SELECT can be a calculation: copies - on_loan, pages / 30, 2026 - year. It's worked out for every row, like a formula filled down a column in Excel.

  • Which maths signs work?

    + add, - subtract, * multiply, / divide. Brackets work as in maths: (copies - on_loan) * 2.

  • What does AS do?

    copies - on_loan AS available

    AS names a column in the result. Without it the heading is the sum itself, which is hard to read and hard to use later.

  • Does it change the table?

    No. A calculated column exists only in the result. To store values in the table you'd use UPDATE, which comes later.

  • How is this like Excel?

    In Excel you'd type =G2-H2 in a new column and fill it down. In SQL you write the sum once, and it's worked out for every row.

Other databases

How other databases do it

The lessons run on SQLite. Most of SQL is the same everywhere; these are the places where this lesson's SQL differs.

What does 7 / 2 give?
DatabaseHowNote
SQLite (these lessons)3Dividing two whole numbers gives a whole number. Write 7.0 / 2 for 3.5.
PostgreSQL3Whole numbers give a whole number. Write 7.0 / 2 or 7::numeric / 2 for 3.5.
MySQL / MariaDB3.5000/ always gives a decimal. DIV gives the whole-number answer.
SQL Server3Whole numbers give a whole number. Write 7.0 / 2 for 3.5.
Oracle3.5Numbers are decimals, so / gives 3.5. TRUNC(7 / 2) gives 3.
Sybase ASE3Whole numbers give a whole number. Write 7.0 / 2 for 3.5.
Using the AS name in WHERE
DatabaseHowNote
SQLite (these lessons)WHERE available > 0Allowed: SQLite looks the name up in SELECT. Other databases refuse it, so repeat the calculation instead.
PostgreSQLErrorWHERE runs before SELECT. Write WHERE copies - on_loan > 0.
MySQL / MariaDBErrorUnknown column. Repeat the calculation in WHERE.
SQL ServerErrorInvalid column name. Repeat the calculation in WHERE.
OracleErrorInvalid identifier. Repeat the calculation in WHERE.
Sybase ASEErrorRepeat the calculation in WHERE.
A name with a space in it
DatabaseHowNote
SQLite (these lessons)AS "On the shelf"Double quotes. [ ] and backticks are accepted too.
PostgreSQLAS "On the shelf"Double quotes. Without them, names are folded to lower case.
MySQL / MariaDBAS `On the shelf`Backticks (double quotes only with the ANSI_QUOTES mode).
SQL ServerAS [On the shelf]Square brackets, or double quotes.
OracleAS "On the shelf"Double quotes. Without them, names are folded to capitals.
Sybase ASEAS "On the shelf"Double quotes need quoted_identifier on. Easiest: no spaces (on_shelf).

Know someone fighting with a spreadsheet? Send them this page.