All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

MySQL Clauses

MySQL have different types of clauses which are used in querying a database like WHERE, ORDER BY, GROUP BY, LIMIT, HAVING etc.



WHERE Clause

WHERE clause is used to retrieve particular rows from the database.


$sql = "SELECT * from student_info where student_name = 'Rohit' ";

It retrieve all the rows where student_name is equal to Rohit




ORDER BY Clause

ORDER BY clause is used to specify the sort order for the rows in a result set.


Syntax

ORDER BY columnName ASC/DESC


/*It retrieve and sort all the rows in ascending order*/

$sql = "SELECT * from student_info where ORDER BY 
student_name ";

or

/*It retrieve and sort all the rows in descending order*/
$sql = "SELECT * from student_info where ORDER BY 
student_name DESC ";



GROUP BY Clause

GROUP BY clause is used to eliminate duplicate rows and returns only unique rows.


Syntax

GROUP BY columnName


$sql = "SELECT * from student_info where GROUP BY 
student_name ";

It returns only unique rows




LIMIT Clause

LIMIT clause is used to specify the maximum number of rows that are returned in the result set.


Syntax

LIMIT [offset], NumberOfRows

/*It returns first five rows from student_info table*/
$sql = "SELECT * from student_info LIMIT 5";

or

/*It returns five rows starting from 3rd row */
$sql = "SELECT * from student_info LIMIT 2,5";




HAVING Clause

HAVING clause is used to determine which goups are included in the final results.


$sql = "SELECT * from student_info where GROUP BY 
student_name HAVING course='BCA' ";
❮ PrevNext ❯