hamburger menu
All Coursesall course arrow
adda247
reward-icon
adda247
    arrow
    arrow
    arrow
    Which of the following MySQL query is syntactically correct and most preferred one?
    Question

    Which of the following MySQL query is syntactically correct and most preferred one?

    A.

    SELECT SECTION, COUNT(*) FROM STUDENT
    ORDER BY SECTION GROUP BY SECTION WHERE MARKS < 33 AND COUNT(*) > 0;

    B.

    SELECT SECTION, COUNT(*) FROM STUDENT
    WHERE MARKS < 33 HAVING COUNT(*) > 0 GROUP BY SECTION ORDER BY SECTION;

    C.

    SELECT SECTION, COUNT(*) FROM STUDENT
    WHERE MARKS < 33 GROUP BY SECTION HAVING COUNT(*) > 0 ORDER BY SECTION;

    D.

    SELECT SECTION, COUNT(*) FROM STUDENT
    GROUP BY SECTION WHERE MARKS < 33 AND COUNT(*) > 0 ORDER BY SECTION;

    Correct option is C


    The correct and most preferred query is:
    SELECT SECTION, COUNT(*) FROM STUDENT WHERE MARKS < 33 GROUP BY SECTION HAVING COUNT(*) > 0 ORDER BY SECTION;
    This query is syntactically correct because:
    1. WHERE clause is used to filter rows before grouping them by SECTION.
    2. GROUP BY is applied after filtering with the WHERE clause to group the results by the SECTION.
    3. The HAVING clause is used to filter the groups based on the result of COUNT(*) after the grouping.
    4. The ORDER BY clause is used to order the final result by SECTION.
    Important Key Points:
    1. WHERE: Filters rows before they are grouped.
    2. GROUP BY: Groups the result set by one or more columns (in this case, SECTION).
    3. HAVING: Filters the grouped results based on aggregate functions (like COUNT(*)).
    4. ORDER BY: Sorts the results based on a specific column (here, by SECTION).
    5. The sequence of WHERE -> GROUP BY -> HAVING -> ORDER BY is the correct and most common way to structure SQL queries.
    Knowledge Booster:
    · Option (a): This query is incorrect because the ORDER BY clause should come after the GROUP BY clause, and the WHERE clause cannot follow GROUP BY. Also, HAVING should be used to filter groups based on aggregate functions, not WHERE.
    · Option (b): This query is incorrect because HAVING COUNT(*) > 0 should come after GROUP BY, not before. The order of the clauses is wrong.
    · Option (d): This query is incorrect because WHERE should come before GROUP BY. The query tries to filter after grouping, which is not the correct sequence. The filtering condition on COUNT(*) should be placed in the HAVING clause, not in the WHERE clause.

    Free Tests

    Free
    Must Attempt

    UPTET Paper 1: PYP Held on 23rd Jan 2022 (Shift 1)

    languageIcon English
    • pdpQsnIcon150 Questions
    • pdpsheetsIcon150 Marks
    • timerIcon150 Mins
    languageIcon English
    Free
    Must Attempt

    UPTET Paper 2 Social Science : PYP Held on 23rd Jan 2022 (Shift 2)

    languageIcon English
    • pdpQsnIcon150 Questions
    • pdpsheetsIcon150 Marks
    • timerIcon150 Mins
    languageIcon English
    Free
    Must Attempt

    UPTET Paper 2 Maths & Science : PYP Held on 23rd Jan 2022 (Shift 2)

    languageIcon English
    • pdpQsnIcon150 Questions
    • pdpsheetsIcon150 Marks
    • timerIcon150 Mins
    languageIcon English
    test-prime-package

    Access ‘EMRS PGT’ Mock Tests with

    • 60000+ Mocks and Previous Year Papers
    • Unlimited Re-Attempts
    • Personalised Report Card
    • 500% Refund on Final Selection
    • Largest Community
    students-icon
    354k+ students have already unlocked exclusive benefits with Test Prime!
    Our Plans
    Monthsup-arrow