I have three tables college_details, college_courses, course of which college_courses is an intermediate table for many to many relation ships between college_details and course.
college_details
+------------+--------------+
| college_id | college_name |
+------------+--------------+
| 1 | abc, Nagpur |
| 2 | xyz, Nagpur |
| 3 | mnop, Nagpur |
+------------+--------------+
college_courses
+------------+--------------+
| college_id | course_id |
+------------+--------------+
| 1 | 1 |
| 1 | 5 |
| 2 | 3 |
| 3 | 5 |
+---------------------------+
course
+----------+-------------+
|course_id | course_name |
+----------+-------------+
| 1 | B.E |
| 2 | M.E |
| 3 | M.Tech |
| 4 | B.Tech |
| 5 | M.B.A |
+----------+-------------+
SELECT cd.college_id
, cd.college_name
, c.course_name
FROM college_details AS cd
LEFT
JOIN college_courses AS cc
ON cc.college_id = cd.college_id
LEFT
JOIN course AS c
ON cc.course_id = c.course_id
From the above query I get the following table.
+------------+---------------+-------------+
| college_id | college_name | course_name |
+------------+---------------+-------------+
| 1 | abc, college | be |
| 1 | abc, college | mba |
| 2 | xyz, college | me |
| 3 | mnop, college | btech |
+------------+---------------+-------------+
query -> "SELECT cd.college_id,cd.college_name,GROUP_CONCAT(c.course_name) FROM college_details AS cd LEFT JOIN college_courses AS cc ON cc.college_id = cd.college_id LEFT JOIN course AS c ON cc.college_id = c.course_id ";
And from above query I get the following table.
+------------+--------------+-----------------------------+
| college_id | college_name | GROUP_CONCAT(c.course_name) |
+------------+--------------+-----------------------------+
| 1 | abc, college | be,be,me,btech |
+------------+--------------+-----------------------------+
But what I want is the following table.
+------------+---------------+-------------+
| college_id | college_name | course_name |
+------------+---------------+-------------+
| 1 | abc, college | be,mba |
| 2 | xyz, college | me |
| 3 | mnop, college | btech |
+------------+---------------+-------------+
Related
I have two tables in my database.
Men table:
+----------------------------------------
| ID | menname | partner |
+----------------------------------------
| 1 | Mark | 1 |
| 2 | Adam | 2 |
----------------------------------------+
Women table:
+---------------------------------------+
| ID | womenname |
+---------------------------------------+
| 1 | Lisa |
| 2 | Emma |
+---------------------------------------+
When I do a inner join, I get:
+----------------------------------------
| ID | menname | partner |
+----------------------------------------
| 1 | Mark | 1 |
| 2 | Adam | 2 |
----------------------------------------+
instead of:
+----------------------------------------
| ID | menname | partner |
+----------------------------------------
| 1 | Mark | Lisa |
| 2 | Adam | Emma |
----------------------------------------+
It gets the id, instead of the partner name.
Anyone know what is wrong with my query?
SELECT m.ID
,m.menname
,w.womenname AS partner
FROM Men AS m
INNER JOIN Women AS w ON m.partner = w.ID;
student table
|----------------------|
| student_id | name |
|------------|---------|
| 1 | Richard |
| 2 | Emily |
| 3 | Hans |
|------------|---------|
lecturer table
|--------------------|
| lecturer_id | name |
|-------------|------|
| 1 | John |
| 2 | Mike |
|-------------|------|
classes table
|-----------------------------------------------|
| class_id | lecturer_id | material |
|----------|-------------|----------------------|
| 1 | 1 | Basic of algorithm |
| 2 | 1 | Basic of programming |
| 3 | 2 | Database Essentials |
| 4 | 2 | Basic of SQL |
|----------|-------------|----------------------|
attendance table
|-----------------------|
| class_id | student_id |
|----------|------------|
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 1 |
| 2 | 2 |
| 3 | 1 |
| 3 | 2 |
| 3 | 3 |
| 4 | 1 |
| 4 | 2 |
|----------|------------|
how to show classes records (from classes table) that not attended by Hans (student) in MySQL?
desired result :
|-----------------------------------------------|
| class_id | lecturer_id | material |
|----------|-------------|----------------------|
| 2 | 1 | Basic of programming |
| 4 | 2 | Basic of SQL |
|----------|-------------|----------------------|
One approach uses EXISTS:
SELECT c.class_id, c.lecturer_id, c.material
FROM classes c
WHERE NOT EXISTS (SELECT 1 FROM attendance a
INNER JOIN student s
ON a.student_id = s.student_id
WHERE a.class_id = c.class_id AND
s.name = 'Hans');
Using joins -
select c.class_id
from attendance a inner join student s on (a.student_id=s.student_id and s.student_id='Hans')
right outer join classes c on (a.class_id=c.class_id)
where a.class_id is null
My problem is I want to count all the students under n department.
Note: Students has a course and courses has their department (there are many courses in 1 department).
SAMPLE OUTPUT:
+---------+-------------------------------+
| student | department |
+---------+-------------------------------+
| 23| Computer Education Department |
| 67| Basic Education Department |
| 39| Mathematics Department |
| 40| Humanities Department |
| 61| Engineering Department |
| 79| Management Department |
+---------+-------------------------------+
tbl_students
+---------+---------------+--------+
| stud_id | name | course |
+---------+---------------+--------+
| 1 | Jack Owen | 1 |
| 2 | Kirby Lopez | 2 |
| 3 | Justin Minus | 1 |
| 4 | Jerome Noveda | 1 |
+---------+---------------+--------+
2. tbl_courses
+-----------+------------+---------+
| course_id | short_name | dept_id |
+-----------+------------+---------+
| 1 | BSIT | 1 |
| 2 | BSCS | 1 |
| 3 | BEED | 2 |
| 4 | BSED | 2 |
| 6 | BSTHRM | 7 |
| 7 | BLIS | 4 |
| 8 | BSCE | 6 |
+-----------+------------+---------+
3. tbl_department
+---------+-------------------------------+
| dept_id | full_name |
+---------+-------------------------------+
| 1 | Computer Education Department |
| 2 | Basic Education Department |
| 3 | Mathematics Department |
| 4 | Humanities Department |
| 6 | Engineering Department |
| 7 | Management Department |
+---------+-------------------------------+
I think you can do something like this:
SELECT
tbl_department.full_name as department,
COUNT(DISTINCT tbl_students.stud_id) AS student
FROM
tbl_department
JOIN tbl_courses
ON tbl_department.dept_id = tbl_courses.dept_id
JOIN tbl_students
ON tbl_courses.course_id = tbl_students.course
GROUP BY
tbl_department.full_name
SELECT
ISNULL(( SELECT COUNT(*) FROM tbl_courses C
INNER JOIN tbl_students S ON C.course_id = S.course
WHERE C.dept_id = D.dept_id
),0) AS student
,D.full_name AS department
FROM
tbl_department D
I hope that it will work for you.
For each student, find the number of courses they take and sort
the rows in descending order. (e.g. student id, the number of
courses taken by that student)
STUDENT TABLE
| ID | name | dept_name | tot_cred |
| S0901 | Alice | Comp.Sci. | 83 |
| S0902 | Martha | Comp.Sci. | 75 |
| S0903 | Micheal | Comp.Sci. | 45 |
| S0904 | Rose | Comp.Sci. | 77 |
| S0905 | Alfie | Comp.Sci. | 91 |
| S1901 | Brad | Biology | 23 |
TAKES TABLE
| ID | course_id | sec_id | semester | year | grade
| S0901 | CS-101 | 1 | Fall | 2009 | A
| S0901 | CS-315 | 1 | Spring | 2010 | B+
| S0901 | HIS-351 | 1 | Spring | 2010 | A-
| S0901 | MTH-101 | 1 | Fall | 2009 | A
| S0901 | MTH-102 | 1 | Spring | 2009 | B+
| S0902 | CS-101 | 1 | Fall | 2009 | A
| S0902 | CS-315 | 1 | Spring | 2010 | B+
| S0902 | CS-319 | 1 | Spring | 2010 | B
| S0902 | HIS-351 | 1 | Spring | 2010 | A-
| S0902 | MTH-101 | 1 | Fall | 2009 | A
| S0902 | MTH-102 | 1 | Spring | 2009 | B+
| S1901 | CS-101 | 1 | Fall | 2009 | B+
| S1901 | CS-190 | 1 | Spring | 2009 | C
| S1901 | CS-315 | 1 | Spring | 2010 | A-
| S1901 | HIS-351 | 1 | Spring | 2010 | A-
Technically, #Marcinek's answer may not be sufficient because it omits students who take zero classes. I would use this instead:
SELECT STUDENT.ID, COUNT(TAKES.ID)
FROM STUDENT LEFT JOIN TAKES ON STUDENT.ID = TAKES.ID
GROUP BY STUDENT.ID
ORDER BY COUNT(TAKES.ID) DESC;
By using LEFT JOIN, you can capture a student whose ID does not appear in the TAKES table.
Just join and group up the result.
SELECT COUNT(*), s.id FROM student s, takes t where t.id = s.id group by s.id order by count(*) desc
I have these 3 tables like that:
lecturers:
+-------------+---------+
| id-lecturer | name |
+-------------+---------+
| 1 | Johnson |
| 2 | Smith |
| ... | ... |
| ... | ... |
+-------------+---------+
subjects:
+------------+---------+
| id-subject | name |
+------------+---------+
| 1 | Math |
| 2 | Physics |
| ... | ... |
| ... | ... |
+------------+---------+
exams:
+---------+-------------+------------+------------+
| id-exam | id-lecturer | id-subject | date |
+---------+-------------+------------+------------+
| 1 | 5 | 1 | 1990-05-05 |
| 2 | 7 | 1 | ... |
| 3 | 5 | 3 | ... |
| ... | ... | ... | ... |
+---------+-------------+------------+------------+
When i try to do the first SELECT:
SELECT e.`id-lecturer`, e.`id-subject`, COUNT(e.`id-lecturer`) AS `exams-num`
FROM exams e
JOIN subjects s ON e.`id-subject`=s.`id-subject`
JOIN lecturers l ON e.`id-lecturer`=l.`id-lecturer`
GROUP BY e.`id-lecturer`, e.`id-subject`
I get the right answer. It shows something like that:
+-------------+------------+-----------+
| id-lecturer | id-subject | exams-num |
+-------------+------------+-----------+
| 0001 | 1 | 4 |
| 0001 | 3 | 1 |
| 0001 | 4 | 1 |
| 0001 | 5 | 1 |
| 0002 | 1 | 2 |
| 0002 | 2 | 1 |
| 0002 | 4 | 1 |
| 0002 | 6 | 3 |
+-------------+------------+-----------+
Now i want to show only the max number for every lecturer, my code is:
SELECT it.`id-lecturer`, it.`id-subject`, MAX(it.`exams-num`) AS `exams-number`
FROM (
SELECT e.`id-lecturer`, e.`id-subject`, COUNT(e.`id-lecturer`) AS `exams-num`
FROM egzaminy e
JOIN subjects s ON e.`id-subject`=s.`id-subject`
JOIN lecturers l ON e.`id-lecturer`=l.`id-lecturer`
GROUP BY e.`id-lecturer`, e.`id-subject`) it
GROUP BY it.`id-lecturer`
output:
+-------------+------------+--------------+
| id-lecturer | id-subject | exams-number |
+-------------+------------+--------------+
| 0001 | 1 | 4 |
| 0002 | 1 | 3 |
| 0003 | 1 | 2 |
| 0004 | 1 | 5 |
| 0005 | 2 | 1 |
+-------------+------------+--------------+
I get the correct numbers of the max values for each lecturer, but the subjects id doesn't match, it always takes the first row's id. How can I make it to match correctly these two fields in every row?
I guess you can simply use the same query for further conditions like below.
Select t.Lecturer_id,max(t.exams-num) from
(SELECT e.id-lecturer as Lecturer_id, e.id-subject as Subject_id,
COUNT(e.id-lecturer) AS exams-num
FROM exams e
JOIN subjects s ON e.id-subject=s.id-subject
JOIN lecturers l ON e.id-lecturer=l.id-lecturer
GROUP BY e.id-lecturer, e.id-subject ) as t
group by t.Lecturer_id