SQL Operators – Logical Operators in SQL

These SQL operators are considered to be a collection of reserved words used in clause Where in SQL. They define the condition, and function in a statement as a conjunction for multiple conditions. We’ll discuss in this tutorial on:

  • Why to Impose Definitions for SQL Operators?
  • AND Operator and its Syntax in SQL
  • Operator SQL OR and its syntax
  • NOT SQL operator, and its syntax

How to Impose Conditions using SQL Operators?

We’ve seen how one condition is passed, as in the earlier Where clause section. But the operators are used when there are several conditions that apply to a table to filter the data. There are two conditions: first, the age should reach 60 years and, second, the profession should be a doctor. The AND operator only shows values when those conditions are met.

There are three different 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 shows only those records where it tests all conditions to real. For instance, if you want to find out all the doctors over 60 years old, the syntax will be the following.

Syntax:

SELECT column1, column2, …, columnN
FROM tablename
WHERE [condition1], … AND [conditionN];

Where Pick, FROM, WHERE, and AND are keywords; column1 to columnN is a set of columns; tablename is the table name, and condition1 to conditionN is a set of conditions followed by a semicolon.

Let’s show from the employee table all workers below 30 years of age who belong to the department of operations

Select e_name, e_age, e_salary From employee where e_age<30 and e_dept=’operation’;

AND Operator in SQL

After the query is written, click the execute button to check for errors

The table will appear after the query has been executed

The AND operator imposes a condition or several conditions in which the conditions must be valid, i.e. all the conditions must be fulfilled.

OR operator in SQL and its syntax

For any condition separated by OR, the OR operator shows records which are evaluated to true. If we have to screen out a software engineer or a doctor, for example, then we use the OR operator.

Syntax:

SELECT column1, column2, … columnN
FROM tablename
WHERE [condition1], …, OR [conditionN];

Where Pick, FROM, WHERE, and OR are keywords; column1 to columnN is a set of columns; tablename is the table name, and condition1 to conditionN is a set of conditions followed by a semicolon.

Let’s see from the employee table all workers who are either from the sales department or from the operations department

Select * From employee where_dept=’sales’ or e_dept=’operation’;

OR Operator in SQL

After the query is written, click the execute button to check for errors

The table will appear after the query has been executed

The OR operator imposes a condition or several conditions that must be applicable to any of those conditions.

NOT operator in SQL and its syntax

If the condition is not valid the NOT operator shows a record. For instance, if we need to remove all records where a person’s occupation is not a software engineer, then the Not Operator is used.

Syntax:

SELECT column1, column2, … columnN
FROM tablename
WHERE NOT [condition1];

Where the keywords are Pick, FROM, WHERE, and NOT; column1 to columnN is a series of columns; tablename is the table name, and a condition followed by a semicolon.

Let’s demonstrate from the employee table all workers whose gender isn’t equal to female

Select * From employee where not e_gender=’female’;

NOT Operator in SQL

After the query is written, click the execute button to check for errors

The table will appear after the query has been executed

Ok, that takes us to the end of this tutorial segment on SQL Operates.

Now, you ‘re ready to start building a table and doing simple filtering and enforcing requirements operations! Though, you can do much more with the data using SQL queries that we’ll see in the following tutorial pages.

Leave a Reply