What will be the query to get Student names for every individual study group? - sql

meetings which looks like this:
meeting_ID
place
1
A
2
B
3
C
study_groups which looks like this:
study_group_id
meeting_id (fk)
topic
1
1
Basics
2
2
Photography
3
1
Film
students which looks like this:
student_id
name
1
xyz
2
abc
2
pqr
group_members which looks like this:
study_group_id (fk)
student_id (fk)
2
10
1
1
2
5
3
15
1
9
3
2
3
11
A meeting has many study_groups and each study_groups has many students.
I want to find the student names in each group individually for meeting_id = 1.
Is that possible or not to do in the same query?
If not what should I do, please suggest me.
This is my present SQL query:
SELECT
study_groups.study_group_id,
(SELECT COUNT(*)
FROM group_members
WHERE study_groups.study_group_id = group_members.study_group_id) AS no_of_students,
study_groups.topic
FROM
study_groups
WHERE
study_groups.meeting_id = 1;
Now the table looks like:
study_group_id
no_of_student
topic
1
2
Basics
2
3
Photography
student_name column result in below is demo but I want the data as like this.
The result I expect:
study_group_id
no_of_student
topic
student_name
1
2
Basics
xyz, abc, pqr
2
3
Photography
abc, pqr

If you want the students in the study group and you are using Postgres, you can aggregate them into an array:
SELECT sgs.study_group_id,
(SELECT ARRAY_AGG(s.name)
FROM group_members gm JOIN
students s
ON gm.student_id = s.student_id
WHERE sg.study_group_id = gm.study_group_id
) AS student_names,
sg.topic
FROM study_groups sg
WHERE sg.meeting_id = 1;

Related

How to join three tables or more than three tables if many ID is pointing from one table to another table?

I am confused here about joining suppose I have three tables here Student table, Course table, Teacher table
'Student' table
Std_Id Std_Name Course_Id Teacher_Id
1 Amit 2 1
2 Yogesh 1 1
3 Pravin 3 2
4 Nilay 1 3
5 Abhijit 2 2
6 Vishal 2 4
7 Ramesh 3 3
8 Vijay 3 4
9 Ben 1 3
10 Vinod 2 4
'Course' table
Course_Id Course_Name Std_Id Teacher_Id
1 JAVA 1 3
2 C# 1 3
3 C++ 3 1
4 SAP 2 2
5 PYTHON 2 1
6 JAVASCRIPT 3 2
'Teacher' table
Teacher_Id Teacher_Name Std_Id Course_Id
1 Roy 1 1
2 John 2 1
3 Ben 1 3
4 Renu 2 3
5 Ramesh 1 3
As you can see I have three tables here so now I want to see the students who have courses and teachers so let`s look up here in the student table, we have Std_Id,Course_Id,Teacher_Id and in the Course table we have Course_Id,Std_Id,Teacher_Id and in the Teacher table we have Teacher_Id,Std_Id ,Course_Id so here I want to join three tables as I said I want to show students who have the courses and teachers then which column should be used here and here i am not giving any reference key so how to join three tables i am giving query here
select Std_Name,Course_Name,Teacher_Name
from Student
inner join Course
on 𝐒𝐭𝐮𝐝𝐞𝐧𝐭.𝐒𝐭𝐝_𝐈𝐝=𝐂𝐨𝐮𝐫𝐬𝐞.𝐂𝐨𝐮𝐫𝐬𝐞_𝐈𝐝
or 𝐒𝐭𝐮𝐝𝐞𝐧𝐭.𝐂𝐨𝐮𝐫𝐬𝐞_𝐈𝐝=𝐂𝐨𝐮𝐫𝐬𝐞.𝐒𝐭𝐝_𝐈𝐝
or 𝐒𝐭𝐮𝐝𝐞𝐧𝐭.𝐂𝐨𝐮𝐫𝐬𝐞_𝐈𝐝=𝐂𝐨𝐮𝐫𝐬𝐞.𝐂𝐨𝐮𝐫𝐬𝐞_𝐈𝐝
or 𝐒𝐭𝐮𝐝𝐞𝐧𝐭.𝐒𝐭𝐝_𝐈𝐝=𝐂𝐨𝐮𝐫𝐬𝐞.𝐒𝐭𝐝_𝐈𝐝
inner join Teacher
on 𝐂𝐨𝐮𝐫𝐬𝐞.𝐂𝐨𝐮𝐫𝐬𝐞_𝐈𝐝=𝐓𝐞𝐚𝐜𝐡𝐞𝐫.𝐓𝐞𝐚𝐜𝐡𝐞𝐫_𝐈𝐝
or 𝐂𝐨𝐮𝐫𝐬𝐞.𝐓𝐞𝐚𝐜𝐡𝐞𝐫_𝐈𝐝=𝐓𝐞𝐚𝐜𝐡𝐞𝐫.𝐂𝐨𝐮𝐫𝐬𝐞_𝐈𝐝
or 𝐂𝐨𝐮𝐫𝐬𝐞.𝐓𝐞𝐚𝐜𝐡𝐞𝐫_𝐈𝐝=𝐓𝐞𝐚𝐜𝐡𝐞𝐫.𝐂𝐨𝐮𝐫𝐬𝐞_𝐈𝐝
or 𝐂𝐨𝐮𝐫𝐬𝐞.𝐓𝐞𝐚𝐜𝐡𝐞𝐫_𝐈𝐝=𝐓𝐞𝐚𝐜𝐡𝐞𝐫.𝐓𝐞𝐚𝐜𝐡𝐞𝐫_𝐈𝐝
so which common column should be used here to join three tables please let me know guys please give me answer.

Select columns from a table and count a value from another table

I have the following tables:
table_user
id
name
1
Arnold
2
Wesley
3
Abel
4
Harry
5
Cristiano
table_post
fk_user is from user who created the post
id
fk_user
title
description
1
2
Movie
I like horror movies
2
4
Music
Music is essential to my days
3
4
Soccer
The World Cup is coming!
table_post_like
fk_user is from user who liked the post
fk_post
fk_user
1
1
1
3
1
4
1
5
2
2
3
3
3
1
I need to select all posts liked by user Arnold (id 1) with all columns from tb_post and number of likes. The result would be something like that:
id
fk_user
title
description
likes
1
2
Movie
I like horror movies
4
3
4
Soccer
The World Cup is coming!
2
I tried this but the number of likes is not correct
SELECT TP.*, COUNT(TPL.fk_post) AS likes
FROM table_post AS TP
RIGHT JOIN table_post_like AS TPL
ON TP.id = TPL.fk_post
WHERE TPL.fk_user = 1
GROUP BY TP.id
Do this
SELECT table_post.*, t.occurence FROM table_post
JOIN
(SELECT fk_post, COUNT(fk_user) as occurence FROM table_post_like WHERE fk_post IN(SELECT fk_post FROM table_post_like WHERE fk_user = 1)
GROUP BY fk_post) as t ON t.fk_post = table_post.id
Query above returns the value as expected
id
fk_user
title
description
likes
1
2
Movie
I like horror movies
4
3
4
Soccer
The World Cup is coming!
2
Check db fiddle here

how can I convert this SQL statement with nested IN clauses to a JOIN format

Here are the very simplified versions of my three tables.
Schools_Subjects Table
SchoolId
SubjectId
1
2
5
1
5
6
The above table contains only parent subject IDs.
Subjects Table
SubjectId
Name
ParentSubjectId
1
Science
{NULL}
2
Mathematics
{NULL}
3
Biology
1
4
Physics
1
5
Chemistry
1
6
History
{NULL}
7
Elementary Math
2
8
Calculus
2
Questions Table
QuestionId
Text
SubjectId
1
What is 2 + 2
7
2
Tell me the fastest animal name
3
3
Salt is composed of which two elements
5
4
How to divide 6 apples among 3 students
7
I want to fetch all the questions given a (or multiple) school ID. For example for schoolId:5, I have the below SQL query:
SELECT *
FROM Questions
WHERE SubjectId IN (
SELECT SubjectId
FROM Subjects
WHERE ParentSubjectId IN (
SELECT SubjectId
FROM Schools_Subjects
WHERE SchoolId = 5
)
)
My above query works but I want to change it into a JOIN format query.
I work on SQL Server, but a ANSI-SQL query will be highly appreciated.
If using Mysql:
SELECT Q.*
FROM Questions Q
JOIN (
SELECT S.SubjectId
FROM Subjects S
JOIN Schools_Subjects SS
ON S.ParentSubjectId = SS.SubjectId AND SS.SchoolId = 5
) t1
ON Q.SubjectID = t1.SubjectId
QuestionId Text SubjectId X
1 2 Tell me the fastest animal name 3 NA
2 3 Salt is composed of which two elements 5 NA
Which is the same results produced by your code

SQL select two count from one table

I have a table with columns:
COURSE_ID (int)
SKILL_ID (int)
One course could have many skills, for example the table could contain values:
COURSE_ID
SKILL_ID
1
1
1
2
2
2
2
3
2
4
3
1
4
1
4
2
The result should show count of courses and count of skills they have.
For example for the table above the result should be:
1 = 1 (course 3 has 1 skill) (count course with 1 skill = 1)
2 = 2 (course 1 and 4 have 2 skills) (count course with 2 skill = 2)
3 = 1 (course 2 has 3 skills) (count course with 3 skill = 1)
Could anybody help with this query?
And one more question. I tried to execute this query and I am expecting one number with count of courses with 6 skills, but I got many records (in fact - rows count = expected result, value in rows = 6), can't understand why, could anybody explain?
select count(table.course_id) from Table table
GROUP BY table.course_id
HAVING COUNT(table.skill_id) = 6
It's not entirely clear what you're expecting as a result but I think this is what you're after
select Skill, Count(*) courseCount
from (
select course_id, Count(distinct SKILL_ID) Skill
from t
group by COURSE_ID
)s
group by Skill;
DB<>Fiddle
Result:
Try
select table.course_id, count(table.skill_id)
from table_name table
GROUP BY table.course_id
And your query should be
select table.course_id, count(table.skill_id)
from table_name table
GROUP BY table.course_id
HAVING COUNT(table.skill_id) = 6

How can i solve blank spots in a SQL View?

In my sqllite-excercises i have discovered the following problem:
I basically have three different Tables:
Subjects
PRIMARY KEY(ID)
Subject
1
Business
2
IT
3
Sports
Participants
PRIMARY KEY(ID)
Name
semester
1
Meyer
6
2
Smith
4
3
Brown
4
4
White
2
5
Anthonie
2
6
Frankson
2
They are referenced in the Table participants List
SUBJECT.ID
Participant.ID
1
2
1
3
1
5
2
4
2
6
3
1
Now im supposted to create a VIEW that contains: The Participants.ID, Participants.Name and Subjects.Subject so i have a Table that shows the ID, the Name and the Subject the participant is visiting.
So far I did this:
CREATE VIEW[OVERVIEW]AS
SELECT Participants.ID,
Participants.Name,
Subjects.Subject
from Participants
LEFT JOIN Subjects on Participants.ID = Subjects.ID;
As a result i get this:
Participants.ID
Participant.Name
Subjects.Subject
1
Meyer
Business
2
Smith
IT
3
Brown
Sports
4
White
None
5
Anthonie
None
6
Frankson
None
And it makes sense since there are only three Subjects and i Leftjoined 6 Participants.ID on only 3 Subjects.Subject
How can i fill out the blank Subjects? So that the subjects for 4-6 are shown aswell?
I hope you can understand my problem and i declared it good enough.
You can't join Participants to Subjects because they are not directly related, so this ON clause:
on Participants.ID = Subjects.ID
does not make sense because the IDs of participants are not related to the IDs of the subjects.
You have the table ParticipantsList that can be used as an intermediate link between the other 2 tables:
SELECT p.ID, p.Name, s.Subject
FROM Participants p
LEFT JOIN ParticipantsList pl ON pl.Participant_ID = p.ID
LEFT JOIN Subjects s ON s.ID = pl.Subject_ID;
This will return all participants, even if they are not linked to any subject.
If you want only the participants for which there is at least 1 subject in the table ParticipantsList then you can use INNER joins.
For the sample data that you provide in your question in both cases the results are the same.
See the demo.