MySQL Where Clause

MySQL WHERE clause is mostly used with SELECT statement to fetch data on specified condition. But, it is also applicable in INSERT, UPDATE, or DELETE statement.

Syntax of WHERE clause

SELECT * FROM table_name WHERE condition;

here, WHERE restricted on SELECT statement result set based on given condition.

MySQL Where Clause Exercise

1. Write a mysql statement to select data of only CS OR IT departments.
Suppose the table is -
+----+--------------+------------+-----+
| id | name         | department | age |
+----+--------------+------------+-----+
|  1 | Maria Gloria | CS         |  22 |
|  2 | John Smith   | IT         |  23 |
|  3 | Gal Rao      | CS         |  22 |
|  4 | Jakey Smith  | EC         |  24 |
|  5 | Rama Saho    | IT         |  22 |
|  6 | Maria Gaga   | EC         |  23 |
+----+--------------+------------+-----+

Solution

The following statement is used to get students data of CS OR IT departments only.

mysql> SELECT name, department, age FROM students
    -> WHERE department = 'CS' OR department = 'IT';

Output of the above statement -

+--------------+------------+-----+
| name         | department | age |
+--------------+------------+-----+
| Maria Gloria | CS         |  22 |
| John Smith   | IT         |  23 |
| Gal Rao      | CS         |  22 |
| Rama Saho    | IT         |  22 |
+--------------+------------+-----+




2. Write a mysql statement to select data of only students with age 22.

The following statement is used to select data of only students with age 22.

mysql> SELECT id, name, department, age FROM students
    -> WHERE age= '22';

Output of the above statement -

+----+--------------+------------+-----+
| id | name         | department | age |
+----+--------------+------------+-----+
|  1 | Maria Gloria | CS         |  22 |
|  3 | Gal Rao      | CS         |  22 |
|  5 | Rama Saho    | IT         |  22 |
+----+--------------+------------+-----+

3. Write a mysql statement to select data of only students with age either in 22,23,24 and department is 'EC'.

The following statement is used to select data of only students with age either in 22,23,24 and department is 'EC'.

mysql> SELECT id, name, department, age FROM students
    -> WHERE age IN(22,23,24) AND department = 'EC';

Output of the above statement -

+----+--------------+------------+-----+
| id | name         | department | age |
+----+--------------+------------+-----+
|  4 | Jakey Smith  | EC         |  24 |
|  6 | Maria Gaga   | EC         |  23 |
+----+--------------+------------+-----+




Related MySQL Exercises

MySQL concatenate
MySQL Get Current Date, User, Version
MySQL Get Maximum Value
MySQL Administrator
MySQL order by
MySQL get difference between two dates
MySQL Pattern Matching
MySQL Join
MySQL Regular Expression
MySQL delete duplicate row
MySQL update multiple rows
Get nth highest salary using MySQL
Display the nth row from MySQL
Commit and rollback in mysql
MySQL SELECT top 5
MySQL display the alternate rows





Read more articles


General Knowledge



Learn Popular Language