MYSQL Tutorial Part 4
MYSQL CONDITIONS
In this part, will learn about MySQL conditions. Using some operators will filter records from the database table.
Example:
Example:
Example:
LIKE:
- The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
- It performs pattern matching.
- The percent sign (%) represents zero, one, or multiple characters
- The underscore sign (_) represents a single character
Syntax
SELECT * FROM table_name WHERE column_name LIKE pattern;
Example
SELECT * FROM Students WHERE district LIKE 'Ch%'; - It finds the district starts with the letter Ch
SELECT * FROM Students WHERE district LIKE '%ai'; - It finds the district ends with the letter ai
SELECT * FROM Students WHERE district LIKE '%an%'; - It finds the district with the letter an in any position
SELECT * FROM Students WHERE district LIKE 'C%i'; - It finds the district starts with the letter C and ends with the letter i
SELECT * FROM Students WHERE district LIKE 'a_'; - It finds the district starts with the letter a and it should be at least 2 characters in length
SELECT * FROM Students WHERE district LIKE '_h'; - It finds the district with the letter h in the second position
IN:
- The IN operator allows you to specify multiple values in a WHERE clause.
- It is like the OR condition.
- It is used to reduce the use of multiple OR.
Syntax
SELECT column_names FROM table_name WHERE column_name IN (value1, value2, ...);
or:
SELECT column_names FROM table_name WHERE column_name IN (SELECT STATEMENT);
Example
SELECT * FROM Students WHERE district IN ('Trichy', 'Thanjavur', 'Chennai');
SELECT * FROM Students WHERE district IN (SELECT dist_name FROM District);
BETWEEN:
- It finds the value within a given range.
- The values can be numbers, text, or dates.
- Begin and end values are included.
Syntax
SELECT * FROM table_name WHERE column_name BETWEEN value1 AND value2;
Example
SELECT * FROM Students WHERE fees BETWEEN 30000 AND 60000;
ALIAS:
- It is used to create a temporary name for a particular table or column.
- It makes the column name or table name more readable.
- An alias only exists for the duration of that query.
- It is created with the AS keyword.
Syntax
SELECT column_name AS alias_name FROM table_name;
Example
SELECT name AS student_name FROM Students;
- 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.
Example:
SELECT * FROM Students WHERE district IS NULL;
Example:
SELECT * FROM Students WHERE district IS NOT NULL;
EXISTS:
- The EXISTS operator is used to test for the existence of any record in a subquery.
- The EXISTS operator returns TRUE if the subquery returns one or more records.
Syntax
SELECT column_names FROM table_name WHERE EXISTS (SELECT column_name FROM table_name WHERE condition);
Example
SELECT name FROM Departments WHERE EXISTS (SELECT department FROM Students WHERE Students.department = Department.name AND fees < 50000);
ANY:
The ANY operator:
- returns a boolean value as a result
- returns TRUE if ANY of the subquery values meet the condition
ANY means that the condition will be true if the operation is true for any of the values in the range.
Syntax
SELECT column_names FROM table_name WHERE column_name operator ANY (SELECT column_name FROM table_name WHERE condition);
Example
SELECT name, department FROM Students WHERE department = ANY (SELECT name FROM Department WHERE location = 'Block1');
ALL:
The ALL operator:
- returns a boolean value as a result
- returns TRUE if ALL of the subquery values meet the condition
- is used with SELECT, WHERE, and HAVING statements
ALL means that the condition will be true only if the operation is true for all values in the range.
Syntax With SELECT
SELECT ALL column_names FROM table_name WHERE condition;
Syntax With WHERE or HAVING
SELECT column_names FROM table_name WHERE column_name operator ALL (SELECT column_name FROM table_name WHERE condition);
Example
SELECT name, department FROM Students WHERE department = ALL (SELECT name FROM Department WHERE location = 'Block1');
Comments
Post a Comment