Welcome to plsql4all.blogspot.com SQL, MYSQL, ORACLE, TERADATA, MONGODB, MARIADB, GREENPLUM, DB2, POSTGRESQL.

Friday 5 April 2024

Common clauses used with SELECT query in SQL

When using the SELECT query in SQL, several clauses can be added to specify additional conditions, filters, sorting, and more. Here are some common clauses used with the SELECT query in SQL:- 


1. SELECT:-  The SELECT clause is used to specify the columns that you want to retrieve from the database table.


2. FROM:-  The FROM clause specifies the table or tables from which you want to retrieve data. It is required in every SELECT statement.


3. WHERE:-  The WHERE clause is used to specify conditions that filter the rows returned by the SELECT statement. Only rows that satisfy the specified conditions are included in the result set.


4. GROUP BY:-  The GROUP BY clause is used to group the rows returned by the SELECT statement into summary rows based on one or more columns. It is often used with aggregate functions like SUM, AVG, COUNT, etc.


5. HAVING:-  The HAVING clause is used to filter the groups produced by the GROUP BY clause. It specifies conditions for group rows in much the same way that the WHERE clause specifies conditions for individual rows.


6. ORDER BY:-  The ORDER BY clause is used to sort the rows returned by the SELECT statement in ascending or descending order based on one or more columns.


7. DISTINCT:-  The DISTINCT keyword is used to return only unique values in the result set. It eliminates duplicate rows from the result.


8. LIMIT/OFFSET (or TOP):-  These clauses are used to limit the number of rows returned by the SELECT statement. LIMIT is commonly used in databases like MySQL and PostgreSQL, while OFFSET is used to skip a specified number of rows before returning the result set. In databases like SQL Server, the TOP clause is used to limit the number of rows returned.


9. FETCH:-  The FETCH clause is used to retrieve a portion of the result set. It is often used in combination with the OFFSET clause for pagination purposes.


These clauses can be combined in various ways to construct complex SELECT queries that retrieve, filter, aggregate, and sort data according to specific requirements.

No comments:

Post a Comment

Please provide your feedback in the comments section above. Please don't forget to follow.