How to combine two SQL queries in one - sql

I have tables user, participant and chat. I need to get all users in a specific chat and amount of chats that user in by chat name. For example current tables:
user chat participant
id|name id|name user_id|chat_id
1|Mike 1|School 1|1
2|John 2|Football 2|1
3|Sara 3|Gym 1|2
3|3
And by keyword "School" I want to get this
Mike|2
John|1
I have two queries to get first and second column in result but don't know how to combine it:
SELECT user.name FROM user
JOIN participant ON (user.id = participant.user_id)
JOIN chat ON (participant.chat_id = chat.id) WHERE chat.name = 'School';
That gives me
Mike
John
And
SELECT user.name, COUNT(*) FROM user
JOIN participant ON (user.id = participant.user_id) GROUP BY user.name;
returns
John|1
Mike|2
Sara|1
So how to combine it?

TRY THIS
SELECT p1.name, COUNT(p.user_id) totUser
FROM participant p
INNER JOIN (select u.id, u.name FROM participant p
inner JOIN chat c ON c.id = p.[chat_id]
INNER JOIN user u ON u.id = p.user_id
AND c.name = 'School') p1 ON p1.id = p.user_id
GROUP BY p1.name

using Subquery and joins :
select u.name,count(p.chat_id) as 'Count' from user u
inner join participant p on p.user_id = u.id
where
p.user_id in ( select user_id from participant pp inner join chat cc on cc.id = pp.chat_id where
cc.name = 'School' )
group by u.name
order by Count desc
Output :

Related

Distinct Count with data from another table

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.

SQL find parent where children match over multiple rows

I have a database where one user has multiple submission and one submission has multiple post
I would like to find all users who have made posts with the tag "car" and posts with the tag "bike".
Following is the query I tried which won't work since it looks for a single post with the tag "car" and "bike"
select distinct u.*
from users u
inner join submission s on u.name = s.user_name
inner join post rp on s.id = rp.submission_id
where rp.tag = 'card' and rp.tag = 'bike'
How would I structure this query correctly so it returns the users who have made posts with the tag "car" and posts with the tag "bike"?
if you need only the name you could try checking for the user with count distinct = and tag = card or tag = bike
select u.name
from users
inner join submission s on u.name = s.user_name
inner join post rp on s.id = rp.submission_id
where rp.tag = 'car' OR rp.tag = 'bike'
group by u.name
having count(distinct rp.tag) = 2
if you need all the user info you should join this result with users
select u.*
from users u
inner join (
select u.name
from users
inner join submission s on u.name = s.user_name
inner join post rp on s.id = rp.submission_id
where rp.tag = 'car' OR rp.tag = 'bike'
group by u.name
having count(distinct rp.tag) = 2
) t on t.name = u.name

Get results from multiple tables based on relationship table

I have dbo.Users tables
Id, Name
1, John
2, Mary
3, Michael
Then I have dbo.Phones table
Id, Phonenumber
10, 1234
11, 5555
Then I have dbo.Relationship table
Id, ChildId
1, 10
2, 11
How could I make a query that returns
Id, Name, Phonenumber
1, John, 1234
2, Mary, 5555
3, Michael, NULL
This is what I got so far.
SELECT u.Id, u.Name, p.Phonenumber
FROM dbo.Users as u
LEFT JOIN dbo.Phones as p
-- Something
SQL Fiddle
Think of the Relationship table as the middle-man between your Users and Phones tables here. It is a many-to-many relationship with a mapping table. Join your Users to the Relationship and then the Relationship to your Phones.
SELECT u.Id
,u.Name
,p.PhoneNumber
FROM dbo.Users u
LEFT JOIN dbo.Relationship r ON r.Id = u.Id
LEFT JOIN dbo.Phones p ON p.Id = r.ChildId
Think of it like:
Users: Hello Relationship, I have UserId = 1, what PhoneIds do I have for that UserId?
Relationship: Hi Users. I have PhoneId = 10 for you. I'll go talk to Phones to see what the number is.
Phones: Hi Relationships! I have PhoneNumber 1234 for you. It matches the PhoneId you gave me.
Join them on id field.I would use inner join depending on the requirement
SELECT distinct u.Id, u.Name, p.Phonenumber
FROM dbo.Users as u
LEFT JOIN dbo.Phones as p on u.id = p.id
or---
SELECT distinct u.Id, u.Name, p.Phonenumber
FROM dbo.Users as u
inner join T JOIN dbo.Phones as p on u.id = p.id
inner join dbo.relationship r on r.id = u.id
where----
try this :
Select u.Id, u.Name, p.Phonenumber
From
Users u
Left join Relationship r on r.Id = u.Id
Left join Phones p on r.ChildId = p.Phonenumber

Find records combining two joins

I have three tables:
User, House and HouseEvent
A House has a foreign_key (user_id) to User and an HouseEvent has a foreign_key (house_id) to House
You can see the schema here: http://sqlfiddle.com/#!9/f08db/5 -
I know how I can get all the users which do not have a house:
SELECT * FROM User
LEFT OUTER JOIN House u ON u.user_id = user.id
WHERE u.user_id IS NULL
But how could I get in a single query, all the users who do not have a house and those users who have a house with at (least) a suspended event.
So, in the example, I would get Lee because he does not have a house, and I would also get John, because even though he has houses, one of its houses has an associated suspended event.
UNION the 2 queries together?
SELECT u.id, u.name FROM User u
JOIN House h ON h.user_id = u.id
JOIN HouseEvent he ON he.house_id = h.id AND he.name = 'suspended'
UNION
SELECT u.id, u.name FROM user u
LEFT JOIN house h ON h.user_id = u.id
WHERE h.user_id IS NULL
Fiddle
You can do that without using SUB QUERY, just use LEFT JOIN here is the query:
SELECT User.name FROM User
LEFT JOIN House h on h.user_id = user.id
LEFT JOIN HouseEvent e on h.user_id = e.house_id
WHERE h.user_id IS NULL OR e.name = 'suspended'
GROUP BY User.name
I modified your SQL Fiddle
SELECT * FROM User
Where id not in (SELECT user_id FROM House) OR
id in (select user_id from House h, HouseEvent he where he.house_id = h.id AND he.id = 2)
http://sqlfiddle.com/#!9/f08db/41

List all friends for a userid SQL query

I have 2 tables a user table (user_id, fname, lname, dob, etc) and a are_friends table
(userA_id, userB_id). I have been trying to do this query for a while now, I need it to list all friends for a user_id.
What I have got so far,
SELECT
U.user_id,
U.fname,
U.lname
FROM are_friends A, user U
WHERE
A.user_id = U.user_id
AND (
A.user_id = 1
OR A.user_id IN (SELECT userB_id FROM are_friends WHERE userA_id = 1)
);
Any help will be much appreciated.
Try using an INNER JOIN like this:
SELECT u2.user_id, u2.fname, u2.lname
FROM user u
INNER JOIN are_friends f ON f.userA_id = u.user_id
INNER JOIN user u2 ON u2.user_id = f.userB_id
WHERE u.user_id = 1
You can change the WHERE clause to specifically get the friends of another user id.