w3resource

MySQL equal operator

equal operator

MySQL equal operator performs an equality comparison.

Syntax:

=

MySQL Version: 8.0

Example: MySQL equal operator

The following MySQL statement checks if 1 is equal to 1, if 1 is equal to 2, if NULL is equal to NULL, if NULL is equal to 3 and if 3 is equal to NULL.

Code:

SELECT 1 = 1, 1=2,NULL = NULL, NULL=3,3= NULL;

Output:

mysql> SELECT 1 = 1, 1=2,NULL = NULL, NULL=3,3= NULL;
+-------+-----+-------------+--------+---------+
| 1 = 1 | 1=2 | NULL = NULL | NULL=3 | 3= NULL |
+-------+-----+-------------+--------+---------+
|     1 |   0 |        NULL |   NULL |    NULL | 
+-------+-----+-------------+--------+---------+
1 row in set (0.00 sec)

Example: MySQL equal operator with WHERE clause

The following MySQL statement will fetch the rows after checking whether the publisher belongs to the USA.

Sample table: publisher


Code:


-- This query selects specific columns from the 'publisher' table where the country is 'USA'.
SELECT pub_name, country, pub_city, estd
-- This statement specifies the columns to be retrieved: 'pub_name', 'country', 'pub_city', and 'estd'.
FROM publisher
-- This part of the query specifies the table from which data is being retrieved, which is 'publisher'.
WHERE country = 'USA';
-- This clause filters the rows to include only those where the 'country' column has the value 'USA'.

Explanation:

  • The purpose of this SQL query is to retrieve information about publishers that are located in the USA.

  • SELECT pub_name, country, pub_city, estd: This part of the query specifies the columns to be selected from the 'publisher' table. It includes the publisher's name (pub_name), country (country), city (pub_city), and establishment date (estd).

  • FROM publisher: This part specifies the table from which the data is being selected, which is the 'publisher' table.

  • WHERE country = 'USA': This clause filters the results to include only those rows where the country column has the value 'USA'. This ensures that only publishers based in the USA are included in the results.

Output:

mysql> SELECT pub_name,country,pub_city,estd
    -> FROM publisher           
    -> WHERE country='USA';
+--------------------------+---------+----------+------------+
| pub_name                 | country | pub_city | estd       |
+--------------------------+---------+----------+------------+
| Jex Max Publication      | USA     | New York | 1969-12-25 | 
| Mountain Publication     | USA     | Houstan  | 1975-01-01 | 
| Summer Night Publication | USA     | New York | 1990-12-10 | 
+--------------------------+---------+----------+------------+
3 rows in set (0.00 sec)

Relational Algebra Expression:

Relational Algebra Expression: MySQL equal operator with WHERE clause.

Relational Algebra Tree:

Relational Algebra Tree: MySQL equal operator with WHERE clause.

Slideshow of MySQL Comparison Function and Operators

Previous: NULL Safe equal to operator (<=>)
Next: Greater than or equal operator(>=)



Follow us on Facebook and Twitter for latest update.