SQL query to exclude records that are part of a group - sql

I can't believe this hasn't been answered elsewhere, but I don't seem to know the right words to convey what I'm trying to do. I'm using Ruby/Rails and PostgreSQL.
I have a bunch of Users in the DB that I'm trying to add to a Group based on a name search. I need to return Users that do not belong to a particular Group, but there is a join table as well (UserGroups, with the appropriate FKs).
Is there a simple way to use this configuration to perform this query without having to result to grabbing all the Users from which belong to the group and doing something like .where.not(id: users_in_group.pluck(:id)) (these groups can be pretty huge, so I don't want to send that query to the DB on a text search as the user types).

I need to return Users that do not belong to a particular Group
SELECT *
FROM users u
WHERE username ~ 'some pattern' -- ?
AND NOT EXISTS (
SELECT FROM user_groups ug
WHERE ug.group_id = 123 -- your group_id to exclude here
AND ug.user_id = u.id
);
See:
Select rows which are not present in other table

Related

group_concat multiple rows where given condition

I have 3 tables (User, User_usergroups , usergroup). I would like to get a list of user who has usergroup equal to "member" and other groups (group_concat) belonging to this user. How do I do that?
I got the first part of the query, but I could not get other groups, aggregated groups of this user (I like to use group_concat to concatenate other groups in one field.
SELECT user.userId, (group_concat(group_.name)???)
FROM User_ user join Users_UserGroups userGroups
on user.userId= userGroups.userId
join UserGroup group_ on userGroups.userGroupId = group_.userGroupId
WHERE group_.name="member";
Assume that is the 3 tables
The outcome will be
Many thanks for your great help.
It looks like my query is so complex and it has down vote from the administrator or someone. I wish whoever did this could add their comment to my query so that, in the future, I will know what I need to do to clarify my question to the community or make my question better. Here is the answer to my question.
First retrieve the list of userId.
Then join this "member" table with other tables to find the result from the list of retrieved ID (step 1).
As we aggregate the name of the groups, we need to use groupby.
Keywords to resolve this problem is to use an alias for subquery or nested SQL
select member.userid, member.screenName, group_concat(member.name)
from (SELECT User_.userid userid, User_.screenName screenName , UserGroup.name name
FROM UserGroup
JOIN Users_UserGroups
ON UserGroup.userGroupId = Users_UserGroups.usergroupid
JOIN User_
on Users_UserGroups.userId = User_.userId
WHERE UserGroup.name = "member")member
JOIN Users_UserGroups
ON member.userid = Users_UserGroups.userId
JOIN UserGroup
on UserGroup.userGroupId=Users_UserGroups.userGroupId
GROUP BY member.userid;

'Exploratory' SQL queries that uses one criteria to find more criteria

Sorry about this dreadful title. Imagine tables like this: http://sqlfiddle.com/#!9/48d921/1
Here, we run a query for all users with the name "Bob", but we are also interested in all users in the same postcode as "Bob" and also all users of the same "type" as Bob.
You can see I joined the same tables twice to achieve this. The trouble with it is it doesn't scale; the more criteria I want to "explore" the more times I have to join the same tables, making the select statement more cumbersome.
So:
What's the best way to do this?
Does this type of query have a name?
The answer updated
SELECT
FROM user
where u.name="Bob"
OR (u.postcode in (SELECT a.postcode
FROM USER u
JOIN ADDRESS a on a.user=u.id
WHERE u.name="Bob")
)
OR (u.type in (SELECT ut.type
FROM USER u
JOIN USER_TYPE ut1 on u.id=ut1.user
WHERE u.name="Bob")
)
So you scan users table just once for each record checking the linking criteria

mysql query using values (no subselect)

Due to the permanent use of ORM I have no idea how to write this query, but I need to write it without ORM.
tables design:
users:
|id|username|...
ports:
|id|user_id|port|
My app gets a username, now I need to find all ports for the matching username using the id of the user to get the ports.
Do a JOIN to find the ports for the user specified by userparam (or whatever you call it.):
select u.id, u.username, p.id, p.user_id, p.port
from users u
join ports p on u.id = p.user_id
where u.username = :userparam
(You can remove the columns you don't need from the SELECT list.)
users:
id|username|...
ports:
id|user_id|port|
think about the SELECT line last. just put a place holder to begin
select count(*)
then actually begin. ask what tables do I need to get data FROM?
FROM users, ports
this is going to (conceptually) make a new table with rows that have every user matched up with every port.
how do I filter out all of the data in this new table that does not
apply to my query? ignore everything except WHERE you tell it to match
WHERE user.id == ports.user_id
you can think of this is as filtering for rows in the giant table made from all the rows of all the tables you put in the FROM clause.
Now that you have these rows of everything you can SELECT which columns from the new table you made that you are interested in.
lets say you want any port.id (s) returned.
SELECT port.id
FROM users, ports
WHERE user.id == ports.user_id
A nicer syntax that keeps the WHERE parts near their FROM parts
which gets important when you have lots of tables looks like this
SELECT port.id
FROM users join ports ON user.id == ports.user_id

Select entries from a list that do not occur in the query result

I use Postgres 9.1. I am running a query which is like
select count(distinct(user_id)) from users where user_id in (11,32,763,324,45,76,37,98,587);
Here the list of user_ids contains 600 entries. The result I am getting is 597. Thus there are 3 user_ids from the list, which are not present in the users. How do I get to know these 3 user_ids?
Please note that user_id is not the Primary Key of users
DISTINCT in your count-query only makes sense if user_id is not defined UNIQUE.
We don't need it either way for the query you ask for:
SELECT t.user_id
FROM unnest('{11,32,763,324,45,76,37,98,587}'::int[]) t(user_id)
LEFT JOIN users u USING (user_id)
WHERE u.user_id IS NULL;
Beware of NOT IN if NULL values can be involved on either side of the expression! IN / NOT IN with a long value list also scales poorly.
Details:
Optimizing a Postgres query with a large IN
Select rows which are not present in other table

How to show values from two different queries?

I have one database that contains all of user information including name. Then there is a second database that contains notes from the users and it contains the #id but not the name. The query i am doing to retrieve user notes doesn't have name so all its doing is showing the notes, then right under it i am doing another query to retrieve the name from the first database using the common #id. But it won't show.
Is there a way I can do this query in one? Please help. Thanks.
Use:
SELECT u.name,
n.*
FROM DB2.NOTES n
LEFT JOIN DB1.USERS u ON n.id = u.id
ORDER BY u.name
Assuming the connection credentials has access to both databases, you prefix the database name in front of the table name and separate with a period.
The LEFT JOIN will show both users, and notes without users associated. Here's a good primer on JOINs.
You might need to show your code, but you can write queries against two databases (or schemas) on the same host, just qualify the table names with the database name, e.g.
SELECT db1.user.id, db1.user.name, db2.userinfo.notes
FROM db1.user
INNER JOIN db2.userinfo ON(db1.user.id=db2.userinfo.id)
The credentials you are connecting with must have access to both databases for this to work of course.