Skip to main content
Documents
Share
LinkedIn
Facebook
Twitter
Copy
ClausesStatementsKeywordsExpressionsFunctions

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

  1. SELECT Clause: Retrieves specific data from tables.
  2. WHERE Clause: Filters records based on specific conditions.
  3. GROUP BY Clause: Groups rows with the same values in specified columns.
  4. ORDER BY Clause: Sorts query results in ascending or descending order.
  5. LIMIT Clause: Restricts the number of rows returned by a query.

Examples

  1. SELECT Clause Example
     
    SELECT name, age FROM employees;

    Explanation: Fetches the name and age columns from the employees table.

  2. WHERE Clause Example
     
    SELECT * FROM orders WHERE status = 'Pending';

    Explanation: Retrieves all orders where the status is Pending.

  3. GROUP BY Clause Example
     
    SELECT department, COUNT(*) FROM employees GROUP BY department;

    Explanation: Counts the number of employees in each department.

  4. ORDER BY Clause Example
     
    SELECT name FROM students ORDER BY grade DESC;

    Explanation: Lists student names sorted by their grades in descending order.

  5. LIMIT Clause Example
     
    SELECT * FROM products LIMIT 5;

    Explanation: Retrieves the first 5 rows from the products table.