Relational Operators in SQL Server
Last Updated :
29 Apr, 2024
In SQL Server, relational operators are used to compare values and establish relationships between data stored in tables. These operators allow us to perform logical comparisons to filter data based on specific conditions.
Understanding relational operators is fundamental for querying and manipulating data effectively in SQL Server. In this article, we'll explore relational operators in SQL Server by covering concepts, examples in detail.
Introduction to Relational Operators
Relational operators in SQL Server are used to compare values and determine the relationship between them. These operators evaluate conditions and return a boolean (true or false) result based on whether the specified condition is met. Common relational operators include:
- Equal to (=): This operator checks if two values are the same. Generally, it compares values for equality.
- Not equal to (<> or !=): The not equal to operator checks if two values are different. It returns true if the values are not equal.
- Greater than (>): This operator checks if the left value is greater than the right value.
- Less than (<): Similar to the greater than operator, but it checks if the left operand is less than the right operand.
- Greater than or equal to (>=): This operator checks if the left operand is greater than or equal to the right operand.
- Less than or equal to (<=): Similar to the greater than or equal to operator, but it checks if the left operand is less than or equal to the right operand.
Examples of Relational Operators in SQL Server
Let's set up an Environment:
To understand Relational Operators in SQL Server we need a table on which we will perform various operations and queries. Here we will consider a table called Persons which contains data as shown below:
CREATE TABLE Persons (
EmpID INT,
Name NVARCHAR(50),
Department NVARCHAR(50),
Salary DECIMAL(10, 2),
Age INT
);
INSERT INTO Persons(EmpID, Name, Department, Salary, Age)
VALUES
(1, 'John Doe', 'Sales', 60000.00, 35),
(2, 'Jane Smith', 'Marketing', 45000.00, 28),
(3, 'Robert Johnson', 'HR', 55000.00, 42),
(4, 'Lisa Brown', 'Sales', 70000.00, 45),
(5, 'Michael Davis', 'IT', 65000.00, 30);
PersonsID | Name | Department | Salary | Age |
---|
1 | John Doe | Sales | 60000 | 35 |
2 | Jane Smith | Marketing | 45000 | 28 |
3 | Robert Johnson | HR | 55000 | 42 |
4 | Lisa Brown | Sales | 70000 | 45 |
5 | Michael Davis | IT | 65000 | 30 |
Example 1: Equal to (=)
Select Persons whose department is 'Sales'
-- Select Persons whose department is 'Sales'
SELECT * FROM Persons
WHERE Department = 'Sales';
PersonsID | Name | Department | Salary | Age |
---|
1 | John Doe | Sales | 60000 | 35 |
4 | Lisa Brown | Sales | 70000 | 45 |
Example 2: Not equal to (<>)
Select employees whose department is not 'Sales'
-- Select Persons whose Salary is not equal to 100
SELECT * FROM Persons
WHERE Salary<> 45000;
PersonsID | Name | Department | Salary | Age |
---|
1 | John Doe | Sales | 60000 | 35 |
3 | Robert Johnson | HR | 55000 | 42 |
4 | Lisa Brown | Sales | 70000 | 45 |
5 | Michael Davis | IT | 65000 | 30 |
Example 3: Greater than (>)
Select Persons whose Age is greater than 35
-- Select Person with a Age greater than 35
SELECT * FROM Persons
WHERE Age> 35;
PersonsID | Name | Department | Salary | Age |
---|
3 | Robert Johnson | HR | 55000 | 42 |
4 | Lisa Brown | Sales | 70000 | 45 |
Example 4: Less than (<)
Select Persons whose age is less than 35
-- Select Persons whose age is less than 30
SELECT * FROM Persons
WHERE Age < 35;
PersonsID | Name | Department | Salary | Age |
---|
2 | Jane Smith | Marketing | 45000 | 28 |
5 | Michael Davis | IT | 65000 | 30 |
Example 5: Greater than or equal to (>=)
Select Persons whose salary is greater than or equal to 60000
-- Select Persons whose salary is greater than or equal to 60000
SELECT * FROM Persons
WHERE salary >= 60000;
PersonsID | Name | Department | Salary | Age |
---|
1 | John Doe | Sales | 60000 | 35 |
4 | Lisa Brown | Sales | 70000 | 45 |
5 | Michael Davis | IT | 65000 | 30 |
Example 6: Less than or equal to (<=)
Select Persons whose Salary is less than or equal to 60000
-- Select products with a Salary less than or equal to 60000
SELECT * FROM Persons
WHERE Salary <= 60000;
PersonsID | Name | Department | Salary | Age |
---|
1 | John Doe | Sales | 60000 | 35 |
2 | Jane Smith | Marketing | 45000 | 28 |
3 | Robert Johnson | HR | 55000 | 42 |
Example 7: Combining Relational Operators with Logical Operators
Relational operators can also be combined with logical operators (AND, OR, NOT) to create complex conditions in SQL queries:
-- Select products with a price between 50 and 100
SELECT * FROM Persons
WHERE PersonsID >= 2 AND PersonsID <= 4;
PersonsID | Name | Department | Salary | Age |
---|
2 | Jane Smith | Marketing | 45000 | 28 |
3 | Robert Johnson | HR | 55000 | 42 |
4 | Lisa Brown | Sales | 70000 | 45 |
Conclusion
Relational operators play a important role in SQL Server for comparing values and filtering data based on specific conditions. By understanding the relational operators such as equal to, not equal to, greater than, less than, greater than or equal to, and less than or equal to, users can perform logical comparisons and establish relationships between data in tables. In this article, we explored relational operators in SQL Server and provided examples of their usage in SQL queries, and demonstrated the output of these queries.
Similar Reads
SQL Server INTERSECT Operator
In SQL Server, the INTERSECT operator is a kind of set operator that is used to combine the results of two SELECT statements and return rows which is common between them. In this article, We will explore the syntax, key concepts, and practical examples of using the INTERSECT operator. Whether you ar
5 min read
How to use Relational Operators in MySQL
In the database management area, relational operators are the building blocks for data filtering and querying. MySQL, one of the most popular relational database management systems that are widely used across the world, features a wide range of relational operators that are highly efficient in handl
6 min read
SQL Server Interview Questions
Data is growing every day, and it plays a big part in making important decisions. To manage this data, we need reliable databases, and SQL Server is one of the most popular options out there. Learning SQL Server can lead to exciting careers as an SQL Specialist or SQL Developer. If youâre preparing
15+ min read
Set Difference Operator in Relational Algebra
Relational Algebra is used to play with the data stored in relational databases. Relational Algebra has many operations to play with tables. One of the fundamental operations is set difference. This article will discuss Set Difference, its condition, and its examples. Key Terms Used in Set Differenc
4 min read
SQL Server SELECT INTO Statement
SQL Server is a relational database management system. SQL Server offers robust security features to protect data integrity and confidentiality. It includes authentication, authorization, encryption, and various mechanisms to secure the database environment. It is designed to scale from small applic
6 min read
SQL Server ALIASES
Aliases in SQL Server are the temporary names given to tables or columns to make it easy to read and maintain the data. Aliases help you to provide different names to columns and tables temporarily so that users can easily understand the data of the table and it does not change any data of the table
3 min read
Select Statement in MS SQL Server
The SELECT statement in SQL Server is a foundational SQL command used for querying and retrieving data from one or more tables within a database. This command allows users to specify which columns and rows to retrieve and apply filters to focus on specific data and perform various operations to mani
4 min read
CREATE TABLE in SQL Server
SQL Server provides a variety of data management tools such as querying, indexing, and transaction processing. It supports multiple programming languages and platforms, making it a versatile RDBMS for various applications. With its robust features and reliability, SQL Server is a popular choice for
4 min read
SQL Server FULL OUTER JOIN
Joins in SQL are used to retrieve data from multiple tables based on a related column (or common column) between them. In this article, we will learn how to use FULL OUTER JOIN, which returns all rows from both tables being joined. It combines the results of both LEFT OUTER JOIN and RIGHT OUTER JOIN
6 min read
SQL Server Architecture
Microsoft SQL Server is a widely used relational database management system (RDBMS) that organizations around the world rely on for managing and processing their data. It provides a scalable and reliable platform for managing large volumes of data, supporting a wide range of applications from small-
5 min read