Obtaining the most reoccurring attribute in SQL - sql

Using SQL, I have a table with a list of usernames and I am trying to output the most repeated one with out using MAX. I am very new to SQL so any help would be much appreciated!
Thanks

You can use the aggregate function count() to get the total number of times a username is repeated:
select username, count(username) Total
from yourtable
group by username
order by total desc
Then depending on your database you can return the username that appears the most.
In MySQL, you can use LIMIT:
select username, count(username) Total
from yourtable
group by username
order by total desc
limit 1;
See SQL Fiddle with Demo
In SQL Server, you can use TOP:
select TOP 1 with Ties username, count(username) Total
from yourtable
group by username
order by total desc
See SQL Fiddle with Demo

Related

SQL statement to get the MIN() from the AVG() returned from second query

I got a question regarding subquery in SQL statement. What I am trying to do is to find a minimum time with the average column result returned from another query.
SELECT userID
FROM myTable
WHERE time = MIN(...)
SELECT userID, AVG(date_time)
FROM myTable
GROUP BY userID
The second query will return me the average between two times and group by a third party.
Then my first query need to find the minimum average time return from my second query. How can I combine both of the queries together?
The sample data for my second query is like:
user1 20
user2 45
user3 10
Then for my first query, I need to get the user with minimum average:
user3 10
Thanks in advance.
If you want one row with the minimum average time, then you can do:
SELECT TOP 1 userID
FROM myTable
GROUP BY userID
ORDER BY AVG(date_time) ASC;
If you want multiple rows then use TOP WITH TIES:
SELECT TOP (1) WITH TIES userID
FROM myTable
GROUP BY userID
ORDER BY AVG(date_time) ASC;
Try this query:
SELECT TOP 1 userID
FROM myTable
GROUP BY userID
ORDER BY AVG(date_time) ASC

Select statement with count

I want to count the number of ABC group using id.
SELECT group, count(id) as total FROM `user` WHERE group=`ABC`;
What's wrong?
Many thanks.
Include the columns in the select list in group by clause when using aggregate functions.
SELECT group, count(id) as total FROM user
WHERE group=`ABC`
GROUP BY group
Else simply get the count with out using other columns in the select statement.
SELECT count(id) as total FROM user
WHERE group=`ABC`
Try this:
SELECT group, count(id) as total FROM `user`
group by group having group like 'ABC';
If you want to get COUNT of users, who has the "group" field = "ABC"
SELECT count(id) as total FROM user WHERE group='ABC';
Also, it's better to avoid using SQL keywords in column names (GROUP is an SQL keyword)

Sql select the winner

having this database, bold = PK
CERTIFICATE(USERID, CERTIFICATENAME)
i need to find the userid with the maximum number of certificates with a SQL query.
sample data:
USERID, CERTIFICATENAME
1,cert1
1,cert2
1,cert3
2,cert4
2,cert5
3,cert2
4,cert1
with this sample data i need a query for find that user:1 has 3 certificates, this user has the maximum number of certificates.
request result:
USERID, COUNT
1,3
in this case my dbms is oracle, but i'm looking for a generic sql solution to my problem.
Using old plain group by:
select top 1 userid, count(certificatename) total
from certificates
group by userid -- but not certificatename
order by 2 desc --you can use total or count(certificatname) here
Common Table Expressions (CTE) don't add any performance preferences because you need group by in any case.
As a subquery:
SELECT MAX(Total), UserId FROM -- select the max count
( -- create the counts per user
SELECT Count(CertificateName) as Total,
UserId
FROM YourTable
GROUP BY CertificateName, UserId
) GROUP BY Total, UserId

Specific sql query with count of records

I have simple table.
I need to build a SQL query and in result get the count of record where user re-played same game. So in this case we will have 3 in result..
You want a count group by
select user_id, count(*)
from your_table
group by user_id ;
You'll want to group by user_id and ensure the game count is greater than 1.
Assuming you're using SQL Server, please try something like:
SELECT user_id, count(*) as GamesPlayed
FROM table_name
GROUP BY user_id
HAVING count(*) > 1;

Fetch one row per account id from list

I have a table with game scores, allowing multiple rows per account id: scores (id, score, accountid). I want a list of the top 10 scorer ids and their scores.
Can you provide an sql statement to select the top 10 scores, but only one score per account id?
Thanks!
select username, max(score) from usertable group by username order by max(score) desc limit 10;
First limit the selection to the highest score for each account id.
Then take the top ten scores.
SELECT TOP 10 AccountId, Score
FROM Scores s1
WHERE AccountId NOT IN
(SELECT AccountId s2 FROM Scores
WHERE s1.AccountId = s2.AccountId and s1.Score > s2.Score)
ORDER BY Score DESC
Try this:
select top 10 username,
max(score)
from usertable
group by username
order by max(score) desc
PostgreSQL has the DISTINCT ON clause, that works this way:
SELECT DISTINCT ON (accountid) id, score, accountid
FROM scoretable
ORDER BY score DESC
LIMIT 10;
I don't think it's standard SQL though, so expect other databases to do it differently.
SELECT accountid, MAX(score) as top_score
FROM Scores
GROUP BY accountid,
ORDER BY top_score DESC
LIMIT 0, 10
That should work fine in mysql. It's possible you may need to use 'ORDER BY MAX(score) DESC' instead of that order by - I don't have my SQL reference on hand.
I believe that PostgreSQL (at least 8.3) will require that the DISTINCT ON expressions must match initial ORDER BY expressions. I.E. you can't use DISTINCT ON (accountid) when you have ORDER BY score DESC. To fix this, add it into the ORDER BY:
SELECT DISTINCT ON (accountid) *
FROM scoretable
ORDER BY accountid, score DESC
LIMIT 10;
Using this method allows you to select all the columns in a table. It will only return 1 row per accountid even if there are duplicate 'max' values for score.
This was useful for me, as I was not finding the maximum score (which is easy to do with the max() function) but for the most recent time a score was entered for an accountid.