Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
msbacademy.org
SQL
Where Clause
msbacademy.org
Where Clause
1. Where
2. AND
3. OR
4. Like
msbacademy.org
Where Clause
Where Clause is used to extract only those records that
satisfied the given condition, like fetch only those student record
which has more than 75 marks
msbacademy.org
When use where clause ?
1. To retrieve the only specific record
2. To delete the only specific record
3. To update only specific record
msbacademy.org
Operators in The WHERE Clause
1. =
2. <>
3. >
4. <
5. >=
6. <=
7. BETWEEN
8. LIKE
9. IN
10.NOT
msbacademy.org
Syntax
SELECT column_name,column_name FROM table_name
WHERE column_name operator value;
Ex: SELECT * from student WHERE marks>75;
msbacademy.org
And Clause
And clause is used to combine two or more than two
conditions together. In this case, you need must be true both
condition then return the result otherwise it won’t return any
result.
Suppose you want to get only students records who have
more than 75 marks and CSE branch in that case you can
combine these two conditions by using AND clause.
msbacademy.org
Syntax
SELECT columns FROM table_name
WHERE condition 1 AND condition 2;
Ex: SELECT * from student
WHERE marks>75 AND branch=‘cse’;
msbacademy.org
OR Clause
OR clause is used to combine two or more than two
conditions together, In this case, you need at least one condition
must true then only it will return the result.
Suppose you want to get only those students records who
have more than 75 marks or CSE branch in that case you can be
combine these two conditions by using OR clause.
msbacademy.org
Syntax
SELECT columns FROM table_name
WHERE condition 1 OR condition 2;
Ex: SELECT * from student
WHERE marks>75 OR branch=‘cse’;
msbacademy.org
Not Operator
The NOT operator retrieves a records if the condition(s) is
NOT TRUE
msbacademy.org
Syntax
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
Ex: SELECT * FROM student
WHERE NOT Country=‘usa';
msbacademy.org
Like Operator
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 starts with M alphabet.
msbacademy.org
Syntax
SELECT column_name FROM table_name
WHERE column_name LIKE pattern;
Ex: SELECT * FROM student
WHERE student_name LIKE ‘m%';
msbacademy.org
LIKE operators with '%' and '_‘Wildcard Characters
LIKE Operator Description
WHERE student_name LIKE ‘ra%' Finds any values that starts with “ra"
WHERE student_name LIKE '%ra' Finds any values that ends with “ra"
WHERE student_name LIKE '%sr%' Finds any values that have “sr" in any position
WHERE student_name LIKE '_m%' Finds any values that have “m" in the second
position
WHERE student_name LIKE ‘e_%_%' Finds any values that starts with “e" and are at
least 3 characters in length
WHERE student_name LIKE ‘s%d' Finds any values that starts with “s" and ends with
“d"
msbacademy.org
Between Operator
1. BETWEEN returns values that fall within a given range.
2. BETWEEN is a shorthand for >= AND <=.
3. BETWEEN operator is inclusive: begin and end values are
included.
msbacademy.org
Syntax
SELECT column-names
FROM table-name
WHERE column-name BETWEEN value1 AND value2;
Ex: SELECT * FROM student
WHERE marks BETWEEN 60 AND 75;
msbacademy.org
IN operator
The IN operator allows you to specify multiple values in a
WHERE clause.
The IN operator is a shorthand for multiple OR conditions
msbacademy.org
Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
Or
SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);
msbacademy.org
IN operator Examples
SELECT * FROM student
WHERE Country IN (‘india', ‘usa', ‘china');
Or
SELECT * FROM student
WHERE Country IN (SELECT Country FROM employee);
msbacademy.org
Follow Us:
/msbacademy
/msb academy
/msb_academy
/msb_academy
Thank You

More Related Content

Where conditions and Operators in SQL