SQL 2
SQL 2
SQL Data Type is an attribute that specifies the type of data of any object. Each
column, variable and expression has a related data type in SQL. You can use these
data types while creating your tables. You can choose a data type for a table column
based on your requirement.
SQL Server offers six categories of data types for your use which are listed below −
tinyint 0 255
bit 0 1
Note − Here, datetime has 3.33 milliseconds accuracy where as smalldatetime has 1
minute accuracy.
1 char
Maximum length of 8,000 characters.( Fixed length non-Unicode characters)
varchar
2
Maximum of 8,000 characters.(Variable-length non-Unicode data).
varchar(max)
3 Maximum length of 2E + 31 characters, Variable-length non-Unicode data (SQL
Server 2005 only).
text
4 Variable-length non-Unicode data with a maximum length of 2,147,483,647
characters.
1 nchar
Maximum length of 4,000 characters.( Fixed length Unicode)
nvarchar
2
Maximum length of 4,000 characters.(Variable length Unicode)
nvarchar(max)
3 Maximum length of 2E + 31 characters (SQL Server 2005 only).( Variable length
Unicode)
ntext
4
Maximum length of 1,073,741,823 characters. ( Variable length Unicode )
1 binary
Maximum length of 8,000 bytes(Fixed-length binary data )
varbinary
2
Maximum length of 8,000 bytes.(Variable length binary data)
varbinary(max)
3 Maximum length of 2E + 31 bytes (SQL Server 2005 only). ( Variable length
Binary data)
image
4
Maximum length of 2,147,483,647 bytes. ( Variable length Binary Data)
sql_variant
1
Stores values of various SQL Server-supported data types, except text, ntext,
and timestamp.
timestamp
2 Stores a database-wide unique number that gets updated every time a row gets
updated
uniqueidentifier
3
Stores a globally unique identifier (GUID)
xml
4 Stores XML data. You can store xml instances in a column or a variable (SQL
Server 2005 only).
5 cursor
Reference to a cursor object
table
6
Stores a result set for later processing
SQL - Operators
What is an Operator in SQL?
An operator is a reserved word or a character used primarily in an SQL statement's
WHERE clause to perform operation(s), such as comparisons and arithmetic
operations. These Operators are used to specify conditions in an SQL statement and
to serve as conjunctions for multiple conditions in a statement.
Arithmetic operators
Comparison operators
Logical operators
Operators used to negate conditions
Divides left hand operand by right hand operand and returns b%a
% (Modulus) remainder. will give
0
Checks if the values of two operands are equal or not, if yes then (a = b) is
=
condition becomes true. not true.
Checks if the values of two operands are equal or not, if values are (a != b)
!=
not equal then condition becomes true. is true.
Checks if the values of two operands are equal or not, if values are (a <> b)
<>
not equal then condition becomes true. is true.
Checks if the value of left operand is greater than the value of right (a > b) is
>
operand, if yes then condition becomes true. not true.
Checks if the value of left operand is less than the value of right (a < b) is
<
operand, if yes then condition becomes true. true.
Checks if the value of left operand is greater than or equal to the (a >= b)
>= value of right operand, if yes then condition becomes true. is not
true.
<= Checks if the value of left operand is less than or equal to the value
(a <= b)
of right operand, if yes then condition becomes true. is true.
Checks if the value of left operand is not less than the value of right (a !< b)
!<
operand, if yes then condition becomes true. is false.
Checks if the value of left operand is not greater than the value of (a !> b)
!>
right operand, if yes then condition becomes true. is true.
1 ALL
The ALL operator is used to compare a value to all values in another value set.
AND
2 The AND operator allows the existence of multiple conditions in an SQL
statement's WHERE clause.
ANY
3 The ANY operator is used to compare a value to any applicable value in the list
as per the condition.
BETWEEN
4 The BETWEEN operator is used to search for values that are within a set of
values, given the minimum value and the maximum value.
EXISTS
5 The EXISTS operator is used to search for the presence of a row in a specified
table that meets a certain criterion.
IN
6 The IN operator is used to compare a value to a list of literal values that have
been specified.
LIKE
7 The LIKE operator is used to compare a value to similar values using wildcard
operators.
NOT
8 The NOT operator reverses the meaning of the logical operator with which it is
used. Eg: NOT EXISTS, NOT BETWEEN, NOT IN, etc. This is a negate
operator.
OR
9 The OR operator is used to combine multiple conditions in an SQL statement's
WHERE clause.
IS NULL
10
The NULL operator is used to compare a value with a NULL value.
UNIQUE
11 The UNIQUE operator searches every row of a specified table for uniqueness (no
duplicates).
SQL - Expressions
An expression is a combination of one or more values, operators and SQL functions
that evaluate to a value. These SQL EXPRESSIONs are like formulae and they are
written in query language. You can also use them to query the database for a specific
set of data.
Syntax
Consider the basic syntax of the SELECT statement as follows −
SELECT column1, column2, columnN
FROM table_name
WHERE [CONDITION|EXPRESSION];
There are different types of SQL expressions, which are mentioned below −
Boolean
Numeric
Date
Let us now discuss each of these in detail.
Boolean Expressions
SQL Boolean Expressions fetch the data based on matching a single value. Following
is the syntax −
SELECT column1, column2, columnN
FROM table_name
WHERE SINGLE VALUE MATCHING EXPRESSION;
Consider the CUSTOMERS table having the following records −
SQL> SELECT * FROM CUSTOMERS;
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
7 rows in set (0.00 sec)
The following table is a simple example showing the usage of various SQL Boolean
Expressions −
SQL> SELECT * FROM CUSTOMERS WHERE SALARY = 10000;
+----+-------+-----+---------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+-------+-----+---------+----------+
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+-------+-----+---------+----------+
1 row in set (0.00 sec)
Numeric Expression
These expressions are used to perform any mathematical operation in any query.
Following is the syntax −
SELECT numerical_expression as OPERATION_NAME
[FROM table_name
WHERE CONDITION] ;
Here, the numerical_expression is used for a mathematical expression or any formula.
Following is a simple example showing the usage of SQL Numeric Expressions −
SQL> SELECT (15 + 6) AS ADDITION
+----------+
| ADDITION |
+----------+
| 21 |
+----------+
1 row in set (0.00 sec)
There are several built-in functions like avg(), sum(), count(), etc., to perform what is
known as the aggregate data calculations against a table or a specific table column.
SQL> SELECT COUNT(*) AS "RECORDS" FROM CUSTOMERS;
+---------+
| RECORDS |
+---------+
| 7 |
+---------+
1 row in set (0.00 sec)
Date Expressions
Date Expressions return current system date and time values −
SQL> SELECT CURRENT_TIMESTAMP;
+---------------------+
| Current_Timestamp |
+---------------------+
| 2009-11-12 06:40:23 |
+---------------------+
1 row in set (0.00 sec)
Another date expression is as shown below −
SQL> SELECT GETDATE();;
+-------------------------+
| GETDATE |
+-------------------------+
| 2009-10-22 12:07:18.140 |
+-------------------------+
1 row in set (0.00 sec)