Back to course

Basic Arithmetic, Concatenation, and Column Aliases

Database Fundamentals: From Zero to Hero

Lesson 18: Basic Arithmetic, Concatenation, and Column Aliases

SQL isn't just for retrieval; it can perform calculations and reformat output directly within the SELECT statement.

1. Performing Calculations

You can use standard arithmetic operators (+, -, *, /) on numeric columns.

Example: Calculating Total Price

If we have a Price column and a TaxRate column (0.10 for 10%):

sql SELECT Name, Price, Price * 0.10 AS TaxAmount, -- Calculate 10% tax Price * 1.10 AS TotalWithTax -- Calculate final price FROM Products;

2. Column Aliases (AS)

An Alias is a temporary name given to a column or calculated field in the result set. We use the AS keyword.

  • Aliases are essential for making calculated fields readable.

3. Concatenation

Concatenation is the process of joining two or more text strings together. The syntax for concatenation varies by database:

  • Standard SQL/PostgreSQL/Oracle: Uses the || operator.
  • SQL Server/MySQL: Uses the CONCAT() function.

Example: Creating a Full Name Field (Using ||)

sql SELECT FirstName || ' ' || LastName AS FullName, Email FROM Employees;

This query combines FirstName, a space, and LastName into a single result column named FullName.