Clause in SQL
Overview
As you know, while managing and storing a large amount of data in a database, SQL is frequently used. SQL clauses are used to carry out activities like adding, removing, and retrieving data from the database table. SQL clause or SQL statements are builtin functions available in SQL. Managing data becomes easy with the help of clauses, and developers can filter and analyze the data very easily. In this article, you will learn what clauses in SQL are and their types.
What is Clause in SQL?
SQL (Structured Query Language) is a query language that is used to query the given data, and the desired or required data is returned from the database. SQL is widely used for multiple operations that are related to the data, and to achieve that, there are various methods or processes available in SQL. To deal with a large amount of data, various clauses are available in SQL, which you'll study in this topic.
A clause in SQL is a built-in function that helps to fetch the required records from a database table. A clause receives a conditional expression, i.e. a column name or some terms involving the columns. The clause calculates the result based on the given statements in the expression. When a large amount of data is stored in the database, clauses are helpful to filter and analyze the queries. There are different types of clauses in SQL that are available for fetching the desired data, and these are mentioned below:
What are the Types of Clauses in SQL?
There are various types of clauses available in SQL, and some of them are listed below:
Clause | Description |
---|---|
HAVING | HAVING clause can be used in a GROUP BY clause. It is used to specify a search condition for a group in the database tables. |
WHERE | The WHERE clause in SQL is used to retrieve the specific data from the database that specifies the conditions exactly that are given in the UPDATE, DELETE, etc. statements. |
ORDER BY | The ORDER BY clause in SQL is used for sorting the records of the database tables. |
GROUP BY | To group the result set of the rows that have the same values in the result set from the database tables, the GROUP BY clause is used. |
TOP | This clause is used when the database has many records. It is used to specify the total number of records to be fetched or returned. |
WITH | WITH clause acts as a temporary view as it is available only during the execution of SELECT, UPDATE, INSERT, DELETE, etc. statements. It is used to simplify complex and long queries. |
LIKE | The SQL LIKE clause compares a value to similar values using wildcard operators, i.e. per cent sign ( % ) and the underscore operator ( _ ). |
FROM | The FROM clause in SQL is used to select the database tables, which are manipulated using the SELECT, DELETE, and UPDATE statements. |
LIMIT | The LIMIT clause is used when you are dealing with large databases. It is used to specify the maximum number of rows to be retrieved from the table. |
AND | The AND clause is used when multiple conditions are specified in a query and returns a dataset when all the conditions given in the AND clause meet the requirements. |
OR | The OR clause is used when multiple conditions are specified in a query and returns a dataset when one of those conditions gets satisfied. |
What are the Uses of SQL Clause?
There are various uses of clauses in SQL based on the type of clause. The uses of some of the clauses in SQL are mentioned below:
ORDER BY
- To sort the database records, you can use the ORDER BY clause in SQL. This clause in SQL is used to arrange the fetched data in ascending or descending order based on the requirements.
WHERE
- The WHERE clause in SQL is used to fetch the data or certain records that match the specified condition in the SELECT statement. SQL's WHERE clause is also used with the DELETE, UPDATE, etc. statements.
GROUP BY
- Another usage of the clause in SQL is to group the rows that have the same values in the result set, and this can be achieved by using the GROUP BY clause in SQL.
TOP
- If you want to determine the total number of record rows in the result then you can use the TOP clause in SQL.
AND
- The AND clause is used with the UPDATE and DELETE statements and returns the resultant dataset only when all the conditions given with the AND clause are satisfied.
OR
- The OR clause is also used with the UPDATE and DELETE statements and returns the resultant dataset when one or more than one condition is satisfied.
LIMIT
- When the amount of data in the database is very large, the LIMIT clause is used to restrict the number of rows from the database records.
Examples of Clause in SQL
Consider the below Students table, which is used as a reference for all the examples that are mentioned below.
Students
stu_id | stu_name | stu_fees | stu_subject | stu_age | stu_class |
---|---|---|---|---|---|
1 | Divyesha Patil | 3000 | Maths | 16 | 10 |
2 | Mayra Pandit | 2000 | Social Science | 15 | 10 |
3 | Kunal Purohit | 4500 | Chemistry | 17 | 11 |
4 | Manvi Tyagi | 2000 | Social Science | 16 | 9 |
5 | Joy Yadav | 3000 | Maths | 16 | 9 |
6 | Tisha Shah | 2500 | Science | 15 | 9 |
7 | Surbhi Soni | 4000 | Chemistry | 17 | 10 |
Example:
In this example, the below SQL query is used along with the WHERE clause in SQL to retrieve all the records of a student from the Students table whose fees is less than 3500.
Output:
stu_id | stu_name | stu_fees | stu_subject | stu_age | stu_class |
---|---|---|---|---|---|
1 | Divyesha Patil | 3000 | Maths | 16 | 10 |
2 | Mayra Pandit | 2000 | Social Science | 15 | 10 |
4 | Manvi Tyagi | 2000 | Social Science | 16 | 9 |
5 | Joy Yadav | 3000 | Maths | 16 | 9 |
6 | Tisha Shah | 2500 | Science | 15 | 9 |
As shown in the output, the WHERE clause in SQL fetches the records of those students whose fee is less than 3500.
Example: The following query uses the GROUP BY clause to fetch the total fees in the students' individual classes. This can be easily done by grouping of the rows from the Students table.
Output:
stu_fees | stu_class |
---|---|
9000 | 10 |
4500 | 11 |
7500 | 9 |
As shown in the output above, the GROUP BY clause is used to group the rows of the students based on the student class column. The total fees in an individual class are summed up, and the grouped rows are displayed in the table.
Example: Let's take another example which includes the ORDER BY clause in SQL. The below query is used to order the students based on the fees of the individual students.
Output:
stu_id | stu_name | stu_fees | stu_subject | stu_age | stu_class |
---|---|---|---|---|---|
2 | Mayra Pandit | 2000 | Social Science | 16 | 10 |
4 | Manvi Tyagi | 2000 | Social Science | 16 | 9 |
6 | Tisha Shah | 2500 | Science | 15 | 9 |
1 | Divyesha Patil | 3000 | Maths | 16 | 10 |
5 | Joy Yadav | 3000 | Maths | 16 | 9 |
7 | Surbhi Soni | 4000 | Chemistry | 17 | 10 |
3 | Kunal Purohit | 4500 | Chemistry | 17 | 11 |
In the above example, the ORDER BY clause is applied to the column stu_fees to sort the final result based on the fees of the students.
Example:
Consider another example which explains the HAVING clause in SQL. The following query returns the details of all the students having an age less than 17 after grouping the records based on stu_id.
Output:
stu_id | stu_name | stu_fees | stu_subject | stu_age | stu_class |
---|---|---|---|---|---|
1 | Divyesha Patil | 3000 | Maths | 16 | 10 |
2 | Mayra Pandit | 2000 | Social Science | 15 | 9 |
4 | Manvi Tyagi | 2000 | Social Science | 16 | 9 |
5 | Joy Yadav | 3000 | Maths | 16 | 10 |
6 | Tisha Shah | 2500 | Science | 15 | 9 |
In the above output, you can see that the HAVING clause is used to fetch the records of students under 17. Also, note that the GROUP BY clause is mandatory if you are using the HAVING clause in SQL.
Conclusion
- In this article, you learned what are the clauses in SQL and how they are used to fetch the required data from the database tables.
- Clause in SQL is a built-in function that is used to retrieve the data from the records present in the database.
- Different clauses in SQL are used to fetch or retrieve the records from the database table.
- The ORDER BY clause in SQL is used to arrange the retrieved results in ascending order or descending order.
- You can use the GROUP BY clause in SQL to group the rows that have the same result set from the database result.