Q1Which of the following is the correct result of SELECT 10 % 3;?
Functions ① — Arithmetic, String Concatenation, and Date Functions
The first of three SQL function articles. Cover the + - * / % arithmetic operators, string concatenation with || and CONCAT, and date functions like current timestamp and year / month extraction — all on a CSV-loaded staff dataset.
Data used in this article — the staff table
Starting from this article, the next three articles cover functions that are built into SQL. The first one focuses on three categories: arithmetic operations, string concatenation, and date functions — all expressions you'll usually drop into the column list of a SELECT.
The dataset is the staff table (10 rows: id / name / birthday / city / salary), auto-loaded from a CSV. Across four practice exercises, you'll do money math with salary, build labels from name and city, and format dates from birthday.
Arithmetic operators — +, -, *, /, %
For calculations on numeric columns, you have the 5 arithmetic operators + - * / %. SQL lets you write expressions directly in the SELECT column list, so you can take a value out of a table and return its calculated result as a separate column. Adding AS alias gives the result column a name, which makes it easier to display or consume in downstream code.
Division / behaves differently across databases: in integer-only divisions, some databases (PostgreSQL / SQL Server) return an integer result, while others (MySQL / this course's console = SQLite) automatically return a decimal.
-- 1) Simple calculations without a table
SELECT 1 + 1; -- 2
SELECT 10 - 3; -- 7
SELECT 3 * 4; -- 12
SELECT 10.0 / 3; -- 3.333...
SELECT 10 % 3; -- 1
-- 2) Calculation using a column (annual salary after a 10% raise)
SELECT name, salary, salary * 1.1 AS next_year_salary
FROM staff;
-- 3) Compute monthly salary (annual / 12)
SELECT name, salary / 12 AS monthly_salary FROM staff;
+ - * / % have the same meaning as in most programming languages.String concatenation — `||` and `CONCAT()`
Joining multiple strings into one is called concatenation. There are two ways to write it: this course's console (SQLite), PostgreSQL, Oracle, and SQL Server use the `||` operator (two pipes), while MySQL uses the `CONCAT()` function. SQLite actually supports both || and CONCAT(), so this article leans on the more portable CONCAT().
By slipping a separator (': ' or ' / ') between values, you can collapse name and city into a single readable column like "Alice Tanaka / Tokyo." It's a workhorse for reports and CSV exports where you want "multiple columns merged into one."
-- 1) The || operator (SQLite / PostgreSQL / Oracle)
SELECT name || ' (' || city || ')' AS label FROM staff;
-- 2) The CONCAT function (MySQL / SQLite 3.40+ / PostgreSQL)
SELECT CONCAT(name, ' (', city, ')') AS label FROM staff;
-- Both return the same result
-- → Alice Tanaka (Tokyo) / Bob Suzuki (Osaka) / ...
`||` vs. `CONCAT()` — which to pick
For portability, `CONCAT()` is the safer bet. It runs on SQLite, MySQL, PostgreSQL, SQL Server (2012+), and Oracle. || is the SQL standard, but in MySQL it defaults to logical OR, so if production might be MySQL, picking CONCAT() means you don't need to rewrite anything.
NULL handling differs too: NULL || 'A' returns NULL with the || operator. CONCAT(NULL, 'A') returns an empty string in MySQL but NULL in PostgreSQL — behavior varies. When NULL might sneak into your inputs, wrap them with COALESCE(column, '') to convert NULL to an empty string before concatenating (COALESCE is covered in the third article of this series).
Date functions — current timestamp and formatting
For things like "stamp the current time on a new record" or "pull just the year out of a birthday," you use date functions. Date functions are one of the categories where function names and syntax vary the most across databases — this course's console (SQLite) and MySQL call them quite differently.
This course uses SQLite's datetime('now') / date('now') / strftime('format', value) for the exercises, with MySQL's NOW() / CURDATE() / DATE_FORMAT(value, 'format') shown side-by-side in the comparison table below. The format strings ('%Y' for year, '%m' for month, '%d' for day, etc.) are the same in both databases, so at least the format specifiers transfer directly.
-- How to write it in this course's console (SQLite)
-- 1) Current timestamp / current date
SELECT datetime('now') AS current_dt, date('now') AS current_d;
-- 2) Format specifier (year only / year-month only)
SELECT strftime('%Y', '1990-04-15') AS year_only; -- '1990'
SELECT strftime('%Y-%m', '1990-04-15') AS year_month; -- '1990-04'
-- 3) Format applied to a column
SELECT name, strftime('%Y', birthday) AS birth_year FROM staff;
-- Reference: same thing in MySQL
-- SELECT NOW(), CURDATE();
-- SELECT DATE_FORMAT(birthday, '%Y') FROM staff;
| Purpose | This course's console (SQLite) | MySQL |
|---|---|---|
| Current timestamp | datetime('now') | NOW() |
| Current date | date('now') | CURDATE() |
| Extract year only | strftime('%Y', d) | DATE_FORMAT(d, '%Y') |
| Custom format | strftime('%Y-%m-%d', d) | DATE_FORMAT(d, '%Y-%m-%d') |
Knowledge Check
Answer each question one by one.
Q2In this course's console, which is the correct way to combine name and city into a single column like Alice Tanaka (Tokyo)?
Q3In this course's console (SQLite), which is the right way to extract just the 4-digit year from a birthday column?