PostgreSQL DEGREES() Function


PostgreSQL DEGREES() Function

The PostgreSQL DEGREES() function is used to convert an angle measured in radians to an angle measured in degrees. This function is essential for working with trigonometric data where conversion from radians to degrees is required.


Syntax

DEGREES(radians)

The DEGREES() function has the following component:

  • radians: The angle in radians to be converted to degrees.

Example PostgreSQL DEGREES() Queries

Let's look at some examples of PostgreSQL DEGREES() function queries:

1. Basic DEGREES() Example

SELECT DEGREES(3.141592653589793) AS degrees;

This query converts the angle π radians to degrees, which is 180 degrees.

2. DEGREES() with Column Values

SELECT radians, DEGREES(radians) AS degrees
FROM angles;

This query retrieves the radians and their corresponding degrees from the angles table.

3. DEGREES() with Negative Values

SELECT radians, DEGREES(radians) AS degrees
FROM angles
WHERE radians < 0;

This query retrieves the radians and their corresponding degrees from the angles table where the radians are negative.


Full Example

Let's go through a complete example that includes creating a table, inserting data, and using the DEGREES() function to convert angles from radians to degrees.

Step 1: Creating a Table

This step involves creating a new table named angles to store angle data.

CREATE TABLE angles (
    id SERIAL PRIMARY KEY,
    radians NUMERIC
);

In this example, we create a table named angles with columns for id and radians.

Step 2: Inserting Data into the Table

This step involves inserting some sample data into the angles table.

INSERT INTO angles (radians)
VALUES (0),
       (1.5708),
       (3.141592653589793),
       (-1.5708);

Here, we insert data into the angles table.

Step 3: Using the DEGREES() Function

This step involves using the DEGREES() function to convert the angles from radians to degrees in the angles table.

Basic DEGREES()

SELECT radians, DEGREES(radians) AS degrees
FROM angles;

This query converts each value in the 'radians' column of the 'angles' table from radians to degrees.

DEGREES() with Negative Values

SELECT radians, DEGREES(radians) AS degrees
FROM angles
WHERE radians < 0;

This query converts each negative value in the 'radians' column of the 'angles' table from radians to degrees.

These queries demonstrate how to use the DEGREES() function to convert the angles from radians to degrees in the angles table, including basic usage and handling negative values.

Conclusion

The PostgreSQL DEGREES() function is a fundamental tool for converting angles from radians to degrees. Understanding how to use the DEGREES() function and its syntax is essential for effective data retrieval and manipulation in PostgreSQL databases.