Count the number of students and display in another table - mysql

I have Student and Course Table:
Student(Roll_no(primary), Name, Course_id(foreign))
Course(Course_no(primary), Course_name)
How will I retrieve:
a. The table with the details of Courses in which more than 2 students got admitted.
b. The Course table along with a count column.
What I tried for the first problem:
SELECT * FROM Course WHERE(SELECT COUNT(*) FROM Student GROUP BY Course_no WHERE COUNT(*) > 2);
I'm not sure what I did, but that didn't work. I am new to Database System
.
Sample:
Student Table
+---------+----------+----------+-----------+
| Roll_no | Name | Semester | Course_no |
+---------+----------+----------+-----------+
| 1 | a | 3 | 101 |
| 2 | b | 5 | 101 |
| 3 | c | 3 | 101 |
| 4 | c | 3 | 101 |
| 5 | d | 3 | 101 |
| 7 | b | 4 | 102 |
+---------+----------+----------+-----------+
Course Table
+-----------+-------------+
| Course_no | Course_name |
+-----------+-------------+
| 101 | BCA |
| 102 | BSC |
+-----------+-------------+
For the First Part I expect a table like:
+-----------+-------------+
| Course_no | Course_name |
+-----------+-------------+
| 101 | BCA |
+-----------+-------------+
For the Second part I expect a table like
+-----------+----------------------+
| Course_no | Course_name |Total |
+-----------+-------------+--------+
| 101 | BCA |5 |
| 102 | BSC |1 |
+-----------+-------------+--------+

First Part:
You need to use JOIN and HAVING
SELECT c.Course_no,c.Course_name
FROM Course c
INNER JOIN Student s on c.Course_no = s.Course_id
GROUP BY c.Course_no,c.Course_name
HAVING count(1) > 2
sqlfiddle:http://sqlfiddle.com/#!9/387386/3
Second part:
You need to JOIN and GROUP BY COUNT
SELECT c.Course_no,c.Course_name,count(1) 'totle'
FROM Course c
INNER JOIN Student s on c.Course_no = s.Course_id
GROUP BY c.Course_no,c.Course_name
sqlfiddle:http://sqlfiddle.com/#!9/bad7e3/1

Related

SQL search merge

I'm trying to merge two select sentence. Is it possible ?
I have three tables :
score
+------+---------+-------+
| ID | SUBJECT | SCORE |
+------+---------+-------+
| 1 | Chinese | 65 |
| 1 | English | 75 |
| 2 | Chinese | 60 |
| 2 | English | 70 |
| 3 | Chinese | 80 |
| 3 | English | 50 |
+------+---------+-------+
student
+------+----------+--------+
| ID | CLASS_ID | NAME |
+------+----------+--------+
| 1 | 1 | TOM |
| 2 | 1 | ANNA |
| 3 | 2 | JOHN |
+------+----------+--------+
class
+------+----------+
| ID | NAME |
+------+----------+
| 1 | 5th |
| 2 | 6th |
+------+----------+
mysql> select class.NAME as CLASS_NAME, student.NAME from class inner join student on student.CLASS_ID = class.ID;
+------------+--------+
| CLASS_NAME | NAME |
+------------+--------+
| 5th | TOM |
| 6th | ANNA |
| 6th | JOHN |
+------------+--------+
mysql> select SUM(SCORE) as total from score group by ID;
+-------+
| total |
+-------+
| 140 |
| 130 |
| 130 |
+-------+
Could I merge two select sentence let it be
+------------+--------+-------+
| CLASS_NAME | NAME | total |
+------------+--------+-------+
| 5th | TOM | 140 |
| 6th | ANNA | 130 |
| 6th | JOHN | 130 |
+------------+--------+-------+
Or is there any better search sentence to do this well?
I try use two sentence to merge , but can't have a good idea.
mysql> select class.NAME as CLASS_NAME, student.NAME from class inner join student on student.CLASS_ID = class.ID;
mysql> select SUM(SCORE) as total from score group by ID;
hope it can be merge success or have another answer to do this well.
You can use inner join to merge the second query. Below query takes in consideration that the join condition will be score.id with student.id
select c.name as class_name,
st.name ,
sc.total
from class c
inner join student st on st.class_id = c.id
inner join ( select id,
SUM(SCORE) as total
from score
group by id
) as sc on sc.id=st.id ;
https://dbfiddle.uk/tauTGBFO

Select rows that has common relationship

I have the following table:
Book
| id | book_title |
|----|------------|
| 1 | a |
| 2 | b |
| 3 | c |
| 4 | d |
Teacher:
| id | teacher_name |
|----|--------------|
| 1 | a |
| 2 | b |
| 3 | c |
| 4 | d |
Education stage:
| id | education_stage_name |
|----|----------------------|
| 1 | a |
| 2 | b |
| 3 | c |
| 4 | d |
The relationship between book and education stage:
| book_id | education_stage_id |
|---------|--------------------|
| 1 | 1 |
| 1 | 2 |
| 2 | 3 |
| 3 | 4 |
And the relationship between book and teacher:
| book_id | teacher_id |
|---------|------------|
| 1 | 1 |
| 1 | 4 |
| 2 | 3 |
| 3 | 4 |
Now I want to select books, which has the same education stages as a teacher with id=1
So the result table should look like this:
| book_id | title |
|---------|-------|
| 1 | a |
Because only the education stage with id=1 is assigned to the book and the teacher with id=1 at the same time.
Here is the fiddle, but I don't know even how to start with this query.
Is there anyone that has some ideas on how to write this query?
Is it possible to write it with DQL language?
This doesn't take anything complex, if all records will always exist, you can inner join on all tables:
select
Book.id as book_id,
Book.book_title,
Teacher.id as teacher_id,
Teacher.teacher_name
from Book
inner join book_education_stage bes on bes.book_id = Book.id
inner join Education_Stage es on es.id = bes.education_stage_id
inner join teacher_education_stage tes on tes.book_id = Book.id
inner join Teacher on tes.teacher_id = Teacher.id
Result:
book_id
book_title
teacher_id
teacher_name
1
a
1
a
1
a
1
a
1
a
4
d
1
a
4
d
2
b
3
c
3
c
4
d
Here is the DB Fiddle if you're interested.

SQL exists 2 table

i have 2 table like this
student
|-----------------------|
| id | name | value |
|-----------------------|
| F01 | Ruben | 4 |
| F02 | Dani | 2 |
| F03 | Mike | 3 |
| F04 | John | 4 |
|-----------------------|
tutor
|---------------------------------|
| id | code | student_id | class |
|---------------------------------|
| 1 | S2244 | F01 | IF-B |
| 2 | S3251 | F02 | IF-B |
| 3 | S2244 | F03 | IF-A |
| 4 | S2244 | F04 | IF-C |
|---------------------------------|
note, tutor.code ( S2244 and S3251) is foreign key from another table, tutor.class ( IF-B, IF-A, IF-C ) is foreign key from another table too, tutor.student_id is foreign key from student table, how to make the two tables combined and produce a result as below?
|-------------------------------|
| id | name | value | class |
|-------------------------------|
| F01 | Ruben | 4 | IF-B |
| F03 | Mike | 3 | IF-A |
| F04 | John | 4 | IF-C |
|-------------------------------|
I want to take all the columns in the student table and combine them with the "class" column in the tutor table.
I have tried it myself but can only display data from the student table, and the "class" column of the tutor table cannot appear, this is the query I made
select s.*
from students s
where exists (
select 1 from tutor t where t.student_id = s.student_id and t.code = 'S2244'
)
I would just use an inner join here:
SELECT
s.id,
s.name,
s.value,
t.class
FROM student s
INNER JOIN tutor t
ON s.id = t.student_id
WHERE
t.code = 'S2244';

mySQL SELECT query from multiple tables

I need a help with mySQL SELECT query from multiple tables. I have got four tables: school, discipline, pupils and teams.
School table looks like:
+------+---------+---------------+----------+
| id | name | discipline_id | pupil_id |
+------+---------+---------------+----------+
| 1 | one | 2 | 5 |
+------+---------+---------------+----------+
| 2 | two | 3 | 8 |
+------+---------+---------------+----------+
| 3 | three | 4 | 12 |
+------+---------+---------------+----------+
Discipline table looks like:
+------+---------+
| id | name |
+------+---------+
| 1 | math |
+------+---------+
| 2 | bio |
+------+---------+
| 3 | liter |
+------+---------+
| 4 | geo |
+------+---------+
Teams table looks like:
+------+---------+---------------+-----------+
| id | name | school_id | member_id |
+------+---------+---------------+-----------+
| 1 | T1 | 1 | 3 |
+------+---------+---------------+-----------+
| 2 | T2 | 3 | 3 |
+------+---------+---------------+-----------+
| 3 | T3 | 2 | 9 |
+------+---------+---------------+-----------+
The result of disciplines I need to get with a "SELECT from discipline..." query by "member_id = 3" is:
+-----------------+---------------+
| discipline_name | discipline_id |
+-----------------+---------------+
| bio | 2 |
+-----------------+---------------+
| geo | 4 |
+-----------------+---------------+
By matching member's school and then getting its discipline, if it makes sense...Is it possible to do with just one mySQL query?
Type of: member_id 3 => school_id 1,3 => discipline_id = show related disciplines names and ids which are 2, 4
Thank you very much...
Your goal is not clear or makes no sense to me.
But here is what you are literally asking for:
SELECT
s.discipline_id
d.name
FROM teams t
LEFT JOIN school s
ON s.id = t.school_id
LEFT JOIN discipline d
ON d.id = s.discipline_id
WHERE t.member_id = 3

How to select table and count other table

Suppose I have two tables:
Customers
Name | id |
-------------------
Benny | 1 |
Wilson | 2 |
Joe | 3 |
Austin | 4 |
Orders
Product | id |
---------------------
TV | 1 |
Hifi-set | 1 |
HTPC | 1 |
CD | 1 |
DVD | 1 |
CD | 1 |
DVD | 1 |
And this is what I want with the results:
Name | Orders |
-------------------
Benny | 7 |
Wilson | 0 |
Joe | 0 |
Austin | 0 |
I'm not familiar with SQL, But I tried:
SELECT c.Name FROM Customers AS c LEFT JOIN Orders AS o ON c.id=o.id GROUP BY c.Name
But got a wrong result:
Name | Orders |
-------------------
Benny | 4 |
Wilson | 1 |
Joe | 1 |
Austin | 1 |
What do I do?
Try:
select
c.Name,
(select count(1) from Orders where ID=c.ID)
from
Customers as c
By not using SubQuery, you can also JOIN instead.
SELECT a.Name, COUNT(b.id)
FROM Customers a LEFT JOIN Orders b
on a.ID = b.ID
GROUP BY a.Name