WHERE Clause in SELECT Statement.
SELECT Emp_name, Salary FROM Employee Where Salary=40,000;
SELECT Statement with Logical Condition Operator.
This statement can also written as
The where clause in SELECT statement is used to filter out records in table according to user constrains. It also used merge two or more by join statement.
SELECT with WHERE
SELECT Column-Names FROM table_name WHERE (Condition);
condition may be any
- Relational Condition =, <, >, !=
- Logical Condition AND, OR and NOT
- Special Operator IN, BETWEEN, ANY, ALL, SOME, EXISTS.
Example table and Statement.
Employee
id | Emp_name | Age | Salary |
1 | Jebas | 35 | 40,000 |
2 | Thomas | 30 | 80,000 |
3 | Mark | 25 | 40,000 |
4 | Harry | 42 | 50,000 |
5 | Willam | 45 | 100,000 |
SELECT Id,Emp_name FROM Employee Where id=1;
This statement return Emp_name with row of table condition satisfying id =1
id | Emp_name |
1 | Jebas |
SELECT Emp_name, Salary FROM Employee Where Salary=40,000;
This statement return Emp_name and Salary of Employee with row of table value condition satisfying salary equal to 40,000.
id | Emp_name | Salary |
1 | Jebas | 40,000 |
3 | Mark | 40,000 |
SELECT Statement with Logical Condition Operator.
SQL using AND
SELECT Emp_name ,Age, Salary FROM Employee Where Age>40 AND Salary>40,000;
This statement return Employee Name, Age, Salary with condition satisfying age greater than 40 and salary greater than 40,000.
Emp_name | Age | Salary |
Harry | 42 | 50,000 |
Willam | 45 | 100,000 |
SQL using OR
SELECT Emp_name ,Age, Salary FROM Employee Where Age>40 OR Salary=40,000;
This statement return Employee Name, Age, Salary with condition satisfying either age greater than 40 or salary greater than 40,000.
Emp_name | Age | Salary |
Jebas | 35 | 40,000 |
Mark | 25 | 40,000 |
Harry | 42 | 50,000 |
Willam | 45 | 100,000 |
SQL using NOT
SELECT Emp_name FROM Employee Where Salary NOT > 35;
This statement return Employee Name with condition satisfying salary not greater than 35.
Emp_name |
Mark |
Thomas |
SELECT Statement with Special Operator.
SQL using IN
SELECT Emp_name FROM Employee WHERE Age IN(25,35);
This statement return Employee Name , whose age are in 25 and 35.
This statement can also written as
SELECT Emp_name FROM Employee WHERE Age = 25 or Age=35;
Emp_name |
Jebas |
Mark |
SQL Using Between
SELECT Emp_name,Age FROM Employee WHERE Age Between 25 and 40;
This statement return Emp_name where Employee age between 25 and 40.
Emp_name | Age |
Jebas | 35 |
Thomas | 30 |
Mark | 25 |
SELECT Emp_name,Age FROM Employee WHERE Age > 25 and Age<= 40;
ANY, ALL, SOME, EXISTS Keyword are used with SUBQUERY will see in https://www.followlect.com/2020/06/any-all-exists-operators-in-sql-query.html post.
Thank You. Like Share and Subscribe 👍 folowlect.com
Comments
Post a Comment