SQL Server Tutorial
Range operators are used to retrieve the data using the condition specified in ranges.
SQL Server supports range operators such as BETWEEN and NOT BETWEEN.
Range operators can be used in the WHERE clause.
SELECT column_name1[, column_name2, ....] FROM table_name WHERE expression1 [BETWEEN | NOT BETWEEN ] expression2 AND expression3here expression can be constant, variable, return value of function, column value or any combination of constant, variable, function or column value.
Let us consider the example 'Employee' database table.
BETWEEN operator is used to specify the condition based on ranges in WHERE clasue.
To get rows based on salary range from 6000 to 8000 in a Employee table.
SELECT Name, DeptId, City, Email, Salary FROM Employee WHERE Salary BETWEEN 6000 AND 8000Output:
To get rows based on DeptId in the range from 2 to 6.
SELECT Name, DeptId, City, Email, Salary FROM Employee WHERE DeptId BETWEEN 2 AND 6Output:
NOT BETWEEN operator is used to specify the negative condition based on ranges in WHERE clasue.
To get rows based on salary other than in the range from 6000 to 8000 in a Employee table.
SELECT Name, DeptId, City, Email, Salary FROM Employee WHERE Salary NOT BETWEEN 6000 AND 8000Output:
To get rows based on DeptId other than in the range from 2 to 6.
SELECT Name, DeptId, City, Email, Salary FROM Employee WHERE DeptId NOT BETWEEN 2 AND 6Output:
SQL Server Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page