Mastering PostgreSQL Operators: From Arithmetic to Aliases - Day -6 of Postgres Learning Journey

Ramkumar SRamkumar S
3 min read

PostgreSQL (PSQL) is a powerful relational database management system that offers a wide range of operators to manipulate and query data effectively. In this comprehensive guide, we will delve into the world of PSQL operators, focusing on arithmetic operators, and explore the concept of aliases to enhance your querying experience. Let's dive in!

1. Introduction to PostgreSQL Operators

Operators in PostgreSQL are symbols or special keywords used to perform various operations on data. They play a crucial role in building queries to retrieve, manipulate, and analyze data stored in the database.

2. Arithmetic Operators

Arithmetic operators in PostgreSQL allow you to perform basic mathematical calculations on numerical data types such as integers and floating-point numbers.

Addition (+)

The addition operator is used to add two numeric values.

Subtraction (-)

The subtraction operator subtracts the second value from the first value.

Multiplication (*)

Multiplication operator performs multiplication of two values.

Division (/)

Division operator divides the first value by the second value.

Modulus (%)

Modulus operator returns the remainder after division.

Exponentiation (^)

Exponentiation operator raises the first value to the power of the second value.

3. Using Aliases in PostgreSQL

What are Aliases?

Aliases provide temporary names to columns, tables, or expressions in the query result. They are especially useful when dealing with complex queries or when you want to give more meaningful names to the output.

Alias for Columns

You can assign aliases to columns using the "AS" keyword.

sqlCopy codeSELECT first_name AS "First", last_name AS "Last"
FROM employees;

Alias for Tables

Table aliases make your query more readable, especially when dealing with self-joins or multiple tables.

sqlCopy codeSELECT e.first_name, d.department_name
FROM employees AS e
JOIN departments AS d ON e.department_id = d.department_id;

Combining Aliases with Operators

Aliases can be combined with operators to create calculated fields in your query result.

sqlCopy codeSELECT order_date, quantity, unit_price, quantity * unit_price AS total_price
FROM order_details;

4. Examples and Results

Let's see some examples of using arithmetic operators and aliases in PostgreSQL queries:

Example 1: Arithmetic Operators

sqlCopy codeSELECT 10 + 5 AS addition,
       15 - 7 AS subtraction,
       6 * 4 AS multiplication,
       20 / 4 AS division,
       23 % 6 AS modulus,
       2 ^ 3 AS exponentiation;

Results:

markdownCopy code addition | subtraction | multiplication | division | modulus | exponentiation
----------+-------------+----------------+----------+---------+----------------
       15 |           8 |             24 |        5 |       5 |              8

Example 2: Using Aliases

sqlCopy codeSELECT first_name || ' ' || last_name AS full_name,
       salary * 0.15 AS bonus
FROM employees;

Results:

yamlCopy code     full_name      |   bonus
---------------------+-----------
 John Doe            |  1500.00
 Jane Smith          |  1200.00
 Michael Johnson     |  1050.00

5. Bonus: Practical Use Cases

Aliases become incredibly handy in complex queries and subqueries. For instance, you can use them to simplify nested calculations or to rename aggregated columns.

sqlCopy codeSELECT product_id,
       (SELECT AVG(unit_price) FROM products) AS avg_price,
       unit_price - (SELECT AVG(unit_price) FROM products) AS price_diff
FROM order_details;

Results:

diffCopy code product_id |      avg_price      |   price_diff
------------+---------------------+-----------------
          1 | 39.7526315789473684 |  7.0773684210526
          2 | 39.7526315789473684 |  1.0273684210526

6. Conclusion

PostgreSQL operators, including arithmetic operators and aliases, are fundamental tools for crafting powerful database queries. They allow you to perform mathematical operations and provide more meaningful names to query results. By mastering these concepts, you'll be well-equipped to manipulate and extract insights from your PostgreSQL databases effectively. Start experimenting with these operators and aliases in your own queries to unlock their full potential. Happy querying!

0
Subscribe to my newsletter

Read articles from Ramkumar S directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ramkumar S
Ramkumar S