SQL chapters

Chapter 5 of 17

Data Types

Try this in our SQLite Compiler →

Built-in types

Every value in SQL has a type that tells the computer what kind of data it is and what you can do with it. The core built-in types you will use every day are numbers, text (strings), booleans (true / false) and a "nothing here" value used for missing data.

CREATE TABLE users (
  id INT PRIMARY KEY,
  name VARCHAR(100),
  age INT,
  joined DATE,
  active BOOLEAN
);

Why types matter

Types stop you from making nonsense operations like multiplying a name by a date. In statically typed languages the compiler checks types before the program runs; in dynamically typed languages the check happens while the program runs. Either way you should always know roughly which type each variable holds.

Converting between types

Real programs constantly convert between types: text from a form becomes a number, a number becomes formatted text on a screen. SQL provides built-in conversion helpers — use them explicitly rather than relying on implicit conversions which can produce surprising results.

Try it yourself

SQLite Compiler
Output
Code runs on the Play with Coding execution engine — your code is saved locally for next time.