What are the 6 clauses in SQL?

To understand SQL and its functionality, it’s essential to know the six clauses that form the backbone of SQL queries: SELECT, FROM, WHERE, GROUP BY, HAVING, and ORDER BY. These clauses allow you to retrieve, filter, group, and sort data efficiently from databases.

What are the Six Clauses in SQL?

SQL, or Structured Query Language, is vital for database management and manipulation. The six primary clauses in SQL queries help structure and refine data retrieval:

  1. SELECT: Specifies the columns to retrieve.
  2. FROM: Indicates the table(s) to query.
  3. WHERE: Filters records based on conditions.
  4. GROUP BY: Groups rows sharing a property.
  5. HAVING: Filters groups based on conditions.
  6. ORDER BY: Sorts the result set.

How Does the SELECT Clause Work?

The SELECT clause is the starting point of any SQL query. It specifies the columns that you want to retrieve from the database. You can select specific columns or use SELECT * to retrieve all columns.

Example:

SELECT first_name, last_name FROM employees;

This query retrieves the first and last names of employees from the employees table.

What is the Purpose of the FROM Clause?

The FROM clause determines which table(s) the query will access. It’s crucial for identifying the data source.

Example:

SELECT * FROM employees;

This query selects all columns from the employees table.

How to Use the WHERE Clause for Filtering?

The WHERE clause is used to filter records based on specified conditions. It allows you to extract only those records that meet certain criteria.

Example:

SELECT * FROM employees WHERE department = 'Sales';

This query retrieves all employees from the Sales department.

What is the GROUP BY Clause?

The GROUP BY clause aggregates data by grouping rows that have the same values in specified columns. It’s often used with aggregate functions like COUNT, SUM, AVG, etc.

Example:

SELECT department, COUNT(*) FROM employees GROUP BY department;

This query counts the number of employees in each department.

How to Apply the HAVING Clause?

The HAVING clause is similar to WHERE but is used for filtering aggregated data. It’s applied after the GROUP BY clause.

Example:

SELECT department, COUNT(*) FROM employees GROUP BY department HAVING COUNT(*) > 10;

This query retrieves departments with more than 10 employees.

How Does the ORDER BY Clause Function?

The ORDER BY clause sorts the result set by one or more columns, either in ascending (ASC) or descending (DESC) order.

Example:

SELECT * FROM employees ORDER BY last_name ASC;

This query sorts employees alphabetically by last name.

Practical Example of SQL Query

To illustrate how these clauses work together, consider the following query:

SELECT department, COUNT(*)
FROM employees
WHERE hire_date > '2020-01-01'
GROUP BY department
HAVING COUNT(*) > 5
ORDER BY department DESC;
  • SELECT: Retrieves department names and counts.
  • FROM: Accesses the employees table.
  • WHERE: Filters employees hired after January 1, 2020.
  • GROUP BY: Groups results by department.
  • HAVING: Filters groups with more than 5 employees.
  • ORDER BY: Sorts results by department in descending order.

People Also Ask

What is the Difference Between WHERE and HAVING Clauses?

The WHERE clause filters rows before any grouping occurs, while the HAVING clause filters groups after aggregation. WHERE is used with individual rows, and HAVING is used with grouped data.

Can You Use Multiple WHERE Conditions?

Yes, you can use multiple conditions in a WHERE clause with logical operators like AND, OR, and NOT to refine your data selection further.

Example:

SELECT * FROM employees WHERE department = 'Sales' AND hire_date > '2020-01-01';

How Do You Sort Data in Descending Order?

To sort data in descending order, use the ORDER BY clause followed by the column name and DESC.

Example:

SELECT * FROM employees ORDER BY salary DESC;

What is the Role of Aggregate Functions in SQL?

Aggregate functions like COUNT, SUM, AVG, MIN, and MAX perform calculations on multiple rows and return a single value. They are often used with the GROUP BY clause to summarize data.

How Can I Retrieve Unique Values in SQL?

To retrieve unique values, use the DISTINCT keyword with the SELECT clause.

Example:

SELECT DISTINCT department FROM employees;

This query retrieves unique department names from the employees table.

Conclusion

Understanding the six essential clauses of SQL—SELECT, FROM, WHERE, GROUP BY, HAVING, and ORDER BY—enables you to construct powerful queries for data manipulation and retrieval. By mastering these clauses, you can efficiently filter, group, and sort data to meet your specific requirements.

For further exploration, consider learning about SQL joins and subqueries, which offer additional ways to handle complex data queries.

Scroll to Top