Dear all I have users table and cars table.
and I have following join query:
select
users.id as user_id,
users.username,
users.job,
cars.id,
cars.brand as car_brand
FROM users
LEFT JOIN cars on users.id = cars.user_id
GROUP BY users.username, users.id, cars.id;
Here is the snapshot:
How to query for users that having cars more than one?
I tried code below but it return empty data:
How to get users that having more than one cars? (username: Ismed)
You can do in this was as well.
select
users.id as user_id,
users.username,
users.job,
cars.id,
cars.brand as car_brand
FROM users
LEFT JOIN cars on users.id = cars.user_id
where exists (select username, count(*) multiplecars
FROM users u
JOIN cars c on u.id = c.user_id
where users.username = u.username
group by
u.username
having count(*) > 1 )
If the users have more than one car (even if same brand then this will bring those records) if you only want users with more than one branded care you can do count(distinct)
The simplest and probably most performance method is to use window functions:
select user_id, username, job, id, brand
from (select u.id as user_id, u.username, u.job,
c.id, c.brand as car_brand,
count(*) over (partition by u.id) as num_cars
from users u join
cars c
on u.id = c.user_id
) uc
where num_cars > 1;
Note that I changed the left join to a join. If you have two matches, you are requiring a match. I also introduced table aliases so the query is easier to write and to read.
SELECT users.username
FROM users
WHERE users.id IN(
select
users.id
FROM users
JOIN cars on users.id = cars.user_id
GROUP BY users.id
HAVING COUNT(*) > 1
);
Filter users first who has more then one car then get corresponding details
Related
I have a user table with id column and request table with user_id and session_id columns.
I want to get the number of sessions each user has.
Here is the query I wrote using a subquery:
select user.id,
(
select count(distinct session_id)
from request
where request.user_id = user.id
) as session_count
from user
What would be the join equivalent of this?
What about this?
select u.id, count(distinct r.session_id)
from user u
join request r on r.user_id = u.id
group by u.id
You can use GROUP BY without need a subquery such as
SELECT u.id, COUNT(DISTINCT r.session_id)
FROM user u
JOIN request r
ON u.id = r.user_id
GROUP BY u.id
I have 4 tables
All ID related things are ints and the rest are texts.
I want to count the number of albums the user is tagged at so if a user is tagged in album1 once album2 once and album3 once it will show 3 and if more in any of them it will still show 3.
I tried to do:
SELECT COUNT(DISTINCT ALBUM_ID) FROM PICTURES WHERE ID=(SELECT PICTURE_ID FROM TAGS WHERE USER_ID=userId);
But this returned 1 although it was supposed to return 3 and the same happened without DISTINCT.
How can I get the amount?
EDIT:
I want to check only one user(I have the user's ID and name)
You must join users with LEFT joins to tags and pictures and aggregate:
SELECT u.id, u.name, COUNT(DISTINCT p.album_id) counter
FROM users u
LEFT JOIN tags t ON t.user_id = u.id
LEFT JOIN pictures p ON p.id = t.picture_id
GROUP BY u.id, u.name
If you want the result for a specific user only:
SELECT u.id, u.name, COUNT(DISTINCT p.album_id) counter
FROM users u
LEFT JOIN tags t ON t.user_id = u.id
LEFT JOIN pictures p ON p.id = t.picture_id
WHERE u.id = ?
GROUP BY u.id, u.name -- you may omit this line, because SQLite allows it
Or with a correlated subquery:
SELECT u.id, u.name,
(
SELECT COUNT(DISTINCT p.album_id)
FROM tags t INNER JOIN pictures p
ON p.id = t.picture_id
WHERE t.user_id = u.id
) counter
FROM users u
WHERE u.id = ?
Replace ? with the id of the user that you want.
Back with another SQL question about joins. I have 3 tables:
user: id, username, name, city, state, private
rides: id, creator, title, datetime, city, state
posts: id, title, user, date, state, city
I need to get the users from the user table, and based on the id of user, get the number of posts and rides for each person. Such as, user with id 25 has 2 rides and 4 posts, while the user with id 27 has 2 rides and 2 posts. The problem I am having, is that both users are coming back with 4 posts and rides each.
user.id = rides.creator = posts.user //just so you know what fields equals the user id
Here is my code:
select u.id, u.username, u.state, u.city, count(p.id) as TotalPosts, count(r.id) as TotalRides
from user u
left join posts p on p.user=u.id
left join rides r on r.creator=u.id
where private='public'
group by u.id
order by u.username, u.state asc;
If I separate them out, and just join the posts or the rides, I get the correct totals back. I tried switching the order of the joins, but I got the same results. Not sure what is going on.
Any ideas or thoughts are appreciated.
Your problem is a Cartesian product along two different dimensions. The best solution is to pre-aggregate the data:
select u.id, u.username, u.state, u.city, p.TotalPosts, r.TotalRides
from user u left join
(select user, count(*) as totalposts
from posts p
group by user
) p
on p.user = u.id left join
(select creator, count(*) as totalrides
from rides r
group by creator
) r
on r.creator = u.id
where u.private = 'public'
group by u.id
order by u.username, u.state asc;
you can always use a sub select.
select u.*,
(select count(*) from posts where user = u.id) as 'posts',
(select count(*) from rides where creator = u.id) as 'rides'
from users u
where .....
Consider a simplified table schema of a StackOverflow-like system:
Tables:
User ( id, name )
Question ( id, user_id, question )
Vote ( id, question_id )
How would I write a SQL query that would
List each user along with the total votes for all his questions, ordered by most votes?
Given a user_id, return a single user record, along with his total votes?
User Name | Total Votes (desc)
select User.id,
User.name,
count(vote.id) as Votes
from User
left join Question
on User.id = Question.user_id
left join Vote
on Vote.question_id = Question.id
group by user.id, User.name
order by 3 desc
For your first question:
SELECT u.name, COUNT(v.id) AS TotalVotes
FROM User u
LEFT JOIN Question q
INNER JOIN Vote v
ON q.id = v.question_id
ON u.id = q.user_id
GROUP BY u.name
ORDER BY TotalVotes DESC
For your second question:
SELECT u.name, COUNT(v.id) AS TotalVotes
FROM User u
LEFT JOIN Question q
INNER JOIN Vote v
ON q.id = v.question_id
ON u.id = q.user_id
WHERE u.id = #GivenUserId
GROUP BY u.name
SELECT user.name, question.question, count(vote.id) votes
FROM user
inner join question on user.id=question.user_id
inner join vote on question.id=vote.question_id
GROUP BY user.name, question.question
ORDER BY votes
Trying to find duplicate users by email, who have an item count less than 0. I've got the duplicate users working (though it returns the full list of users sorted, instead of a subset):
select users.id, email, count(email) as count
from users
group by users.email
order by count desc
I'm trying to join on Items where count(items.id) < 1, but that doesn't seem to work.
select users.id, email, count(email) as count
from users
join items on items.user_id = users.id
having count(items.id) < 1
group by users.email
order by count desc
I also tried an IN query, but can't seem to get the syntax right.
Easy way to do this? Thanks!
UPDATE:
This query did the trick:
select DISTINCT(u1.id), u1.email
from users u1
inner join users u2
on 1=1
and u1.email = u2.email
and u1.id != u2.id
where not exists (select * from items i where i.user_id = u1.id)
order by u1.id
Duplicated users:
select
email,
count(*) as count,
min(id) first_user_with_this_email_id
from users
group by email
having count(*) > 1
For second one, try this:
select
users.email,
count(*) as count
from users
left join items
on (items.user_id = users.id)
where items.id is null --there are no related items
group by users.email
having count(*) > 1 --there are at least two users
Another version of second:
select
u.email,
count(*) as count
from users u
where not exists (select * from items i where i.user_id = u.id)
group by u.email
having count(*) > 1 --there are at least two users
Make sure you have index on user_id in items table.
try using WHERE instead of "having".
May be this link helps: http://www.devx.com/DevX/Tip/21295
Modified query would be :
select users.id, email, count(email) as count
from users
join items on items.user_id = users.id
where count(items.id) < 1
group by users.email
order by count desc
[ did not run and check the query, just a correction ]