SQL Operators
SQL Operators
Logical
Operators
AND Operator
The AND operator is used to combine two or more conditions in a
SQL query. It returns true only if all the conditions specified are true.
For instance, in a query where we want to select employees who are
both in the 'Sales' department and have a salary greater than 50000,
the statement would look like this: SELECT * FROM Employees
WHERE Department = 'Sales' AND Salary > 50000.
OR Operator
The OR operator is employed to
combine conditions in a SQL query
where at least one of the conditions
must be true. It expands the results
returned by the query. For example, if
you want to select employees who are
either from the 'Sales' department or
who have a salary greater than
50000, you could structure your SQL
query as follows: SELECT * FROM
Employees WHERE Department =
'Sales' OR Salary > 50000.
NOT Operator
The NOT operator is used to reverse the logical state of its operand.
When applied, it returns true if the condition is false and vice versa.
For example, in a query to select employees who are not in the
'Sales' department, the syntax would be: SELECT * FROM Employees
WHERE NOT Department = 'Sales'. This operator helps filter out
specific data sets based on negative conditions.
02
Comparison
Operators
Equal to (=)
The Equal to operator (=) compares two values and returns true if
they are equal. It is commonly used in WHERE clauses of SQL
statements to filter records. For instance, to find employees with a
salary of 60000, you would write: SELECT * FROM Employees WHERE
Salary = 60000. This operator is fundamental in ensuring data
matches specific criteria.
Greater than (>)
The Greater than operator (>) compares two values and returns true
if the left operand is greater than the right operand. It is often used
to filter results in queries. For example, to select employees whose
salaries exceed 70000, the SQL query would be: SELECT * FROM
Employees WHERE Salary > 70000. This operator allows for
retrieving data sets based on thresholds.
Less than (<)
The Less than operator (<) checks
whether the left operand is less than
the right operand and returns true if
this condition is met. This operator is
particularly useful in conditions to
filter out data below a certain value.
For instance, to find employees with a
salary less than 40000, your SQL
statement would be: SELECT * FROM
Employees WHERE Salary < 40000.
Conclusions
In conclusion, SQL operators are vital for data manipulation and
querying in databases. Logical operators like AND, OR, and NOT
enable complex conditions, while comparison operators such as
Equal to, Greater than, and Less than allow precise filtering of data.
Understanding these operators enhances the ability to retrieve and
manipulate data effectively.
Thank you!