What is SQL Clause?

SQL Clause:

A clause is an in-built function in SQL. SQL Clause helps us filter and analyze data quickly. Clause receives a conditional expression that can be a column name involving columns that supports the MySQL functions to calculate the result values for a table in the database.

Types of SQL Clause:

There are five types of SQL Clause and these are:
i. WHERE Clause
ii. ORDER BY Clause
iii. GROUP BY Clause
iv. HAVING Clause
v. TOP Clause

WHERE Clause:

We want to see only a certain number of rows that contain specific values. For this, we would need to place the WHERE clause along with the SELECT statement. It has the following syntax:

SELECT <column-name(s)> FROM <table-name> WHERE <column-name><operator><value>;

ORDER BY Clause:

In general, the rows displayed from a query don’t have any specific order either in order of ascending or descending. But if you want them to be shown in ascending or descending order in a particular field, then you can control this order even for the selected rows. This is done by adding the clause ORDER B to the SELECT command. It has the following syntax:

SELECT <column-name(s)> FROM <table-name> WHERE <condition> ORDER BY <column to be ordered> [<ASC/DESC>];

GROUP BY Clause:

The GROUP BY Clause allows to form group based on some given conditions. The Group By clause is used with aggregate functions. It has the following syntax:

SELECT <column>, function(<column>) FROM <table> GROUP BY <column>;

HAVING Clause:

The HAVING clause defines criteria used to eliminate certain groups from the output, just as the WHERE clause does for individual rows. It has the following syntax:

SELECT Column FROM Table WHERE condition GROUP BY Column HAVING condition [ORDER BY Column];

TOP Clause:

The TOP clause is used to define the number of record rows to be shown in the result. It has the following syntax:

SELECT Column-Name(s) FROM Table-Name WHERE ROWNUM <= no;