Basic Operations With MySQL - Part 3
Basic Operations With MySQL - Part 3
LOGICAL 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
is 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.
The AND operator displays a record if both the first condition and the second
condition is true.
The OR operator displays a record if either the first condition or the second
condition is true.
AND Condition
AND condition is used with SELECT, INSERT, UPDATE or DELETE
statements to test two or more conditions in an individual query. Here the
operation will be performed only on those records which satisfies all the
conditions.
If any of the conditions evaluates to false, then those records will not be
selected for the operation (SELECT, INSERT, UPDATE or DELETE).
Syntax:
WHERE condition1
AND condition2
...
AND condition_n;
Example: Display the details of the customer whose fname is Ajeet and
cus_ID is 3.
SELECT * FROM Customer
WHERE fname = 'Ajeet' AND cus_id > 3;
OR Condition
The MySQL OR condition specifies that if you take two or more conditions then
one of the conditions must be fulfilled to get the records as result.
SYNTAX:
WHERE condition1
OR condition2
...
OR condition_n;
We can use AND & OR condition both together with the SELECT,
INSERT, UPDATE and DELETE statement. While combine these
conditions, you must be aware where to use round brackets so that the
database know the order to evaluate each condition.
Syntax:
WHERE condition1
AND condition2
...
OR condition_n;
Output:
LIKE condition
Syntax:
Parameters:
• expression: It specifies a column or field.
• pattern: It is a character expression that contains pattern
matching.
• escape_character: It is optional. It allows you to test for literal
instances of a wildcard character such as % or _. If you do not
provide the escape_character, MySQL assumes that "\" is the
escape_character.
Syntax:
where, value1, value2, ... or value_n: These are the values to test against
expression. If any of these values matches expression, then the IN
condition will evaluate to true. This is a quick method to test if any one
of the values matches expression.
RANGE SEARCHING
The BETWEEN operator selects a range of data between two values. The
values can be numbers, text, or dates.
Syntax:
SELECT column_name FROM table_name
Example: Display the productno, name profit and selling price from
Product_master table, where profits between 500 and 800.
Example: Display the productno, name, profit and selling price whose
profits is other than the range 100 and 350.
Example: Display the details of the products with price between 10000
and 20000 of vendors other than HP, Vivo and Nokia
AGGREGATE FUNCTIONS
MATH BASED FUNCTIONS
• upper(char)
Returns character with all case in upper case letters.
Select upper(‘welcome’) From Dual; Results in WELCOME
UPDATE employees
SET salary = salary + TO_NUMBER('100.00','9G999D99')
WHERE last_name = 'Perkins';
Example:
TO_DATE('2003/07/09', 'yyyy/mm/dd')
SQL ALIASES
The following SQL statement selects all the orders from the customer
with CustomerID=4. We use the "Customers" and "Orders" tables, and
give them the table aliases of "c" and "o" respectively (Here we use aliases
to make the SQL shorter):
Example:
2. Select productno total quantity ordered for productno 1001 and 3453.
Select productno, SUM(qtyOrdered) “TOTAL”
FROM SalesOrder
GROUP BY productno
HAVING productno=1001 AND productno=3453;
Result:
productno TOTAL
1001 13
3453 15