Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
7 views5 pages

Mis - SQL - Worksheet - 2

This document provides an overview of the SQL WHERE clause, including its syntax and various operators such as =, <>, >, <, BETWEEN, IN, AND, OR, NOT, and LIKE. It includes examples of how to use these operators in SELECT, UPDATE, and DELETE statements to filter records based on specified conditions. Additionally, it explains the use of wildcards with the LIKE operator and the syntax for updating and deleting records in a database.

Uploaded by

Chappa Varshitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

Mis - SQL - Worksheet - 2

This document provides an overview of the SQL WHERE clause, including its syntax and various operators such as =, <>, >, <, BETWEEN, IN, AND, OR, NOT, and LIKE. It includes examples of how to use these operators in SELECT, UPDATE, and DELETE statements to filter records based on specified conditions. Additionally, it explains the use of wildcards with the LIKE operator and the syntax for updating and deleting records in a database.

Uploaded by

Chappa Varshitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

MIS | SQL WORK SHEET – 2

MBA –MS 2025-27 Batch

The WHERE Clause


The WHERE clause is used to filter records.

The WHERE clause is used to extract only those records that fulfill a specified condition.

Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition;

Note: The WHERE clause is not only used in SELECT statement, it is also used in UPDATE,
DELETE statement, etc.!

Operators in The WHERE Clause

The following operators can be used in the WHERE clause:

Operator Description
= Equal
<> Not equal.
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
BETWEEN Between a certain range
IN To specify multiple
possible values for a
column
Example :
SELECT *
FROM STUDENT
WHERE NAME=’SABASTIN’;

SELECT *
FROM STUDENT
WHERE NAME <>’SABASTIN’;

SELECT *
FROM STUDENT
WHERE AGE > 20;

SELECT *
FROM STUDENT
WHERE AGE < 30;

SELECT *
FROM STUDENT
WHERE AGE >= 20;

SELECT *
FROM STUDENT
WHERE AGE <= 30;

TO FETCH DATE

Syntax :
SELECT *
FROM table_name
WHERE DOB = ‘YYYY-MM-DD;

Example :

SELECT *
FROM STUDENT
WHERE DOB= ’1986-11-17’

The SQL BETWEEN Operator


The BETWEEN operator selects values within a given range. The values can
be numbers, text, or dates.

The BETWEEN operator is inclusive: begin and end values are included.

BETWEEN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

Example :
SELECT *
FROM STUDENT
WHERE ID BETWEEN 001 AND 005;

The SQL 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.

IN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);

Example :
SELECT *
FROM STUDENT
WHERE NAME IN (‘Sabastin’, ‘Shirl’);
The SQL AND, OR and NOT Operators
The WHERE clause can be combined with AND, OR, and NOT operators.

The AND and OR operators are used to filter records based on more than one
condition:

 The AND operator displays a record if all the conditions separated by


AND are TRUE.
 The OR operator displays a record if any of the conditions separated by
OR is TRUE.

The NOT operator displays a record if the condition(s) is NOT TRUE.

AND Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;

Example :
SELECT *
FROM DEPARTMENT
WHERE DEPTNAME = ’cse’ AND DEPTPLACE = ‘blr’

OR Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;

Example :
SELECT *
FROM DEPARTMENT
WHERE DEPTNAME = ’cse’ OR DEPTNAME = ‘ece’

NOT Syntax
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;

Example :
SELECT *
FROM DEPARTMENT
WHERE NOT DEPTNAME = ’cse’

The SQL LIKE Operator


The LIKE operator is used in a WHERE clause to search for a specified pattern
in a column.
There are two wildcards often used in conjunction with the LIKE operator:

 % - The percent sign represents zero, one, or multiple characters


 _ - The underscore represents a single character

LIKE Syntax

SELECT column1, column2, ...


FROM table_name
WHERE columnN LIKE pattern;

LIKE Operator Description


WHERE CustomerName LIKE 'a%' Finds any values that start with "a"
WHERE CustomerName LIKE '%a' Finds any values that end with "a"
WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any position
WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second position
WHERE CustomerName LIKE 'a_%' Finds any values that start with "a" and are at least 2
characters in length
WHERE CustomerName LIKE 'a__%' Finds any values that start with "a" and are at least 3
characters in length
WHERE ContactName LIKE 'a%o' Finds any values that start with "a" and ends with "o"

Examples :

SELECT *
FROM DEPARTMENT
WHERE PLACE LIKE ‘b%’

SELECT *
FROM DEPARTMENT
WHERE PLACE LIKE ‘%e’

SELECT *
FROM DEPARTMENT
WHERE PLACE LIKE ‘%ys%’

SELECT *
FROM DEPARTMENT
WHERE PLACE LIKE ‘_a%’

SELECT *
FROM DEPARTMENT
WHERE PLACE LIKE ‘m__%’

SELECT *
FROM DEPARTMENT
WHERE PLACE LIKE ‘b%e’
The MySQL UPDATE Statement
The UPDATE statement is used to modify the existing records in a table.

UPDATE Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

Examples :

UPDATE Customers
SET ContactName = 'Jacklin', City = 'Pune'
WHERE CustomerID = 1;

The MySQL DELETE Statement


The DELETE statement is used to delete existing records in a table.

DELETE Syntax
DELETE FROM table_name WHERE condition;
Examples :

DELETE FROM Customers WHERE CustomerName='Sabastin;

You might also like