Table : Class
class_id ClassName
----------------------
1 AAA
2 BBB
3 CCC
Table : Groups
id class_id GroupName
---------------------------
1 1 A1
2 1 A2
3 2 B1
4 3 C1
5 2 B2
6 1 A3
Expected Output :
class_id ClassName count(*)
-------------------------------
1 AAA 3
2 BBB 2
3 CCC 1
Use Inner Join to get result :
SELECT Class.class_id, Class.ClassName, COUNT(*) AS count
FROM Class INNER JOIN
Groups ON Class.class_id = Groups.class_id
GROUP BY Class.class_id, Class.ClassName
Related
I have parent table as
pID Name
1 AAA
2 BBB
3 CCC
and a child table as
cID pID Name
1 1 XXX
2 1 YYY
3 2 XXX
4 2 YYY
5 2 ZZZ
6 3 YYY
7 3 ZZZ
now i need to select the parent rows that have at least 2 child rows one of them have the value YYY and the other ZZZ, which should be pID 2 & 3.
is this possible?
thanks in advance everyone
You can try the below -
select p.pid,p.name from c join p on p.pid=c.pid
where c.name in ('YYY','ZZZ')
group by p.pid,p.name
having count(distinct c.name)=2
I have database where 2 roles can't be associated with each other, and I need to display any users who have conflicting roles.
For example: an (id 2) accountant can't also be a (id 5) trainer
this has to be done without using CTE's
Table a Table b table c
--------------- ------------------- ------------
userID | roleID roleID | conflictID roleID | Role Name
1 2 2 5 1 chef
1 3 2 accountant
1 5 3 driver
2 3 4 barmaid
2 1 5 trainer
3 2
3 3
the result should contain only the userID who has both roles 2 and 5
userID
------
1
Join the b table with the a table twice, to get userID's with conflicting combinations:
select distinct a1.userid
from tableb b
join tablea a1 on b.roleID = a1.roleID
join tablea a2 on b.conflictID = a2.roleID
and a1.userID = a2.userID
3 tables:
Mark table:
student_id sa_id marks
1 1 75
1 2 80
1 3 100
2 4 85
2 5 90
2 6 60
course table:
course_code sat_id name_code
AAA 100 1 midterm1
AAA 100 2 midterm2
AAA 100 3 final
BBB 200 4 midterm1
BBB 200 5 midterm2
BBB 200 6 final
transform table:
sa_id sat_id
1 1
2 2
3 3
4 4
5 5
6 6
select course.course_code, mark.marks
from mark
left outer join transform on transform.sa_id = mark.sa_id
left outer join course on course.sat_id = transfrom.sat_id
where course.name_code = 'midterm1'
At the above query only midterm1 result, also we can extract mid2 and final
select mark.student_id,course.course_code, mark.marks, course.name_code
from mark
left outer join transform on transform.sa_id = mark.sa_id
left outer join course on course.sat_id = transfrom.sat_id
order by mark.student_id, course.course_code
Result will give:
student_id course_code marks name_code
1 AAA 100 75 midterm1
1 AAA 100 80 midterm2
1 AAA 100 100 final
2 BBB 200 85 midterm1
2 BBB 200 90 midterm2
2 BBB 200 60 final
So how to build query that should be
student_id course_code midterm1 midterm2 final
1 AAA 100 75 80 100
2 BBB 200 85 90 60
You can use case when , group by (and a fake aggregation function)
select
student_id
, course_code
, max(case when name_code ='midterm1' then mark else null) midterm1
, max(case when name_code ='midterm2' then mark else null) midterm2
, max(case when name_code ='final' then mark else null) final
from mark
left outer join transform on transform.sa_id = mark.sa_id
left outer join course on course.sat_id = transfrom.sat_id
group by student_id, course_code
order by mark.student_id, course.course_code
But if you don't like aggregation functio you can use a 3 seleft join on mark
select
mm1.student_id
, mm1.course_code
, mm1.mark midterm1
, mm2.mark midterm2
, mm3.mark finale
from mark as mm1
left join mark as mm2 on mm1.student_id = mm2.student_id and mm1.course_code = mm2.course_code
left join mark as mm3 on mm1.student_id = mm3.student_id and mm1.course_code = mm3.course_code
left outer join transform on transform.sa_id = mm1.sa_id
left outer join course on course.sat_id = transfrom.sat_id
where mm1.name_code = 'midterm1'
and mm2.name_code = 'midterm2'
and mm3.name_code = 'final'
My table schema is like follow:
Table Name: Quality
ID Name Type
-- ---- ----
1 XYZ S1
1 XYZ B1
1 XYZ S2
1 XYZ R1
2 ABC B1
2 ABC B2
2 ABC R1
2 ABC U1
3 PQR B1
3 PQR B2
3 PQR R2
3 PQR R1
4 AAA B1
4 AAA S1
5 BBB B1
5 BBB B2
5 BBB U2
I want to filter out those IDs whose Type is B1 but it should not be (R1 and U1 in other rows). Also those IDs whose type is B2 but it shuld not be (R2 and S2 in other rows)
here,the output should be
ID Name Type
-- ---- ----
2 ABC B2
4 AAA B1
5 BBB B1
My query is following which is nto giving proper result:
SELECT
ID , NAME , TYPE
FROM
QUALITY Q
WHERE
(Q.TYPE IN ('B1') AND (Q.TYPE Not IN ('R1', 'U1'))
OR
(Q.TYPE IN ('B2') AND (Q.TYPE Not IN ('R2', 'U2'))
My query runs for one record at a time so i am not getting proper result. how can I make this query check every record of that particular ID to find TYPE?
Any help will be really useful.
You can use NOT EXISTS:
SELECT *
FROM Quality q
WHERE
(Type = 'B1' AND NOT EXISTS(SELECT 1 FROM Quality WHERE ID = q.ID AND Type IN ('R1', 'U1')))
OR (Type = 'B2' AND NOT EXISTS(SELECT 1 FROM Quality WHERE ID = q.ID AND Type IN ('R2', 'U2')))
select * from
quality q
where
(q.type = 'b1' and q.id not in (select q2.id from quality q2 where q2.type in ('r1','u1'))
or
(q.type = 'b2' and q.id not in (select q3.id from quality q3 where q3.type in ('r2','u2'))
I have a mapping table tableA
key value
-----------
1 "John"
2 "George"
3 "Kate"
4 "loves"
5 "hates"
and another tableB that contains rows based on keys of tableA
col1 col2 col3
------------------
1 5 2
2 4 3
3 4 1
I want to write a selection query which will return rows from table B but replaced with their appropriate values.
e.g. the output has to be:
John | hates | George
George | loves | Kate
Kate | loves | John
Thank you.
SELECT A1.value, A2.value, A3.value
FROM tableB
JOIN tableA as A1 ON tableB.col1 = A1.key
JOIN tableA as A2 ON tableB.col2 = A2.key
JOIN tableA as A3 ON tableB.col3 = A3.key;
You should probably put the last 2 items, 'loves' and 'hates', into a separate table as they represent a different type of data than the other 3.
Here are the tables:
users:
id name
----------
1 John
2 Edward
3 Kate
feelings:
id type
----------
1 love
2 hate
feelings_users:
id user1_id feeling_id user2_id
-----------------------------------
1 1 2 2
2 2 1 3
3 3 1 1
and here's the query:
select `Ua`.`name`, `F`.`type`, `Ub`.`name` from `feelings_users` as `X`
left join `users` as `Ua` on `X`.`user1_id` = `Ua`.`id`
left join `feelings` as `F` on `X`.`feeling_id` = `F`.`id`
left join `users` as `Ub` on `X`.`user2_id` = `Ub`.`id`
and it will output:
name type name
--------------------
John hate Edward
Edward love Kate
Kate love John