I want to write an ORDER BY
clause before my WHERE
condition because I need to truncate my results to just 10, but I need to sort them in alphabetical order first.
I know that you can't put ORDER BY
before WHERE
so how can I do it?
I need to do something like the following:
SELECT *
FROM myTable
ORDER BY TOP10
WHERE ROWNUM <=10
The GROUP BY clause is placed after the WHERE clause. The GROUP BY clause is placed before the ORDER BY clause.
GROUP BY clause is used with the SELECT statement. In the query, GROUP BY clause is placed after the WHERE clause.
The correct answer is Select, where, group by, having.
No, that order doesn't matter (or at least: shouldn't matter). Any decent query optimizer will look at all the parts of the WHERE clause and figure out the most efficient way to satisfy that query.
You can use an inline view for this
SELECT *
FROM (
SELECT *
FROM myTable
ORDER BY TOP10) T
WHERE ROWNUM <=10
SELECT *
FROM (SELECT * FROM myTable RANK() OVER (ORDER BY TOP10) rank)
WHERE rank <= 10;
For more see this
Edited: Thanks Wolf for correction:
select * from (select mt.*, rank() over (order by top10) rank from mytable mt)
WHERE rank <= 10
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With