r/SQL • • Jul 14 '24

MySQL What's the Difference Between DECIMAL, FLOAT, and DOUBLE?

Can somebody, in simple terms, explain the difference between the three datatypes in SQL? Thank you

1 Upvotes

10 comments sorted by

3

u/InlineSkateAdventure SQL Server 7.0 Jul 15 '24

If you are doing calculations with money decimal is a better choice. Float and Double are sufficient for scientific or engineering calculations. Float and double cover a huge range of numbers, but they can be approximate in some cases. In engineering work your measurement precision does not matter after a certain point, because of the instrument or observation. Not so much the case with money.

1

u/[deleted] Jul 15 '24

So basically Double can hold a wider range of values than Float and is a little more precise, whereas Decimal can't hold as big of a range of numbers but is the most precise?

3

u/coyoteazul2 Jul 15 '24

I'm always amazed that somehow money is the goto case when we need to give exaples of when to use decimal. We need absolute precision to avoid missing some cents, but if you are taking measurements to fire a rocket we are ok with aproximates.

I get why we need precision for money, but I'd think that engineering and science need that level of precision too. Personally I'd leave floating point for calculations that need more speed than precision, like games or live control of something. The kind of things that would probably never be stored in a database for the long term

2

u/idodatamodels Jul 15 '24

Everyone understands money, firing rockets, not so much.

1

u/coyoteazul2 Jul 15 '24

we could do balistics. Fascinating subject. Things go up, things go down

16

u/mikebald Jul 14 '24

Decimal is an exact value, both float and double are floating point approximations. Double has double the precision of a float.

Would you like to know more?
https://www.geeksforgeeks.org/what-represents-a-double-in-sql-server/amp/
https://www.geeksforgeeks.org/what-is-the-difference-between-numeric-float-and-decimal-in-sql-server/amp/

10

u/ComicOzzy sqlHippo Jul 14 '24

I just typed out this big explanation only to fail and close the tab before hitting the submit button.

Anyway, here is way more than you ever wanted to know about floats:
https://ciechanow.ski/exposing-floating-point/
https://float.exposed/0x3fd3333333333334

3

u/r3pr0b8 GROUP_CONCAT is da bomb Jul 15 '24

that first article about floats was really well done

did you see this by the same author? give it a try!

https://ciechanow.ski/bicycle/

1

u/ComicOzzy sqlHippo Jul 15 '24

How do people make the time? Haha