What kind of SQL join would this be? - sql

I need to go to two tables to get the appropriate info
exp_member_groups
-group_id
-group_title
exp_members
-member_id
-group_id
I have the appropriate member_id
So I need to check the members table, get the group_id, then go to the groups table and match up the group_id and get the group_title from that.

INNER JOIN:
SELECT exp_member_groups.group_title
FROM exp_members
INNER JOIN exp_member_groups ON exp_members.group_id = exp_member_groups.group_id
WHERE exp_members.member_id = #memberId

SELECT g.group_title
FROM exp_members m
JOIN exp_member_groups g ON m.group_id = g.group_id
WHERE m.member_id = #YourMemberId

If there is always a matching group, or you only want rows where it is, then it would be an INNER JOIN:
SELECT g.group_title
FROM exp_members m
INNER JOIN
exp_member_groups g
ON m.group_id = g.group_id
WHERE m.member_id = #member_id
If you want rows even where group_id doesn't match, then it is a LEFT JOIN - replace INNER JOIN with LEFT JOIN in the above.

Related

How do I filter out SELECT results from tables?

I have three tables that have user name/id and how many tasks they have submitted. I'm trying to SELECT user.name and the max amount of submissions they have for a single task.
SELECT DISTINCT O.nimi, COUNT(T.id)
FROM Opiskelijat O
LEFT JOIN Lahetykset L ON O.id = L.opiskelija_id
LEFT JOIN Tehtavat T ON T.id = L.tehtava_id
GROUP BY O.id, L.tehtava_id
The first picture shows the tables in question. In the second picture the above is what I'm trying to get, and the bottom is what my code does at the moment. I'm trying to get it to only show Maija - 3 instead of both.
Maybe somehing like this:
SELECT O.nimi, COUNT(*)
FROM Opiskelijat O
LEFT JOIN Lahetykset L ON O.id = L.opiskelija_id
LEFT JOIN Tehtavat T ON T.id = L.tehtava_id
GROUP BY O.id, O.nimi
As was pointed out, you need to have all the selected output fields in the GROUP BY. And also the DISTINCT should not be needed. I think the problem was the group by on L.tehtava_id.
If you want one row per nimi that should be the only column in the GROUP BY. I think you want:
SELECT O.nimi, COUNT(T.id)
FROM Opiskelijat O LEFT JOIN
Lahetykset L
ON O.id = L.opiskelija_id LEFT JOIN
Tehtavat T
ON T.id = L.tehtava_id
GROUP BY O.nimi;
I suspect that you don't actually need the join to Tehtavat:
SELECT O.nimi, COUNT(L.tehtava_id)
FROM Opiskelijat O LEFT JOIN
Lahetykset L
ON O.id = L.opiskelija_id
GROUP BY O.nimi;

How to list all users, and use a sub-query in an outer join

I want to return all users, and if they went to a conference I want to return the conference information. The #ConferenceID will be a parameter.
SELECT
U.UserId,
O.ConferenceName,
O.LocationName
FROM Users
My outer join will need something like:
SELECT *
FROM Conferences C
INNER JOIN Locations L ON C.LocationId = L.LocationId
WHERE UserId = ??
AND C.ConferenceID = #ConferenceID
Is it possible to perform an outer join so that all users are returned, and then optionally display the conference info if they went to one?
I tried this:
SELECT
U.*,
oj.
FROM Users U
OUTER JOIN (
SELECT c.ConferenceName, L.LocationName
FROM Conferences C
INNER JOIN Locations L ON C.LocationId = L.LocationId
WHERE C.ConferenceID = #ConferenceID
) AS oj.UserID = U.UserID
But I get an error
The multi-part identifier "U.UserId" could not be bound.
DDL:
User
-UserId
Conference
-ConferenceID
-UserID
-LocationId
Locations
-LocationID
Yes, you can use an outer join, in particular a LEFT JOIN, but you need to move the ConferenceID condition to the join clause, and you need to LEFT JOIN both tables.
SELECT U.UserId
, C.ConferenceName
, L.LocationName
FROM Users U
LEFT JOIN Conferences C ON C.ConferenceID = #ConferenceID
AND C.UserID = U.UserId
LEFT JOIN Locations L ON L.LocationID = C.LocationId

Check Specfic row with multiple rows in ORACLE

I want to select users list who is status is disabled for all application in username(ur_username table and that users should be active in Person Table(ur_person).
I tried with the following query :
select distinct E.PERSON_ID , E.DOMAIN_ID,e.first_name,e.last_name,e.type,E.username, E.SYSTEM_name from
(SELECT distinct p.person_id,p.domain_id,p.first_name,p.last_name,p.type,u.username, U.STATUS, U.SYSTEM_ID,s.system_name from ur_username u join
ur_username_person up on u.username_id=up.username_id
join ur_person p on up.person_id=p.person_id join ur_system s on u.system_id=s.system_id
WHERE p.status='ACTIVE') E WHERE E.person_id IN
( select distinct P.PERSON_ID from ur_username u join ur_username_person up on u.username_id=up.username_id
join ur_person p on up.person_id=p.person_id
where u.status='DISABLED')
I need to get the person list who is status is Disabled in all system in username Table as below image:
But instead I am getting person who is disable in one system but active in some other systems also:
You can get the person_ids using aggregation and having:
select p.person_id
from ur_username u join
ur_username_person up
on u.username_id = up.username_id join
ur_person p join
ur_system s
on u.system_id = s.system_id
where p.status = 'ACTIVE'
group by p.person_id
having max(u.status) = min(u.status) and max(u.status) ='DISABLED';
You specify that you want the persons, so this seems to answer your question. You can easily enhance this query to return more columns.

SQL LEFT JOIN for joining three tables but one with to exclude content

I have 3 tables
STUDENTS
FEES_PAID
SUSPENDED
I want to get the details of the students who have paid the fees but not from SUSPENDED.
SELECT
ID
FROM
STUDENTS s
LEFT JOIN
SUSPENDED p ON s.ID = p.ID
INNER JOIN
FEES_PAID f ON f.ID = s.ID
WHERE
s.ID IS NULL
Unfortunately this does not work. Can any one suggest an efficient query?
Thanks in advance
You need to check if the second table is missing from the LEFT JOIN. So, you need to look at a column in that table. Change the WHERE to:
WHERE p.ID IS NULL
Alternatively, use NOT EXISTS:
SELECT s.ID
FROM STUDENTS s INNER JOIN
FEES_PAID f
ON f.ID = s.ID
WHERE NOT EXISTS (SELECT 1 FROM SUSPENDED p WHERE s.ID = p.ID);
Note that for both these queries, you will need to qualify the ID in the SELECT to specify the table where it comes from.
This should work:
SELECT
s.ID
FROM
STUDENTS s
LEFT JOIN
SUSPENDED p
ON s.ID=p.ID
INNER JOIN
FEES_PAID f
ON f.ID= s.ID
WHERE
p.ID IS NULL

SQL two multi-row counts in one query

I have got two queries:
select m.name, count(distinct a.kursnr)
from trainer t
left outer join mitarbeiter m
on t.svnr = m.svnr
left outer join einzeltraining e
on t.svnr = e.trainer
left outer join abhaltung a
on t.svnr = a.trainer
group by m.name, t.svnr;
select m.name, count(e.trainer)
from trainer t
left outer join mitarbeiter m
on t.svnr = m.svnr
left outer join einzeltraining e
on e.trainer = t.svnr
group by m.name, e.trainer;
The first one returns the correct number of courses (kursnr) and the second number the correct number of individual classes (einzeltraining) hold by a trainer. However, I cannot make one SQL statement which shows both values in one table. Any help would be appreciated. Thank you.
While there is likely a more efficient way to do this, I wanted to show you the easy way to combine any two queries that share a common field such as this:
select coalesce(q2.name, q1.name) As Name, q1.KursnrCount, q2.TrainerCount
from
( --original first query
select m.name, count(distinct a.kursnr) as KursnrCount
from trainer t
left outer join abhaltung a
on t.svnr = a.trainer
left outer join mitarbeiter m
on t.svnr = m.svnr
left outer join einzeltraining e on svnr = e.trainer
group by m.name, t.svnr
) q1
full join
( --original second query
select count(e.trainer) as TrainerCount, m.name
from trainer t
left outer join einzeltraining e
on e.trainer = t.svnr
left outer join mitarbeiter m
on t.svnr = m.svnr
group by e.trainer, m.name
) q2 on q2.name = q1.name
You could also use an inner join or left join, instead of a full join, depending on how the name fields from those queries match up.