What is the correct order of clauses for a proper SQL query?
Explanation
Proper SQL query has the following order of clauses:
SELECT fields
FROM tables
WHERE conditions
GROUP BY fields
HAVING conditions on aggregate fields
OREDR BY fields
Theory
  • The HAVING Clause

    The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions.

    SQL HAVING Syntax

    SELECT column_name, aggregate_function(column_name)
    FROM table_name
    WHERE column_name operator value
    GROUP BY column_name
    HAVING aggregate_function(column_name) operator value;
    
  • SQL - ORDER BY Clause

    The SQL ORDER BY clause is used to sort the data in ascending or descending order, based on one or more columns. Some database sorts query results in ascending order by default.

    Syntax:

    The basic syntax of ORDER BY clause is as follows:
    SELECT column-list 
    FROM table_name 
    [WHERE condition] 
    [ORDER BY column1, column2, .. columnN] [ASC | DESC];
    
    You can use more than one column in the ORDER BY clause. Make sure whatever column you are using to sort, that column should be in column-list.
    Read more: SQL - ORDER BY Clause
SQL
SQL Quiz
Start Quiz

or Read more about SQL Quiz

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback