Operators in SQL
These SQL operators are considered as a set of reserved words used in SQL Where clause. They specify the condition and also serve as a conjunction for multiple conditions in a statement. In this tutorial, we will discuss on:
Watch this Operators in SQL video
How to Impose Conditions using SQL Operators?
As in the earlier Where clause section, we saw how one condition is passed. But when there are multiple conditions applied to a table to filter the data, the operators are used. There are two conditions: first, the age should be greater than 60 and, second, the occupation must be doctor. Only when these conditions are met, the AND operator displays values.
There are three types of operators:
- AND Operator in SQL
- OR Operator in SQL
- NOT Operator in SQL
lets look each of them in detail
AND operator in SQL and its syntax:
The AND operator displays only those records where all conditions are evaluated to true. For example, if you want to find out all the doctors aged greater than 60, the syntax would be as follows.
Syntax
SELECT column1, column2, …, columnN
FROM tablename
WHERE [condition1], … AND [conditionN];
where SELECT, FROM, WHERE, and AND are the keywords; column1 to columnN are a set of columns; tablename is the name of the table, and condition1 to conditionN are a set of conditions followed by a semicolon.
- Let’s display all employees whose age is below 30 and who belong to the operations department from the employee table
Select e_name, e_age, e_salary From employee where e_age<30 and e_dept=’operation’;
- After writing the query, click on the execute button to check for errors
- Once the query is executed, the table appears
The AND operator imposes a condition or multiple conditions in which the conditions must be true, i.e., it must satisfy all the conditions.
OR operator in SQL and its syntax
The OR operator displays records for any condition separated by OR which is evaluated to true. For example, if we have to filter out a software engineer or a doctor, then we use the OR operator.
Syntax
SELECT column1, column2, … columnN
FROM tablename
WHERE [condition1], …, OR [conditionN];
where SELECT, FROM, WHERE, and OR are the keywords; column1 to columnN are a set of columns; tablename is the name of the table, and condition1 to conditionN are a set of conditions followed by a semicolon.
- Let’s display all employees who are from either the sales department or the operations department from the employee table
Select * From employee where_dept=’sales’ or e_dept=’operation’;
- After writing the query, click on the execute button to check for errors
- Once the query is executed, the table appears
The OR operator imposes a condition or multiple conditions in which either of the conditions must be true.
Get 100% Hike!
Master Most in Demand Skills Now!
NOT operator in SQL and its syntax
The NOT operator displays a record if the condition is not true. For example, if we have to extract all records where the occupation of a person is not a software engineer, then the Not operator is used.
Syntax
SELECT column1, column2, … columnN
FROM tablename
WHERE NOT [condition1];
where SELECT, FROM, WHERE, and NOT are the keywords; column1 to columnN are a set of columns; tablename is the name of the table, and there is a condition followed by a semicolon.
- Let’s display all employees whose gender is not equal to female from the employee table
Select * From employee where not e_gender=’female’;
- After writing the query, click on the execute button to check for errors
- Once the query is executed, the table appears
Well, this brings us to the end of this SQL Operates tutorial section. Now, you’re ready to get started with creating a table and conducting basic operations of filtering and imposing conditions! While there is a lot more you can do with the data using SQL queries which we will see in the following tutorial sections.