MYSQL Tutorial Part 6

 MYSQL CLAUSE

    In this part, will discuss the MySQL clause. Will learn how to filter particular records from the database table.



WHERE CLAUSE:

        The WHERE clause is used to filter records.

        Syntax

            SELECT * FROM table_name WHERE condition;

        Example:
            SELECT * FROM Students WHERE department = 'CSE';

ORDER BY:
  • The ORDER BY keyword is used to sort the result-set in ascending or descending order.
  • The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.
      ORDER BY Syntax
            SELECT* FROM table_name ORDER BY column1, column2 ASC|DESC;

    Example:
        SELECT * FROM Students ORDER BY district;
        SELECT * FROM Students ORDER BY district DESC;
    
LIMIT:
  • The LIMIT clause is used to specify the number of records to return.
  • The LIMIT clause is useful on large tables with thousands of records. Returning a large number of records can impact performance.
        Syntax
                SELECT * FROM table_name WHERE condition LIMIT number;

        Example:
             
SELECT * FROM Students LIMIT 6;

GROUP BY:
  • The GROUP BY statement groups rows that have the same values into summary rows.
  • The GROUP BY statement is often used with aggregate functions (COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result set by one or more columns.
    Syntax

        SELECT column_names FROM table_name WHERE condition GROUP BY column_names;

    Example
        SELECT COUNT(name), department FROM Students GROUP BY department;

HAVING:
  • The HAVING clause was added to SQL because the WHERE keyword cannot be used with aggregate functions.
  • It is used with the GROUP BY clause.
  • It returns the row where the condition is TRUE.
    Syntax

        SELECT column_names FROM table_name WHERE condition GROUP BY column_names HAVING condition ORDER BY column_names;

    Example

        SELECT COUNT(name), department FROM Students GROUP BY department HAVING COUNT(name) > 5;



                                                Thanks for choosing our blog 😊. 
                                    Will discuss Joins in the next part. -  Part-7

Tutorial Topics:
Introduction  -  Part-1
Database Creation  -  Part-2
Aggregate Functions  - Part-3
Conditions  - Part-4
Constraints  - Part-5
Clauses  -  Part-6
Joins  -  Part-7


Tutorial Topics for examples:
Database Creation -  Part-1
Table Modifications  -  Part-2
Clauses   - Part-3
Operators  - Part-4
Joins  - Part-5

Comments

Popular posts from this blog

MySQL Tutorial Part 1

Pandas DataFrame empty

Introduction to Java