Order highest to lowest value without using MAX and ORDER BY - sql

I wanted to get the maximum count of a column for which I used Count() and wanted to order the column highest to lowest value without using max or order by desc.
I tried
SELECT COUNT(subject) AS CNT, student
FROM AUTHOR
GROUP BY student AU1
WHERE not exists (SELECT * FROM AUTHOR AU2
WHERE AU2.student <> AU1.student AND AU2.subject > AU1.CNT)
but it doesn't return the desired output.
The desired output is the same as
SELECT COUNT(subject) AS CNT, student
FROM AUTHOR
GROUP BY Student
ORDER BY CNT DESC
but, without the order by desc or MAX.

You can use ORDER BY ASC:
SELECT COUNT(subject) AS CNT, student
FROM AUTHOR
GROUP BY Student
ORDER BY (- CNT) ASC;
If you want the results in a particular order, you need to use ORDER BY. That is one of the rules of using SQL.

OK, so DESC is not allowed and MAX is not allowed.
SELECT
COUNT(subject) AS CNT, student
FROM
AUTHOR
GROUP BY
Student
ORDER BY
-CNT
This does not work in each database software.
An other version:
SELECT * FROM (
SELECT
COUNT(subject) AS CNT, student
FROM
AUTHOR
GROUP BY
Student
) t
ORDER BY
-CNT

Related

SQL Query using count(*)

I am wanting to list names and the number of times they have done a certain action. I then want to order the names by the most amount of times.
I have the below code so far but I keep getting errors:
select name, count(*) as NoOfTimes
from CustName
group by count(*);
order by count(*) asc;
I should note that if you want the most times at the beginning of the result set, the you want a descending sort:
select name, count(*) as NoOfTimes
from CustName
group by name
order by count(*) desc;
In order to show count by name, you must group by name
select name, count(*) as NoOfTimes
from CustName
group by name
order by NoOfTimes desc
Order by index also a good idea:
select name, count(*) as NoOfTimes
from CustName
group by name
order by 2 DESC

Find the month in which maximum number of employees hired

I have a situation where I need to find the month in which maximum number of employees hired.
Here is my Employee table:
Although I have a solution for this:
select MM
from (
select *, dense_RANK() OVER(order by cnt desc) as rnk
from (
select month(doj) as MM,count(month(doj)) as CNT
from employee
group by month(doj)
)x
)y
where rnk=1
But I am not satisfied with what i have implemented and want the most feasible solution for it.
I think the simplest way is:
select top 1 year(doj), month(doj), count(*)
from employee
group by year(doj), month(doj)
order by count(*) desc;
Notes:
This interprets "month" as being "year/month". If you really do only want the month, then remove year() from both the select and group by.
This returns one row. If you want multiple rows when there are ties, then use select top (1) with ties.

Selecting type(s) of account with 2nd maximum number of accounts

Suppose we have an accounts table along with the already given values
I want to find the type of account with second highest number of accounts. In this case, result should be 'FD'. In case their is a contention for second highest count I need all those types in the result.
I'm not getting any idea of how to do it. I've found numerous posts for finding second highest values, say salary, in a table. But not for second highest COUNT.
This can be done using cte's. Get the counts for each type as the first step. Then use dense_rank (to get multiple rows with same counts in case of ties) to get the rank of rows by type based on counts. Finally, select the second ranked row.
with counts as (
select type, count(*) cnt
from yourtable
group by type)
, ranks as (
select type, dense_rank() over(order by cnt desc) rnk
from counts)
select type
from ranks
where rnk = 2;
One option is to use row_number() (or dense_rank(), depending on what "second" means when there are ties):
select a.*
from (select a.type, count(*) as cnt,
row_number() over (order by count(*) desc) as seqnum
from accounta a
group by a.type
) a
where seqnum = 2;
In Oracle 12c+, you can use offset/fetch:
select a.type, count(*) as cnt
from accounta a
group by a.type
order by count(*) desc
offset 1
fetch first 1 row only

Get mixed in sql query

I've got a table in postgresql include in countries and bird species column. I want to get what country has the most number of bird species.How can I do that? any suggestion?
You can use dense_rank to get all the countries with the highest number of species.
select country from
(
select country, dense_rank() over(order by count(*) desc) as rnk
from yourtable
) t
where rnk = 1
A typical way to solve this is using group by and limit/fetch first 1 row only:
select country, count(*) as cnt
from t
group by country
order by count(*) desc
limit 1;
Note: in case multiple countries are tied, then this will only return one of them.

Need to change LIMIT into something else

Is there a way to change "LIMIT 1" and get the same output? I have to get client's name, surname and a quantity of books that has the most books
SELECT stud.skaitytojas.name, stud.skaitytojas.surname,
COUNT (stud.skaitytojas.nr) AS quantity
FROM stud.egzempliorius , stud.skaitytojas
WHERE stud.egzempliorius.client = stud.skaitytojas.nr
GROUP BY stud.skaitytojas.nr
ORDER BY quantity DESC
LIMIT 1
Postgres supports the ANSI standard FETCH FIRST 1 ROW ONLY, so you can do:
SELECT s.name, s.surname, COUNT(s.nr) AS quantity
FROM stud.egzempliorius e JOIN
stud.skaitytojas s
ON e.client = s.nr
GROUP BY s.name, s.surname
ORDER BY quantity DESC
FETCH FIRST 1 ROW ONLY;
Also notice the use of table aliases and proper JOIN syntax. I also prefer to list the columns in the SELECT in the GROUP BY, although that is optional if s.nr is unique.
You can select the row with the highest quantity using row_number()
SELECT * FROM (
SELECT * , row_number() over (order by quantity desc) rn FROM (
SELECT stud.skaitytojas.name, stud.skaitytojas.surname, COUNT (stud.skaitytojas.nr) AS quantity
FROM stud.egzempliorius , stud.skaitytojas
WHERE stud.egzempliorius.client = stud.skaitytojas.nr
GROUP BY stud.skaitytojas.name, stud.skaitytojas.surname
) t
) t where rn = 1
If you want to include ties for the highest quantity, then use rank() instead.