for comparisons and BETWEEN, IN, LIKE for more complex conditions. The AND, OR and NOT operators allow combining multiple conditions to further refine the results. Aliases can be used to temporarily rename tables or columns to make the SQL more readable. The LIKE operator supports wildcard characters % and _ to match patterns within a string."> for comparisons and BETWEEN, IN, LIKE for more complex conditions. The AND, OR and NOT operators allow combining multiple conditions to further refine the results. Aliases can be used to temporarily rename tables or columns to make the SQL more readable. The LIKE operator supports wildcard characters % and _ to match patterns within a string.">
Practical 4
Practical 4
SELECT * FROM Customers
WHERE Country = 'Mexico';
SELECT * FROM Customers
WHERE CustomerID = 1;
Operator Description
= Equal
AND Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
OR Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
NOT Syntax
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
Example:
SELECT * FROM Customers
WHERE Country = 'Germany' AND City = 'Berlin';
SELECT * FROM Customers
WHERE City = 'Berlin' OR City = 'Stuttgart';
SELECT * FROM Customers
WHERE NOT Country = 'Germany';
SELECT * FROM Customers
WHERE Country = 'Germany' AND (City = 'Berlin' OR City = 'Stuttgart');
SELECT * FROM Customers
WHERE NOT Country = 'Germany' AND NOT Country = 'USA';
BETWEEN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;
SELECT * FROM Products
WHERE Price NOT BETWEEN 10 AND 20;
SELECT * FROM Customers
WHERE Country IN ('Germany', 'France', 'UK');
SELECT * FROM Customers
WHERE Country NOT IN ('Germany', 'France', 'UK');
MySQL Aliases
Aliases are used to give a table, or a column in a table, a temporary name.
SELECT column_name AS alias_name
FROM table_name;
SELECT column_name(s)
FROM table_name AS alias_name;
SELECT CustomerID AS ID, CustomerName AS Customer
FROM Customers;
The percent sign and the underscore can also be used in combinations!
WHERE CustomerName LIKE 'a Finds any values that start with "a"
%'
WHERE CustomerName LIKE Finds any values that end with "a"
'%a'
WHERE CustomerName LIKE Finds any values that have "or" in any position
'%or%'
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 Finds any values that start with "a" and are at
'a__%' least 3 characters in length
WHERE ContactName LIKE 'a Finds any values that start with "a" and ends
%o' with "o"
SELECT * FROM Customers
WHERE CustomerName LIKE 'a%';
SELECT * FROM Customers
WHERE CustomerName LIKE '%a';
SELECT * FROM Customers
WHERE CustomerName LIKE '%or%';
SELECT * FROM Customers
WHERE CustomerName LIKE '_r%';
SELECT * FROM Customers
WHERE CustomerName LIKE 'a__%';
SELECT * FROM Customers
WHERE CustomerName NOT LIKE 'a%';