SQL
Where Clause is used to extract only those records that satisfied the given condition.
To retrieve the only specific record
To delete the only specific record
To update only specific record
Syntax:
SELECT column1, column 2, ... column n
FROM table_name
WHERE [conditions]
Operators in THE WHERE CLAUSE:
Operator | Description |
---|---|
= | EQUAL |
> | GREATER THAN |
< | LESS THAN |
>= | GREATER THAN OR EQUAL |
<= | LESS THAN OR EQUAL |
<> | NOT EQUAL TO |
It is used to combine two or more than two conditions together.
WHERE condition1 AND condition 2;
WHERE marks>75 and branch='BTech';
OR clause is used to combine two or more than two conditions together, In this case, you need at least one condition to be true then only it will return the result.
WHERE condition1 or condition 2;
WHERE Country='India'AND City='kakinada';
The NOT operator retrieves records if the condition(s) is NOT TRUE.
WHERE NOT condition;
WHERE NOT Country='USA';
Like Operator is used in a WHERE clause to search for a specified pattern in a column.
suppose you want to search those students' records whose names start with ‘A ‘ alphabet.
There are two wildcards used in Like Operator.
The percent sign (%) represents zero, one, or multiple characters.
The underscore sign (_) represents one, single character .
WHERE columnNLIKE pattern;
SELECT * FROM student WHERE student_nameLIKE 'a%';
LIKE Operator | Description |
---|---|
WHERE student_name LIKE ‘a%’ | Find any values that start with “a” |
WHERE student_name LIKE ‘%a’ | Find any values that ends with “a” |
WHERE student_name LIKE ‘%or%’ | Find any values that have “or” in any position |
WHERE student_name LIKE ‘_r%’ | Find any values that have “r” in the second position |
WHERE student_name LIKE ‘a_%’ | Find any values that start with “a” and at least 2 characters in length |
WHERE student_name LIKE ‘a__%’ | Find any values that start with “a” and at least 3 characters in length |
WHERE student_name LIKE ‘a%o’ | Find any values that start with “a” and ends “o” |
BETWEEN returns values that fall within a given range.
BETWEEN is a shorthand for >= AND <=.
BETWEEN operator is inclusive : begin and end values are included.
The underscore sign (_) represents one, single character .
WHERE column_name BETWEEN value1 AND value2;
WHERE marksBETWEEN 60 AND 75;
The IN operator allows you to specify multiple values in a WHERE clause.
The IN operator is a shorthand for multiple OR conditions.
WHERE column_name IN (value1, value2, ...);
OR SELECT column_name(s) FROM table_name (
WHERE column_name IN (SELECT STATEMENT);
WHERE Country IN('india','usa','china');
SELECT * from student
WHERE Country IN(SELECT Country FROM employee);