WHERE Query in DBMS

WHERE Query

On this article, we will learn about WHERE Query in DBMS. Sometimes a user is interested in filtering data and retrieving only specific data that meet certain requirements and conditions, this can be done using WHERE clause which performs data filtering

WHERE Query in DBMS

  • WHERE  clause is used to specify a condition while retrieving and updating data from the database table and display only those records in the table for which the condition specified in the where clause becomes true
  • The WHERE clause is most commonly used with a select, update and delete  statements

Syntax:

SELECT Column1, Column2.......
From table_name
WHERE condition;
Consider the sample table emp
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7839 KING PRESIDENT 17-NOV-81 5000 10
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7566 JONES MANAGER 7839 02-APR-81 2975 20
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7369 SMITH CLERK 7902 17-DEC-80 800 20

Display the records of those employees whose designation is ‘MANAGER’

select * from emp
where job='MANAGER';
O/P

3 rows selected.

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7566 JONES MANAGER 7839 02-APR-81 2975 20
In our emp table, there are three managers that are why only three records are displayed, because the condition is true for only those three rows i.e when the job is a manager.

Display name, job, the salary of that employee whose salary is greater than 2900

select ename,job,sal 
from emp
where sal>2900;
O/P
ENAME JOB SAL
KING PRESIDENT 5000
JONES MANAGER 2975
SCOTT ANALYST 3000
FORD ANALYST 3000
You can also display  only specified columns and filter the records

Operators that can be used in Where query in DBMS

Not only comparison(=) operator many operators shown below can be used with where clause
Operator Description
= Equal to
!= Not Equal to
< Less than
> Greater than
<= Less than or Equal to
>= Greater than or Equal to
BETWEEN Between a specified range of values
LIKE This is used to search for a pattern in value.
IN In a given set of values

Display the records of all employees other than managers

select ename,sal,job,deptno from emp
where job != 'MANAGER';
O/P 4 rows selected
ENAME SAL JOB DEPTNO
KING 5000 PRESIDENT 10
SCOTT 3000 ANALYST 20
FORD 3000 ANALYST 20
SMITH 800 CLERK 20

Display records of all employees whose salary is between 1000/- to 2500/-

select ename,sal,job,deptno from emp
where SAL between 1000 AND 2500;
  • Here we can make use of between operator where the condition is applied for all the values within the specified range
  • In the above example all the records of employees whose salary in between the range of 2000 and 2500 is displayed(inclusive of 2000 and 2500)

Deleting records based on the condition specified in where clause

Conditions can be specified by using where the where clause for deletion and updation of data
delete from emp
where empno=7934;
O/P
1 row deleted

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription