MySQL - SPLessons

MySQL Clauses

Chapter 6

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

MySQL Clauses

MySQL Clause

shape Clause

Clause is defined as a set of rules, that makes to understand the concepts of MySQL command in Database. MySQL Clauses are very similar to SQL clause, except some functional operations.

Where Clause

shape Description

To specify conditions in MySQL command use Where clause i.e., Select * from the table name will show all the rows from the table and Where clause will specify the condition for the result set in the table.  

shape Syntax

Select * from <table_name> where <condition> ;

table_name => Any accurate table.

condition => condition is a logic to get a specific record.

shape Examples

By viewing the below example, the concept of where clause can be easily understood.

mysql> select * from employee;
+--------+-------+-------+
| emp_id | ename | salary|
+--------+-------+-------+
|   1001 | mike  | 12000 |
|   1002 | rambo | 13000 |
+--------+-------+-------+
2 rows in set (0.00 sec)

mysql> select * from employee where ename='rambo';
+--------+-------+-------+
| emp_id | ename | salary|
+--------+-------+-------+
|   1002 | rambo | 13000 |
+--------+-------+-------+
1 row in set (0.00 sec)


mysql> select * from employee where ename='mike';
+--------+-------+-------+
| emp_id | ename | salary|
+--------+-------+-------+
|   1001 | mike  | 12000 |
+--------+-------+-------+
1 row in set (0.00 sec)

The above example tells that, when where clause operation is performed on a column name like rambo, then it automatically displays the entire details of rambo like emp_id and salary.

Order by Clause

shape Description

To arrange data of a table either in ascending order or descending order based on single column use Orderby clause i.e., Select * from the table name will show all the rows from the table and order by clause is used to arrange the specific column in ascending or descending order.

shape Syntax

Select * from <table_name> ORDER BY <column_name> DESC;

table_name => Any accurate table.

column_name => The condition that one can perform on a column by using Order by clause.

shape Examples

By viewing the below example, the concept of Order by clause can be easily understand.

mysql> select * from employee;
+--------+-------+-------+
| emp_id | ename | salary|
+--------+-------+-------+
|   1001 | mike  | 12000 |
|   1002 | rambo | 13000 |
+--------+-------+-------+
2 rows in set (0.00 sec)

mysql> select * from employee order by salary desc;
+--------+-------+-------+
| emp_id | ename | salary|
+--------+-------+-------+
|   1002 | rambo | 13000 |
|   1001 | mike  | 12000 |
+--------+-------+-------+
2 rows in set (0.06 sec)

mysql> select * from employee order by salary;
+--------+-------+-------+
| emp_id | ename | salary|
+--------+-------+-------+
|   1001 | mike  | 12000 |
|   1002 | rambo | 13000 |
+--------+-------+-------+
2 rows in set (0.00 sec)

The above example tells that, when order by clause operation is performed on a column name like salary, then it automatically displays the entire details of employees in descending order.

Groupby Clause

shape Description

To divide the data of a table into groups based on single column use GroupBy clause.

shape Syntax

Select <column_name>,Group <function1>,Group <function2> from <table_name> Group by <column_name>

table_name => Any accurate table.

column_name => The condition that one can perform on a column by using Group by clause.

shape Examples

By viewing the below example, the concept of group by clause can be easily understand.

mysql> select * from employee;
+--------+-------+-------+--------+
| emp_id | ename | salary| deptno |
+--------+-------+-------+--------+
|   1001 | mike  | 12000 |     10 |
|   1002 | rambo | 13000 |     20 |
|   1003 | kate  | 14000 |     10 |
|   1003 | jeo   | 14000 |     20 |
|   1003 | finn  | 14000 |     30 |
+--------+-------+-------+--------+
5 rows in set (0.00 sec)

mysql> select deptno,max(salary) from employee group by deptno;
+--------+------------+
| deptno | max(salary)|
+--------+------------+
|     10 |    14000   |
|     20 |    14000   |
|     30 |    14000   |
+--------+------------+
3 rows in set (0.00 sec)

mysql> select deptno,min(salary) from employee group by deptno;
+--------+-------------+
| deptno | max(salary) |
+--------+-------------+
|     10 |    12000    |
|     20 |    13000    |
|     30 |    14000    |
+--------+-------------+
3 rows in set (0.00 sec)

The above example tells that, when group by clause operation is performed on a column name like deptno and salary, then it automatically displays the entire details of employees based on maximum salary of employee.

Having Clause

shape Description

To specify a condition with group by clause use Having clause.

shape Syntax

Select <column_name>,group <function1>,group <function2> from <table_name> group by column name having <condition>;

table_name => Any accurate table.

condition =>condition is a logic to get a specific record.

shape Examples

By viewing the below example, the concept of having clause can be easily understood.

mysql> select * from employee;
+--------+-------+-------+--------+
| emp_id | ename | salary| deptno |
+--------+-------+-------+--------+
|   1001 | mike  | 12000 |     10 |
|   1002 | rambo | 13000 |     20 |
|   1003 | kate  | 14000 |     10 |
|   1003 | jeo   | 14000 |     20 |
|   1003 | finn  | 14000 |     30 |
+--------+-------+-------+--------+
5 rows in set (0.00 sec)

mysql> select deptno,max(salary) from employee group by deptno having max(salary)>13000;
+--------+------------+
| deptno | max(salary)|
+--------+------------+
|     10 |    14000   |
|     20 |    14000   |
|     30 |    14000   |
+--------+------------+
3 rows in set (0.00 sec)

mysql> select deptno,max(salary) from employee group by deptno having min(salary)<13000;
+--------+------------+
| deptno | min(salary)|
+--------+------------+
|     10 |    12000   |
+--------+------------+
3 rows in set (0.00 sec)

The above example tells that, when having clause operation is performed on a column name like deptno and salary having salary greater that 13000, then it automatically displays the details of employees based on maximum salary.

Distinct Clause

shape "Description"

To eliminate duplicate values in a column use Distinct clause.

shape Syntax

Select DISTINCT <column_ name> from <table_name>;

Table_name => Any accurate table.

column_name => The condition that one can perform on a column by using distinct clause.

shape Examples

By viewing the below example, the concept of distinct clause can be easily understood.

mysql> select * from employee;
+--------+-------+-------+--------+
| emp_id | ename | salary| deptno |
+--------+-------+-------+--------+
|   1001 | mike  | 12000 |     10 |
|   1002 | rambo | 13000 |     20 |
|   1003 | kate  | 14000 |     10 |
|   1003 | jeo   | 14000 |     20 |
|   1003 | finn  | 14000 |     30 |
+--------+-------+-------+--------+
5 rows in set (0.00 sec)

mysql> select distinct deptno from employee;
+--------+
| deptno |
+--------+
|     10 |
|     20 |
|     30 |
+--------+
3 rows in set (0.00 sec)

The above example tells that, when distinct clause operation is performed on a column deptno, then it displays the values of jeo, finn, and kate as they have the maximum salary and automatically remove duplicate values of mike and rambo as they having minimum salary. 

Summary

shape Key Points

  • Clause is set of rule, that make one to understand the concepts of MySQL commands in the Database.
  • Where clause is used to specify condition in select statement.
  • Order by clause is utilized to arrange the table column in ascending or descending order.
  • Group by clause is used to divide the data of a table into groups.
  • Having clause is used to put condition in group by clause.
  • Distant clause is used to eliminate duplicate values.