DOUBLE

DOUBLE is a data type in SQL, specifically used to store floating-point numbers with large precision. It allows for storage of extremely large or tiny values with decimals. DOUBLE refers to a double-precision floating-point number. Any values stored as DOUBLE are approximate, not exact values, but are accurate up to 15 digits. This data type can be used to hold numbers that are larger or more precise than those held by the FLOAT data type.

Example

CREATE TABLE Employee (
ID int,
Salary DOUBLE(8,2)
);
INSERT INTO Employee(ID, Salary)
VALUES (1, 3500.75), (2, 4500.50);
SELECT * FROM Employee;

Output

+------+---------+
| ID | Salary |
+------+---------+
| 1 | 3500.75 |
| 2 | 4500.50 |
+------+---------+

Explanation

In this example, a table named Employee is created with two columns: ID (an integer) and Salary (a DOUBLE). A DOUBLE in MySQL is a floating-point number that allows for decimal points. The notation DOUBLE(8,2) specifies that the Salary numbers have a total of 8 digits, of which 2 are after the decimal point. The INSERT INTO statement is used to add two rows of data into the Employee table. Running SELECT * FROM Employee then retrieves all records from the table.

Example

DECLARE @Value DOUBLE;
SET @Value = 10.12345;
SELECT @Value AS 'Double Value';

Output

Double Value
-----------
10.12345

Explanation

The provided SQL Server script declares a variable @Value of DOUBLE datatype and assigns it the value 10.12345. The SELECT statement is used to return this value.

Example

CREATE TABLE Employee (
ID NUMBER,
Salary DOUBLE PRECISION
);
INSERT INTO Employee (ID, Salary) VALUES (1, 75000.50), (2, 60000.80);
SELECT * FROM Employee;

Output

ID | Salary
1 | 75000.50
2 | 60000.80

Explanation

The DOUBLE PRECISION data type is a floating point numeral data type that holds 15 digits of precision. It is capable of storing numbers that are substantially larger or smaller than those that can be stored in an NUMBER data type. When retrieving data from Employee table, it correctly displays the decimal precision in salaries.

Example

CREATE TABLE employees (
id INTEGER PRIMARY KEY,
first_name TEXT,
last_name TEXT,
salary DOUBLE
);
INSERT INTO employees (first_name, last_name, salary)
VALUES ('John', 'Doe', 50000.75);

Output

To verify the data, use the following SELECT statement.

SELECT * FROM employees;

Output:

id: 1
first_name: John
last_name: Doe
salary: 50000.75

Explanation

In the example above, a new SQLite table called ‘employees’ is created with four columns: ‘id’ (an integer that serves as a primary key), ‘first_name’ (a text string), ‘last_name’ (a text string), and ‘salary’ (a double precision number).

After the table is created, data is inserted into it. The ‘salary’ field is filled with a double value, which is a number that can include fractional parts, as shown by the value 50000.75.

The SELECT statement is then used to retrieve the data from the table, confirming that the ‘salary’ value was stored accurately.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.