0% found this document useful (0 votes)
39 views16 pages

SQL WHERE Clause and Operators Guide

The document provides an overview of SQL's WHERE clause, which specifies criteria for filtering records in query results. It covers various MySQL operators, including arithmetic, comparison, logical operators, and the use of wildcard characters with the LIKE operator. Additionally, it explains handling NULL values and sorting results using the ORDER BY clause.

Uploaded by

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

SQL WHERE Clause and Operators Guide

The document provides an overview of SQL's WHERE clause, which specifies criteria for filtering records in query results. It covers various MySQL operators, including arithmetic, comparison, logical operators, and the use of wildcard characters with the LIKE operator. Additionally, it explains handling NULL values and sorting results using the ORDER BY clause.

Uploaded by

khajezjhozhua131
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Information

Management
What is where clause?
 In a SQL statement, the WHERE clause specifies criteria that field
values must meet for the records that contain the values to be included
in the query results.
 Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition;

 Example:
SELECT * FROM products WHERE stock = 18;
MySQL Operators
 Operators perform arithmetic, comparison, and logical operations to
manipulate and retrieve data from databases.
Arithmetic Operators
Operator Description SYNTAX
+ Add SELECT 20 + 30

- Subtract SELECT 20 - 30

* Multiply SELECT 20 * 30

/ Divide SELECT 20 / 30

% Modulo SELECT 17 % 5;
Comparison Operators
Operator Description Example
= Equal to SELECT * FROM products
WHERE stock = 18;

> Greater than SELECT * FROM products


WHERE stock > 30;

< Less than SELECT * FROM products


WHERE stock < 30;

>= Greater than or SELECT * FROM products


equal to WHERE stock >= 30;

<= Less than or equal SELECT * FROM products


to WHERE stock <= 30;

<> Not equal to SELECT * FROM products


WHERE stock <> 18;
Logical Operators
Operator Description Example
AND TRUE if all the conditions SELECT * FROM products
separated by AND is TRUE WHERE category = "Fashion"
AND is_active = "1";
OR TRUE if any of the SELECT * FROM products
conditions separated by WHERE category = "Fashion"
OR is TRUE OR is_active = "1";

NOT Displays a record if the SELECT * FROM products


condition(s) is NOT TRUE WHERE NOT category =
"Fashion";
BETWEEN TRUE if the operand is SELECT * FROM products
within the range of WHERE price BETWEEN 50
comparisons AND 60;

IN TRUE if the operand is SELECT * FROM products


equal to one of a list of WHERE category IN
expressions ('Fashion','Sports');
Logical Operators
Operator Description Example
LIKE TRUE if the operand SELECT * FROM products
matches a pattern WHERE category LIKE 's%';

ALL TRUE if all of the subquery SELECT * FROM Products


values meet the condition WHERE Price > ALL (SELECT
Price FROM Products WHERE
Price > 500);
ANY TRUE if any of the SELECT * FROM Products
subquery values meet the WHERE Price > ANY (SELECT
condition Price FROM Products WHERE
Price > 50);
EXISTS TRUE if the subquery SELECT * FROM Products
returns one or more WHERE EXISTS (SELECT Price
records FROM Products WHERE Price
> 50);
SOME TRUE if any of the SELECT * FROM Products
subquery values meet the WHERE Price > SOME (SELECT
condition Price FROM Products WHERE
Price > 20);
Wildcard Characters
 A wildcard character is used to substitute one or more
characters in a string.

 Wildcard characters are used with the LIKE operator. The LIKE
operator is used in a WHERE clause to search for a specified pattern in
a column.
Wildcard Characters in MySQL
Symbol Description Example Example Query
% Represents zero or bl% finds bl, SELECT * FROM products
more characters black, blue, and WHERE category LIKE 'bl%';
blob
_ Represents a single h_t finds hot, hat, SELECT * FROM products
character and hit WHERE category LIKE 'h_t';
Wildcard Characters
 The wildcards can also be used in combinations!

 Here are some examples showing different LIKE operators with '%' and
'_' wildcards:
Wildcard Characters in MySQL
LIKE Operator Description Example
WHERE CustomerName LIKE 'a%' Finds any values that starts with "a" SELECT * FROM products WHERE
category LIKE 'a%';

WHERE CustomerName LIKE '%a' Finds any values that ends with "a" SELECT * FROM products WHERE
category LIKE '%a';

WHERE CustomerName LIKE '%or%' Finds any values that have "or" in SELECT * FROM products WHERE
any position category LIKE '%or%';

WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the SELECT * FROM products WHERE
second position category LIKE '_r%';

WHERE CustomerName LIKE 'a_%_%' Finds any values that starts with "a" SELECT * FROM products WHERE
and are at least 3 characters in category LIKE 'a_%_%';
length
WHERE ContactName LIKE 'a%o' Finds any values that starts with "a" SELECT * FROM products WHERE
and ends with "o" category LIKE 'a%o';
MySQL NULL Values
 A field with a NULL value is a field with no value.

 If a field in a table is optional, it is possible to insert a new record or update a record


without adding a value to this field. Then, the field will be saved with a NULL value.

 It is not possible to test for NULL values with comparison operators, such as =, <, or
<>.

 We will have to use the IS NULL and IS NOT NULL operators instead.

IS NULL Syntax: IS NOT NULL Syntax:


SELECT column_names SELECT column_names

FROM table_name FROM table_name

WHERE column_name IS NULL; WHERE column_name IS NOT NULL;


MySQL NULL Values
Operator Description Example
IS NULL The IS NULL operator SELECT firstname, middlename, lastname,
is used to test for suffixes
empty values (NULL FROM tbl_customers
values). WHERE suffixes IS NULL;
IS NOT NULL The IS NOT SELECT firstname, middlename, lastname,
NULL operator is used suffixes
to test for non-empty FROM tbl_customers
values (NOT NULL WHERE suffixes IS NOT NULL;
values).
MySQL ORDER BY
 The ORDER BY keyword is used to sort the result-set in ascending or
descending order.

 The ORDER BY keyword sorts the records in ascending order by default.


To sort the records in descending order, use the DESC keyword.

 Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE ORDER BY column1, column2, … ASC|DESC;

 Example:
SELECT * FROM Customers ORDER BY Country;
MySQL ORDER BY
Keyword Description Example
ORDER BY This SQL statement selects all customers from the SELECT * FROM customers ORDER
"Customers" table, sorted by the "Country" column BY Country;

ORDER BY DESC This SQL statement selects all customers from the SELECT * FROM customers ORDER
"Customers" table, sorted DESCENDING by the "Country" BY Country DESC;
column
ORDER BY Several This SQL statement selects all customers from the SELECT * FROM customers ORDER
Columns "Customers" table, sorted by the "Country" and the BY Country, CustomerName;
"CustomerName" column. This means that it orders by
Country, but if some rows have the same Country, it
orders them by CustomerName

ORDER BY Several SQL statement selects all customers from the SELECT * FROM customers ORDER
Columns "Customers" table, sorted ascending by the "Country" BY Country ASC, CustomerName
and descending by the "CustomerName" column DESC;
Thank you!

You might also like