Introduction
Welcome, Ninjas! Do you know how to retrieve data from the tables you create in SQL? What if we want to fetch some of the data, not the whole table? Do you know how to put constraints while retrieving data? Don't worry; We got this covered.
This blog will look into SQL clauses. There will be specific examples for the same. Let us get started!
SQL clauses are like the conditional statements we use in other programming languages. They form specific parts of a SQL statement that perform a specific function and allow us to put constraints on data. The sql clauses can help filter out the data according to the users' needs. The main clauses are SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, INSERT, UPDATE, DELETE, and JOIN. Each clause has a syntax and its own set of rules and options. They can also be used in combination to create complex queries.
Click here to know about Introduction to JQuery
Types of SQL Clauses
The following are the various SQL clauses:
1. Group By
2. Having
3. Order By
4. Where By
5. Top
6. With
7. Like
8. From
9. Limit
10. End
11. Or
GROUP BY
The GROUP BY clause in SQL groups the data based on one or more columns. We can group rows with similar values together, and it is typically used in conjunction with aggregate functions like SUM, COUNT, AVG, MIN, and MAX.
HAVING
The HAVING clause in SQL filters the data after the GROUP BY clause has grouped it. It filters the grouped rows based on a specific condition and is typically used with aggregate functions like SUM, COUNT, AVG, MIN, and MAX.
Order By
The ORDER BY clause in SQL sorts the data based on one or more columns. It arranges the rows in a specific order, either in ascending or descending order.
Where By
The WHERE clause in SQL filters rows from a table based on specified conditions before the data is retrieved.
Top
The TOP clause retrieves a specified number or percentage of rows from a result set, useful for limiting the number of rows returned.
With
The WITH clause, also known as Common Table Expression (CTE), creates a temporary result set that can be referenced within a SQL query to simplify complex queries.
Like
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column, often used with wildcard characters for pattern matching.
From
The FROM clause specifies the table or tables from which data should be retrieved in a SQL query.
Limit
The LIMIT clause, commonly used in databases like MySQL and PostgreSQL, restricts the number of rows returned in a query result.
End
"END" is often used to terminate a block of code in SQL, such as the end of a stored procedure or a conditional statement.
Or
The OR operator is used in a WHERE clause to retrieve rows that meet at least one of multiple specified conditions. It provides flexibility in querying data with multiple criteria.
Also see, Natural Join in SQL