SQL Data Types and Operators
In this lesson, we will learn about the different data types supported by SQL.
SQL data types
A SQL data type is an attribute that specifies the type of data of any object. You can specify the data type of each column in the table based on your requirements.
Some of them are listed below:
Exact Numeric Data Types
Data Type | Ranges From | To |
---|---|---|
int | -2,147,483,648 | 2,147,483,647 |
bigint | -9,223,372,036,854,775,808 | 9,223,372,036,854,775,807 |
smallint | -32,768 | 32,767 |
tinyint | 0 | 255 |
bit | 0 | 1 |
decimal | -10^38 +1 | 10^38 -1 |
numeric | -10^38 +1 | 10^38 -1 |
Approximate Numeric Data Types
Data Type | Ranges From | To |
---|---|---|
float | -1.79E + 30 | 1.79E + 308 |
real | -3.40E + 38 | 3.40E + 38 |
Date and Time Data Types
Data Type | Ranges From | To |
---|---|---|
datetime | Jan 1, 1753 | Dec 31, 9999 |
smalldatetime | Jan 1, 1900 | Jun 6, 2079 |
date | Stores a date like June 30, 1991 | - |
time | Stores a time like 12:30 P.M. | |
- |
Character Strings Data Types
DATA TYPE | Description |
---|---|
char | Maximum length of 8,000 characters.( Fixed length non-Unicode characters) |
varchar | Maximum of 8,000 characters. (Variable-length non-Unicode data). |
varchar(max) | Maximum length of 2E + 31 characters, Variable-length non-Unicode data. |
text | Variable-length non-Unicode data with a maximum length of 2,147,483,647 characters. |
What is an operator in SQL?
An operator is a reserved word or character used primarily in a SQL statement’s WHERE clause to perform operation(s), such as comparisons and arithmetic operations. These operators are used to specify conditions in a SQL statement and to serve as conjunctions for multiple conditions in a statement.
SQL arithmetic operators
Assume variable ‘a’ holds 10 and variable ‘b’ holds 20.
Operator | Description | Example |
---|---|---|
+ (Addition) | Adds values on either side of the operator | a + b will give 30 |
- (Subtraction) | Subtracts right-hand operand from left-hand operand | a - b will give -10 |
* (Multiplication) | Multiplies values on either side of the operator | a * b will give 200 |
/ (Division) | Divides left-hand operand by right-hand operand | b / a will give 2 |
% (Modulus) | Divides left-hand operand by right-hand operand and returns remainder | b % a will give 0 |
SQL comparison operators
Assume ‘variable a’ holds 10 and ‘variable b’ holds 20.
Operator | Description | Example |
---|---|---|
= | Checks if the values of two operands are equal or not, if yes then condition becomes true | (a = b) is not true |
!= | Checks if the values of two operands are equal or not, if values are not equal then condition becomes true | (a != b) is true |
<> | Checks if the values of two operands are equal or not, if values are not equal then condition becomes true | (a <> b) is true |
> | Checks if the value of the left operand is greater than the value of right operand, if yes then condition becomes true | (a > b) is not true |
< | Checks if the value of the left operand is less than the value of right operand, if yes then condition becomes true | (a < b) is true |
>= | Checks if the value of the left operand is greater than or equal to the value of right operand, if yes then condition becomes true | (a >= b) is not true |
<= | Checks if the value of the left operand is less than or equal to the value of right operand, if yes then condition becomes true | (a <= b) is true |
You don’t have to worry about these terms too much as we will be using them in the lessons to come, which will help you to understand how they are used. In the next lesson, we will discuss SQL constraints.
Get hands-on with 1400+ tech skills courses.