# SQL result ranking and HAVING

Sort results and SQL HAVING

SQL Sort Results

SELECT default data out arrangement without, of course, may look like sometimes arranged in a certain order. However, it was only by chance.

If we want to come out of the results of SELECT is arranged in a certain order, then you need to use the ORDER BYcommand. The format of this instruction is used ORDER BY <排序键> <排序规则>.

Among them, the collation is not necessary, if you do not specify a collation, the default is ASCascending, descending, then if you want, you should use DESC.

There are talked about before the NULL value can not be used for comparison, since his return value is an uncertain value. As for the ORDER BYterms, the line has a NULL value will focus appear at the beginning or the end. Some DBMS can choose the location in which they appear.

ORDER BYThe last step before the implementation of the returned results appear. At the same time, even if the sentence had not occurred SELECT columns can be used as sort key.

It allows multiple sort key, and can specify a plurality of different collation of sort keys.

HAVING

WHERE is the role of data limitations, but their roles are quite different.

WHERE used to filter the data recording, while HAVING used to filter GROUP BYsub-data table generated after. However, sometimes we can see the HAVING or WHERE results and there is no difference, you can get the data they want.

However, preference WHERE, because he was faster in general.

HAVING while using the function keys allow polymerization and the constant, and aggregation functions GROUP BYpolymerisable used.

to sum up

Now we row over grammar order:

SELECT --> FROM --> WHERE --> GROUP BY --> HAVING --> ORDER BY

DBMS execution order is:

FROM --> WHERE --> GROUP BY --> HAVING --> SELECT --> ORDER BY

We can send the order to the syntax and order of execution or have a big difference.

While mentioned previously GROUP BYcan not be used in the SELECT clause aliases defined, it can be seen from this reason. Because GROUP BYof the executed before the SELECT clause. Then ORDER BYyou can use an alias defined.

Meanwhile HAVING polymeric bond and bond polymeric WHERE have the same restrictions. As long as we understand HAVING polymerization key is to filter the child table of data, the choice should be easy aggregation bond understand.

Guess you like

Origin www.cnblogs.com/freesfu/p/11440269.html