Careeroza
Operators
SQL Arithmetic Operators
| Operator | Description | Example |
|---|---|---|
+ | Add | Price + 1 |
- | Subtract | Price - 1 |
* | Multiply | Price * Stock |
/ | Divide | Amount / 2 |
% | Modulo (remainder) | OrderID % 2 |
Example
Find orders with an even OrderID and show the amount including 18% tax:
SELECT OrderID, Amount, ROUND(Amount * 1.18, 2) AS WithTax
FROM Orders
WHERE OrderID % 2 = 0;Result: 4 rows
| OrderID | Amount | WithTax |
|---|---|---|
| 102 | 89.9 | 106.08 |
| 104 | 56.25 | 66.38 |
| 106 | 320.75 | 378.48 |
| 108 | 198.4 | 234.11 |
Integer Division
When both operands are integers, many databases (SQL Server, PostgreSQL, SQLite) do integer division: 7 / 2 is 3. Multiply by 1.0 to get a decimal result. MySQL and Oracle return 3.5.
Example
Compare integer division with decimal division:
SELECT 7 / 2 AS IntegerDivision, 7 * 1.0 / 2 AS DecimalDivision;Result: 1 row
| IntegerDivision | DecimalDivision |
|---|---|
| 3 | 3.5 |
Comparison and Logical Operators
| Type | Operators |
|---|---|
| Comparison | = <> != > < >= <= |
| Logical | AND OR NOT |
| Special | BETWEEN IN LIKE IS NULL EXISTS ANY ALL |
The following pages cover each of these in detail.