Write SQL select queries with OR operator.
SQL OR Operator The WHERE clause can be combined with OR operators
Example
Table: Employee+----+-------+--------+--------------+| Id | Name | Salary | DepartmentId |+----+-------+--------+--------------+| 1 | Ram | 85000 | 1 || 2 | Henry | 80000 | 2 || 3 | Sam | 60000 | 2 || 4 | Shyam | 60000 | 1 || 5 | Geeta | 90000 | 1 || 6 | Sheet | 90000 | 1 || 7 | Leela | 80000 | 1 || 8 | Geeta | 70000 | 1 |+----+-------+--------+--------------+
Approach
Mysql: Select Employee using OR operator
SELECT * FROM EMPLOYEE WHERE DepartmentId=1 or DepartmentId=2;
Result:
+----+-------+--------+--------------+| Id | Name | Salary | DepartmentId |+----+-------+--------+--------------+| 1 | Ram | 85000 | 1 || 2 | Henry | 80000 | 2 || 3 | Sam | 60000 | 2 || 4 | Shyam | 60000 | 1 || 5 | Geeta | 90000 | 1 || 6 | Sheet | 90000 | 1 || 7 | Leela | 80000 | 1 || 8 | Geeta | 70000 | 1 |+----+-------+--------+--------------+
Mysql: Select Employee using OR operator on a different column
SELECT * FROM EMPLOYEE WHERE DepartmentId=2 or Salary=90000;
Result:
+----+-------+--------+--------------+| Id | Name | Salary | DepartmentId |+----+-------+--------+--------------+| 2 | Henry | 80000 | 2 || 3 | Sam | 60000 | 2 || 5 | Geeta | 90000 | 1 || 6 | Sheet | 90000 | 1 |+----+-------+--------+--------------+
No comments:
Post a Comment