SQL Basics · AS aliases · calculated columns
9. Monthly pay
Concept: AS aliases · calculated columnsshow
SELECT can do math: salary / 12 or price * 1.2 makes a new column, worked out for every row. AS gives any column a name of your choice (salary AS annual_salary), and that name becomes the column header. One catch: whole number divided by whole number drops the decimals in Postgres (7 / 2 is 3), so write 12.0 to keep them.
Example (different data):
SELECT title AS product, price AS price_before_tax, ROUND(price * 1.2, 2) AS price_with_tax FROM products;
Your task
HR is rolling out a payslip preview and wants each person's yearly salary shown next to what they get per month.
Return employee (the name), annual_salary (the salary column) and monthly_salary (salary divided by 12, rounded to 2 decimal places) for every employee. Sort by annual_salary, highest first, then by employee A to Z.
Starting Postgres in your browser…
Write a query and press Run Query (Ctrl/⌘ + Enter).