MySQL – Clauses
MySQL queries are SQL functions that allow us to retrieve a particular set of records from a database table. By using clauses or SQL statements, we can request any information or data from the database. For example, SQL Clauses support calculating the result values of a table in the database based on conditional expressions that can be column names or valid terms involving columns.
There are five general categories of SQL clauses in MySQL Server. Following is a list of them:
- WHERE Clause
- ORDER BY clause
- HAVING Clause
- TOP Clause
- GROUP BY Clause
The SQL SELECT statement is executed in MySQL to select data from a table in the database. The WHERE clause allows for the filtering of records that exactly match a specific condition. Thus, it enables us to retrieve only those records from the database that satisfy the specified expressional conditions from the database. WHERE clauses are used with SELECT statements, as well as UPDATE and DELETE type statements and aggregate functions to limit the number of results. Data to be retrieved from the table. We can also apply logical or comparison operators such as LIKE,<,>,=, etc. Using the WHERE clause to fulfill certain conditions.
ORDER BY Clause
For sorting records in SQL, the ORDER BY clause is applied. You can use it to arrange the results either ascending or descending. An unordered list is returned when we query the database with the SELECT statement. Thus, the ORDER BY clause can be used to sort the result rows when the SELECT statement is combined with the ORDER BY clause.
GROUP BY Clause
By using the GROUP BY clause, you can group rows in the result set that have similar values. As an example, we may wish to find the names of books in the table grouped by CatID.
This clause is actually designed to apply functions to a query when the WHERE clause is used. The HAVING clause was added to SQL in order to allow the application of aggregate functions to the WHERE clause.
The TOP clause determines how many rows of records should be displayed in the result. A TOP clause is used in conjunction with a SELECT statement that is specially designed for large data sets with many records. Some database systems do not support the LIMIT clause. For example, MySQL supports the LIMIT clause for selecting a limited number of rows, whereas Oracle uses ROWNUM.