Clause in SQL

Learn via video course
FREE
View all courses
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
Topics Covered

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:

ClauseDescription
HAVINGHAVING clause can be used in a GROUP BY clause. It is used to specify a search condition for a group in the database tables.
WHEREThe 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 BYThe ORDER BY clause in SQL is used for sorting the records of the database tables.
GROUP BYTo 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.
TOPThis clause is used when the database has many records. It is used to specify the total number of records to be fetched or returned.
WITHWITH 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.
LIKEThe SQL LIKE clause compares a value to similar values using wildcard operators, i.e. per cent sign ( % ) and the underscore operator ( _ ).
FROMThe FROM clause in SQL is used to select the database tables, which are manipulated using the SELECT, DELETE, and UPDATE statements.
LIMITThe 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.
ANDThe 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.
ORThe 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_idstu_namestu_feesstu_subjectstu_agestu_class
1Divyesha Patil3000Maths1610
2Mayra Pandit2000Social Science1510
3Kunal Purohit4500Chemistry1711
4Manvi Tyagi2000Social Science169
5Joy Yadav3000Maths169
6Tisha Shah2500Science159
7Surbhi Soni4000Chemistry1710

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_idstu_namestu_feesstu_subjectstu_agestu_class
1Divyesha Patil3000Maths1610
2Mayra Pandit2000Social Science1510
4Manvi Tyagi2000Social Science169
5Joy Yadav3000Maths169
6Tisha Shah2500Science159

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_feesstu_class
900010
450011
75009

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_idstu_namestu_feesstu_subjectstu_agestu_class
2Mayra Pandit2000Social Science1610
4Manvi Tyagi2000Social Science169
6Tisha Shah2500Science159
1Divyesha Patil3000Maths1610
5Joy Yadav3000Maths169
7Surbhi Soni4000Chemistry1710
3Kunal Purohit4500Chemistry1711

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_idstu_namestu_feesstu_subjectstu_agestu_class
1Divyesha Patil3000Maths1610
2Mayra Pandit2000Social Science159
4Manvi Tyagi2000Social Science169
5Joy Yadav3000Maths1610
6Tisha Shah2500Science159

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.

See Also: