SQL Basics

Chapter 7 - Calculated Fields

  • Calculated fields contain values that are not stored directly in the database
  • They are created by combining or calculating values that are stored in the database
  • Combining text fields is called concatenation
  • Examples of combined fields: street + house number, postal code + town
SELECT <column1 en column2>
FROM   <table>
  • Values can also be calculated mathematically
  • Examples of mathematical calculations: years of service, age, totals and averages
SELECT <column1 gedeeld door column2>
FROM   <table>
  • Although these values are not stored ready-made in the database, it is often preferable to calculate them in SQL rather than in the client application
DBMS Syntax
SQL Server, MS Access +
DB2, Oracle, PostgreSQL, SQLite ||
MySQL, MariaDB function: concat

CONCAT is a function. Functions are covered in the next chapter.



SELECT <text1> + <text2>

SELECT <text1> || <text2>

SELECT CONCAT(text1, text2)

Select vend_name and vend_country from Vendors and combine them. Put parentheses around vend_country and sort the result

-- MySQL - MariaDB
SELECT   CONCAT(vend_name,' (',vend_country,')')
FROM     Vendors
ORDER BY vend_name;
CONCAT(vend_name, ' (',vend_country,')')
Bear Emporium (USA)
Bears R Us (USA)
Doll House Inc. (USA)
Fun and Games (England)
Furball Inc. (USA)
Jouets et ours (France)


  • If a field contains unwanted spaces, use a TRIM function when concatenating:
    • RTRIM( ) removes trailing spaces
    • LTRIM( ) removes leading spaces
    • TRIM( ) removes both leading and trailing spaces
-- Oracle - DB2 - PostgreSQL - SQLite	
SELECT   TRIM(vend_name) || ' (' || TRIM(vend_country) || ')'
FROM     Vendors
ORDER BY vend_name;
-- SQL Server	
SELECT   TRIM(vend_name) + ' (' + TRIM(vend_country) + ')'
FROM     Vendors
ORDER BY vend_name;
  • Use an alias to name a calculated field (a new column)
  • Client applications can refer to an alias
  • Create an alias by adding the AS keyword to the SELECT statement
SELECT <value1> + <value2> AS <NewColumnName> 	
FROM   <table>
  • Avoid spaces in an alias
  • Aliases are often used to provide names without spaces for existing columns

Select vend_name and vend_country from Vendors and combine them into a column named 'VendTitle'. Put parentheses around vend_country and sort the result

-- Oracle
SELECT   TRIM(vend_name) || ' (' || TRIM(vend_country) || ')' AS VendTitle
FROM     Vendors
ORDER BY vend_name;	
-- MySQL
SELECT   CONCAT(vend_name, ' (', vend_country, ')') AS VendTitle
FROM     Vendors
ORDER BY vend_name;
-- SQL Server
SELECT   TRIM(vend_name) + ' (' + TRIM(vend_country) + ')' AS VendTitle
FROM     Vendors
ORDER BY vend_name;	
VendTitle
Bear Emporium (USA)
Bears R Us (USA)
Doll House Inc. (USA)
Fun and Games (England)
Furball Inc. (USA)
Jouets et ours (France)
  • The following operators can be used in SQL

Operator Description
+ Addition
- Subtraction
/ Division
* Multiplication

Use parentheses ( ) to specify the order of operations in a calculation


-- MySQl / SQL Server	
SELECT 8*2+4-10/2;
-- Oracle (19c & 21C)	
SELECT 8*2+4-10/2
FROM DUAL;

SELECT <column1> * <column2> AS <column3>
FROM   <table>;

Select the product ID, quantity and price from OrderItems. Add a column that multiplies quantity by price and name it 'TotaalPrijs'. Sort by TotaalPrijs.

-- Oracle / MySQL / SQL Server	
SELECT   prod_id,
         quantity,
         item_price,
         quantity * item_price AS TotaalPrijs
FROM     OrderItems
ORDER BY TotaalPrijs;
prod_idquantityitem_priceTotaalPrijs
RGAN0154.9924.95
BNBG01103.4934.9
BNBG02103.4934.9
BNBG03103.4934.9
BR03511.9959.95
BR02108.9989.9
BR01205.99119.8
BR031011.99119.9
RGAN01504.49224.5
BNBG011002.99299
BNBG021002.99299
BNBG031002.99299
BR011005.49549
BR035011.49574.5
BNBG012502.49622.5
BNBG022502.49622.5
BNBG032502.49622.5
BR0310010.991099
  1. Select the product names and prices, and add fields for VAT (21%) and the price including VAT. Sort by product name (9)

TIP

  1. Select the product name and price from Products
  2. Rename the price column 'PrijsExcl'
  3. Add a column by multiplying the product price by 0.21 and name it 'BTW'
  4. Add a column by multiplying the product price by 1.21 and name it 'PrijsIncl'


  1. Select the customer name and add a column combining the state abbreviation and postal code, separated by a space. Name it 'StateZip'. Sort by customer name. (5)


  1. Select the vendor names and add a column containing city and country. Name it 'locatie' and sort by the new column. (6)


  1. Select all order numbers, quantities and prices. Add a column containing quantity × price and name it 'totaal'. Display only rows with a total above $224.50 (9)


  1. Select the product ID and price. Add a column with a 10% discount on the original price and name it 'ActiePrijs'. Sort by the new column. (9)