Clauses
MySQL clauses are the foundational components used to construct database queries. They define the structure and constraints of SQL statements, enabling efficient data manipulation and retrieval. Clauses are integral to queries and work in conjunction with other SQL components like keywords and functions.
Common Uses of MySQL Clauses
- SELECT Clause: Retrieves specific data from tables.
- WHERE Clause: Filters records based on specific conditions.
- GROUP BY Clause: Groups rows with the same values in specified columns.
- ORDER BY Clause: Sorts query results in ascending or descending order.
- LIMIT Clause: Restricts the number of rows returned by a query.
Examples
- SELECT Clause Example
SELECT name, age FROM employees;
Explanation: Fetches the
name
andage
columns from theemployees
table. - WHERE Clause Example
SELECT * FROM orders WHERE status = 'Pending';
Explanation: Retrieves all orders where the
status
isPending
. - GROUP BY Clause Example
SELECT department, COUNT(*) FROM employees GROUP BY department;
Explanation: Counts the number of employees in each department.
- ORDER BY Clause Example
SELECT name FROM students ORDER BY grade DESC;
Explanation: Lists student names sorted by their grades in descending order.
- LIMIT Clause Example
SELECT * FROM products LIMIT 5;
Explanation: Retrieves the first 5 rows from the
products
table.