MySQL

Chapter 7 - DQL (Data Query Language)

'=' (equal to) Operator

In MySQL, the = (equal) operator is one of the most commonly used operators in Data Query Language (DQL) for filtering rows where a column's value matches a specified value. It is often used in the WHERE clause to compare values in queries, allowing you to retrieve records that match a given condition. Whether you're comparing numbers, strings, or dates, the = operator is essential for building effective SQL queries.

Here’s a breakdown of how to use the = operator with examples and helpful tips for new students:

  1. Basic Syntax for = (Equal):

    • The = operator is used to return rows where the column value matches the specified value exactly.
    • Syntax:
    SELECT * FROM employees WHERE department_id = 3;
    • This query retrieves all employees who belong to department 3.
  2. Using = with String Comparisons:

    • The = operator can also be used to compare string values. String comparisons are case-insensitive in MySQL unless explicitly set otherwise.
    • Example:
    SELECT * FROM employees WHERE last_name = 'Smith';
    • This query returns all employees whose last name is "Smith".
  3. Using = with Date Values:

    • You can also use the = operator to filter rows based on date values.
    • Example:
    SELECT * FROM employees WHERE hire_date = '2022-01-01';
    • This query retrieves all employees who were hired on January 1, 2022.
  4. Combining = with Other Conditions:

    • You can combine the = operator with other operators like AND or OR to create more complex conditions.
    • Example:
    SELECT * FROM employees WHERE department_id = 3 AND salary = 50000;
    • This query returns employees who belong to department 3 and have a salary of exactly 50,000.
  5. Using = with NULL Values:

    • The = operator cannot be used to check for NULL values directly because NULL represents an unknown value. Instead, you should use IS NULL to check for NULL values.
    • Example:
    SELECT * FROM employees WHERE manager_id IS NULL;
    • This query retrieves all employees who do not have a manager assigned (i.e., where the manager_id is NULL).
  6. Using = in Joins:

    • The = operator is commonly used in JOIN operations to match rows between related tables based on a common key.
    • Example:
    SELECT e.employee_id, e.first_name, d.department_name FROM employees e JOIN departments d ON e.department_id = d.department_id;
    • This query joins the employees and departments tables where the department_id matches.
  7. Performance Considerations:

    • The = operator is highly efficient, especially when comparing values in indexed columns. Ensure that commonly queried columns, such as id or department_id, are indexed for better performance.
    • Example:
    SELECT * FROM employees WHERE employee_id = 1001;
    • If employee_id is indexed, this query will perform faster.
  8. Case Sensitivity in = Comparisons:

    • In MySQL, string comparisons using = are case-insensitive by default. However, if you need a case-sensitive comparison, you can use the BINARY keyword.
    • Example:
    SELECT * FROM employees WHERE BINARY first_name = 'John';
    • This query will return rows where the first name matches "John" exactly, including case sensitivity.

These examples demonstrate the versatility of the = operator in MySQL. Whether you're comparing numbers, strings, or dates, this operator is essential for querying specific values and filtering data efficiently.

Tansy SQL Course | '=' (equal to) Operator | Chapter 7 | Lesson 15 - Video Thumbnail

Test code

Fetch records for clients identified as female.

SELECT *
FROM org_client
WHERE gender = 'F';
Try it now

Fetch orders where the order statuses are equal to 5.

SELECT *
FROM act_order
WHERE order_status_id = 5;
Try it now

Retrieve orders with shipping dates matching December 12th.

SELECT *
FROM act_order
WHERE shipped_date = '2023-12-12';

-- Oracle WHERE shipped_date = 12/12/23';
Try it now

Example 1:

Let's explore the procedure of retrieving information from a designated table using the SQL '=' (equal to) operator with string data. Fetch records for clients identified as females.

Example 1 - Raw data from client table

i

Example 1 - Query

SELECT * FROM org_client WHERE gender = 'F';

Example 1 - Query data mapping

i

In the provided image, the green color signifies the data that has been selected or satisfies the conditions specified in our query. Data points in red indicate information that does not meet the criteria set by the query.

Example 1 - Query Output

i

Example 2:

Let's delve into the process of retrieving information from a specified table using the SQL '=' (equal) operator with numeric data. Retrieve orders with statuses that match the value 5.

Example 2 - Raw data from orders table

i

Example 2 - Query

SELECT * FROM act_order WHERE order_status_id = 5;

Example 2 - Query data mapping

i

In the provided image, the green color signifies the data that has been selected or satisfies the conditions specified in our query. Data points in red indicate information that does not meet the criteria set by the query.

Example 2 - Query Output

i

Example 3:

Let's explore the process of retrieving information from a designated table using the SQL '=' (equal) operator with date data. Fetch orders with shipping dates that match December 12th.

Example 3 - Raw data from orders table

i

Example 3 - Query

SELECT * FROM act_order WHERE shipped_date = '2023-12-12';

Example 3 - Query data mapping

i

In the provided image, the green color signifies the data that has been selected or satisfies the conditions specified in our query. Data points in red indicate information that does not meet the criteria set by the query. Please be aware that the query will attempt to filter based on the value of December 12th. Since many records have NULL for the shipped date value, NULL cannot be compared as it is not an actual value. Consequently, these null records will be excluded from the results as well.

Example 3 - Query Output

i

Comments(0 comments)

Comments Not Found