Posts

Showing posts from February, 2025

DBA Magic -- Doing Math with SQL

Image
SQL (Structured Query Language) is not just for querying data; it also has robust mathematical capabilities that allow you to perform various calculations directly within your queries. Here’s an overview of how to perform mathematical operations in SQL, along with examples. 1. Basic Arithmetic Operations SQL supports the four basic arithmetic operations: addition, subtraction, multiplication, and division. You can use these operators directly in your SQL queries. Addition (+): SELECT price, quantity, (price * quantity) AS total_cost FROM products; Subtraction (-): SELECT employee_id, salary, (salary - deductions) AS net_salary FROM employees; Multiplication (*): SELECT item, price, quantity, (price * quantity) AS total_price FROM inventory; Division (/): SELECT sales, expenses, (sales / expenses) AS profit_margin FROM financials; 2. Aggregate Functions SQL provides built-in aggregate function...