Hi everyone, In this article, we will examine the conditional clause “WHERE” and logical operators.
In the SQL language, if you want to show the output of the SQL command according to a certain condition, the “WHERE” command will be useful.
- Strings and date values must be enclosed in single quotes.
- The clauses must follow the table name in accordance with the hierarchy.
Let’s try to understand better with the example,
SELECT * FROM HR.employees WHERE salary >4000;
We have called a query to return values greater than 4000 for the “SALARY” column.
If the condition statement will be more than one, each condition statement must be accompanied by “AND“, “OR” commands.
SELECT * FROM HR.employees WHERE salary >4000 AND FIRST_NAME ='David';
Let’s continue with a different example,
SELECT * FROM HR.employees WHERE SALARY>=10000 ORDER BY SALARY;
The “SALARY” value is 10000 and we brought the larger ones.
In the next example let’s specify a range, Important keyword “BETWEEN”.
SELECT * FROM HR.employees WHERE SALARY BETWEEN 10000 AND 14000;
One of the important issues is the “like” operator. The “like” operator is used to find strings that match a given template.
- It can contain a conditional sentence and / or a number.
- “%” contains zero or more characters
- The special character “_” represents exactly one character.
We’ve questioned names starting with “J”.
SELECT * FROM HR.employees WHERE FIRST_NAME like 'J%';
Let’s question the names with “ald” in it.
SELECT * FROM HR.employees WHERE FIRST_NAME like '%ald';
Logical Operators
If we want to make comparisons in SQL Language according to multiple criteria, we need to do this using logical operators.
- AND
- OR
- NOT
The AND operator returns true if both conditions are true.
Get records with the name “William” and the surname “Gietz”,
SELECT * FROM HR.employees WHERE FIRST_NAME='William' AND LAST_NAME='Gietz';
The OR operator, whether the query result can return true depends on whether at least one of the given conditions is true.
Let’s see the records with the name “Susan” or Salary 17000.
SELECT * FROM HR.employees WHERE FIRST_NAME='Susan' OR SALARY=17000;
The NOT operator allows us to retrieve values other than those specified in the query result list,
SELECT * FROM HR.employees WHERE SALARY NOT IN (3000,5000,10000);
As can be seen from the result, we brought 3000, 5000 and 10000 non-salaries.
See you in next article..