I have two tables, like that:
users(id, name)
phones(user_id, number)
I'd like to select all user's names that are in more than three rows in the table phones. How can I do that?
Join the tables and add a having clause that limits the results returned by the count of the user_ids
select name,
count(user_id)
from users u
join phones p
on u.id = p.user_id
group by name
having count(user_id) > 3
SQL Fiddle: http://sqlfiddle.com/#!2/c5516/2
select name from user
join phones on id = user_id
Group By user_id
Having Count(number) > 3
Related
I am trying to get a list of users that only exist in a single group and no other groups. The users can be in multiple groups.
The group I want to get users in has an ID of 20064212 and belongs to the acc_id 200640
There are three tables involved:
Users
USER_PK_ID,
ACC_ID,
Active,
Deleted
USER_GROUP_USER
GROUP_ID, USER_PK_ID
USER_GROUP
ID,
ACC_ID,
DESCRIPTION,
ACTIVE
I can get all the users in the group with the fallowing query but it does not exclude users that exist in other groups also.
SELECT DISTINCT (U.USER_PK_ID)
FROM USER_GROUP_USER U
JOIN USERS US ON US.USER_PK_ID = U.USER_PK_ID
WHERE GROUP_ID = 20064212
AND US.acc_id = 200640
AND US.DELETED = 'N'
I have tried various queries but they always seem to return users that also exist in other groups
SELECT DISTINCT (U.USER_PK_ID)
FROM USER_GROUP_USER U
JOIN USERS US ON US.USER_PK_ID = U.USER_PK_ID
WHERE GROUP_ID = 20064212
AND US.acc_id = 200640
AND US.DELETED = 'N'
AND GROUP_ID NOT IN (
SELECT ID
FROM USER_GROUP
WHERE acc_id = 200640
AND ID != 20064212)
Hoping I parsed your text correctly; this SQL returns users with ACC_ID= 200640 and DELETE='N' and that are member of group with ID 20064212, but not member of any other group.
select u.user_pk_id
from users u
join user_group_user ugu on (u.user_pk_id = ugu.user_pk_id)
join user_group ug on ugu.group_id=ug.id
where ugu.group_id=20064212
and u.acc_id=200640
and u.deleted='N'
and not exists (
select null
from user_group_user ugu2
where ugu2.group_id != ugu.group_id
and ugu.user_pk_id = ugu2.user_pk_id
);
I have 3 tables
User Table
id
Name
1
Mike
2
Sam
Score Table
id
UserId
CourseId
Score
1
1
1
5
2
1
1
10
3
1
2
5
Course Table
id
Name
1
Course 1
2
Course 2
What I'm trying to return is rows for each user to display user id and user name along with the sum of the maximum score per course for that user
In the example tables the output I'd like to see is
Result
User_Id
User_Name
Total_Score
1
Mike
15
2
Sam
0
The SQL I've tried so far is:
select TOP(3) u.Id as User_Id, u.UserName as User_Name, SUM(maxScores) as Total_Score
from Users as u,
(select MAX(s.Score) as maxScores
from Scores as s
inner join Courses as c
on s.CourseId = c.Id
group by s.UserId, c.Id
) x
group by u.Id, u.UserName
I want to use a having clause to link the Users to Scores after the group by in the sub query but I get a exception saying:
The multi-part identifier "u.Id" could not be bound
It works if I hard code a user id in the having clause I want to add but it needs to be dynamic and I'm stuck on how to do this
What would be the correct way to structure the query?
You were close, you just needed to return s.UserId from the sub-query and correctly join the sub-query to your Users table (I've joined in reverse order to you because to me its more logical to start with the base data and then join on more details as required). Taking note of the scope of aliases i.e. aliases inside your sub-query are not available in your outer query.
select u.Id as [User_Id], u.UserName as [User_Name]
, sum(maxScore) as Total_Score
from (
select s.UserId, max(s.Score) as maxScore
from Scores as s
inner join Courses as c on s.CourseId = c.Id
group by s.UserId, c.Id
) as x
inner join Users as u on u.Id = x.UserId
group by u.Id, u.UserName;
So I have two tables: Please see the ER diagram here
I want to use SELECT to create one table with "name" from the USER table, "id" as the foreign key for the two tables, and the count of friend_id as the number of friends each user has.
Here is my code:
SELECT name, id, (SELECT count(friend_id) as number
FROM friend
GROUP BY user_id)
FROM user
ORDER BY number DESC
I'm wondering what's the problem with these lines. Thank you!
You can use a subquery to calculate the count.
SELECT name, id, COALESCE(f.Count, 0) AS friend_count
FROM user u
LEFT JOIN (
SELECT user_id, COUNT(DISTINCT friend_id) AS Count
FROM friend
GROUP BY user_id
) f ON f.user_id = u.id
ORDER BY friend_count DESC
I used a LEFT JOIN so that if a user doesn't have a row in friend, it will still return a row with a friend count of 0 (thanks to COALESCE). I also added a DISTINCT so that if the friend has duplicates the friend is counted only one, might not be necessary especially if you have a UNIQUE INDEX setup on columns user_id, friend_id
Just add where to find only one id and remove group by because you have only one id for one or more friends as your diagram says.
SELECT name, id, (SELECT count(friend_id) as number
FROM friend
WHERE user_id = user.id)
FROM user
ORDER BY number DESC
I think this will be correct for you puprose
CREATE TABLE #user(
id VARCHAR(22),
[name] VARCHAR(255),
)
CREATE TABLE #friend(
user_id VARCHAR(22),
friend_id VARCHAR(22)
)
SELECT name, id, (SELECT COALESCE(COUNT(friend_id), 0)
FROM #friend f
WHERE f.user_id = u.id
GROUP BY user_id) as number
FROM #user u
ORDER BY number DESC
--Same query with join:
SELECT u.[name], u.id, COALESCE(COUNT(f.friend_id),0) number
FROM #user u
LEFT JOIN #friend f ON f.user_id = u.id
GROUP BY u.[name], u.id
ORDER BY number
DB is H2(in-memory).
There are two tables:
Users with id, name, surname. And Documents with id, title, text, user_id.
user_id is a foreign key from Users id.
The task is:
Select a unique name from Users that have more than 5 titles.
I created this select, but it gives an error:
SELECT DISTINCT users.name, documents.user_id,
( SELECT COUNT(*)
FROM documents AS d
WHERE d.user_id = documents.user_id
)
AS rn
FROM documents, users WHERE users.id = documents.user_id
GROUP BY documents.user_id AND users.name having rn > 5 ORDER BY documents.user_id, users.name, rn;
Error: [22018][22018] Data conversion error converting "Douglas"; SQL statement: SELECT DISTINCT users.name, documents.user_id, ( SELECT DISTINCT COUNT(*) FROM documents AS d WHERE d.user_id = documents.user_id ...
(Douglas is a first row name from a table)
Help me to resolve this problem, and find a mistake.
GROUP BY documents.user_id AND users.name
SQL is trying to resolve the boolean expression documents.user_id AND users.name and cannot reconcile what integer and string is supposed to be.
Separate multiple columns in a group by with a comma.
group by documents.user_id, users.name
I got 3 tables, Users, courses and course realation tables. I want to get users who aren't on specific course. So I figure I need somehow merge 2 selects with right join. How could I make one select from 2 selects?
SELECT ID, NAME, LASTNAME, ROLE FROM COURSERELATION JOIN USERS ON
ID_USER = ID WHERE ID_COURSE = ?
RIGTH JOIN
SELECT ID, NAME, LASTNAME, ROLE from COURSERELATION JOIN USERS ON
ID_USER = ID WHERE ID_COURSE != ?
You need to extract users for which it doesn't exist a record of that user for the specific course. You can filter the rows using a NOT EXISTS clause over a subquery.
Please try below query:
SELECT u.ID,
u.NAME,
u.LASTNAME,
u.ROLE
FROM USERS u
WHERE NOT EXISTS (SELECT 1
FROM COURSERELATION s
WHERE s.id_user = u.id
AND s.id_course = 'YOUR_COURSE_ID_HERE' )