mysql joins using group by - mysql

These are my tables
mysql> select * from professor;
+-------+--------+--------+--------+------+
| empid | name | status | salary | age |
+-------+--------+--------+--------+------+
| 1 | Arun | 1 | 2000 | 23 |
| 2 | Benoy | 0 | 3000 | 25 |
| 3 | Chacko | 1 | 1000 | 36 |
| 4 | Divin | 0 | 5000 | 32 |
| 5 | Edwin | 1 | 2500 | 55 |
+-------+--------+--------+--------+------+
5 rows in set (0.00 sec)
mysql> select * from works;
+----------+-------+---------+
| courseid | empid | classid |
+----------+-------+---------+
| 1 | 1 | 10 |
| 2 | 2 | 9 |
| 3 | 3 | 8 |
| 4 | 4 | 10 |
| 5 | 5 | 9 |
| 6 | 1 | 9 |
| 2 | 3 | 10 |
| 2 | 1 | 7 |
| 4 | 2 | 6 |
| 7 | 5 | 6 |
| 3 | 5 | 2 |
| 2 | 4 | 6 |
| 2 | 5 | 2 |
+----------+-------+---------+
15 rows in set (0.00 sec)
mysql> select * from course;
+----------+------------+--------+
| courseid | coursename | points |
+----------+------------+--------+
| 1 | Maths | 4 |
| 2 | Science | 4 |
| 3 | English | 85 |
| 4 | Social | 4 |
| 5 | Malayalam | 99 |
| 6 | Arts | 40 |
| 7 | Biology | 100 |
+----------+------------+--------+
7 rows in set (0.00 sec)
Question is :
Return the names of full professors who have taught at least two courses in one Class
My query is :
select professor.name from professor
inner join works
on professor.empid=works.empid
group by works.empid
having count(distinct works.courseid)>=2
The ouput I get now is :
Arun
Benoy
Chacko
Divin
Edwin
I am supposed to get the ouput as 'Edwin' as he is the only person who has taught 2 subjects in the same class. Pls help

you must consider your group by is wrong change it to this
group by classid,works.empid
like that you will count courseid in classid .

Related

How to select multiple values for one table "cell" MySQL

For all players, I need to find the player number and a list of the numbers of teams for which they have ever played.
Here is the table "MATCHES":
+---------+--------+----------+-----+------+
| MATCHNO | TEAMNO | PLAYERNO | WON | LOST |
+---------+--------+----------+-----+------+
| 1 | 1 | 6 | 3 | 1 |
| 2 | 1 | 6 | 2 | 3 |
| 3 | 1 | 6 | 3 | 0 |
| 4 | 1 | 44 | 3 | 2 |
| 5 | 1 | 83 | 0 | 3 |
| 6 | 1 | 2 | 1 | 3 |
| 7 | 1 | 57 | 3 | 0 |
| 8 | 1 | 8 | 0 | 3 |
| 9 | 2 | 27 | 3 | 2 |
| 10 | 2 | 104 | 3 | 2 |
| 11 | 2 | 112 | 2 | 3 |
| 12 | 2 | 112 | 1 | 3 |
| 13 | 2 | 8 | 0 | 3 |
+---------+--------+----------+-----+------+
The best I could come up with was:
SELECT DISTINCT playerno, teamno
FROM matches
ORDER BY playerno;
which results in:
+----------+--------+
| playerno | teamno |
+----------+--------+
| 2 | 1 |
| 6 | 1 |
| 8 | 1 |
| 8 | 2 |
| 27 | 2 |
| 44 | 1 |
| 57 | 1 |
| 83 | 1 |
| 104 | 2 |
| 112 | 2 |
+----------+--------+
Notice how player 8 has played on two teams. How can I get the table to show only one row for player 8 and a list of teamno's (1 & 2)?
You could use the group_concat aggregate function:
SELECT playerno, GROUP_CONCAT(DISTINCT teamno)
FROM matches
GROUP BY playerno
ORDER BY playerno;
You could use group_concat
SELECT playerno, group_concat( teamno)
FROM matches
GROUP BY playerno;

how to select a list from random id to the last

im new with SQL. i know to how select a list with limit comand. but that way need a value to select. what if i want select a list from random id to the last. exemple:
I want to select a list with id from 4 -> last of row (cuz i dont know whats last id)
select * from thing1 where id>=4 order by rand();
where thing1 is your table name. How you seed your random number generator (RNG) is up to you.
+----+---------+------------+
| id | conn_id | read_date |
+----+---------+------------+
| 11 | 3 | 2013-02-21 |
| 5 | 1 | 2012-02-21 |
| 8 | 5 | 2010-12-21 |
| 15 | 7 | 2019-12-21 |
| 14 | 6 | 2019-12-21 |
| 13 | 5 | 2016-02-21 |
| 4 | 2 | 2010-12-21 |
| 7 | 2 | 2014-02-21 |
| 6 | 2 | 2007-12-21 |
| 12 | 4 | 2014-02-21 |
| 16 | 8 | 2010-12-21 |
| 9 | 3 | 2010-12-21 |
| 10 | 4 | 2010-12-21 |
+----+---------+------------+
13 rows in set (0.14 sec)
mysql> select * from thing1 where id>=4 order by rand();
+----+---------+------------+
| id | conn_id | read_date |
+----+---------+------------+
| 13 | 5 | 2016-02-21 |
| 6 | 2 | 2007-12-21 |
| 10 | 4 | 2010-12-21 |
| 16 | 8 | 2010-12-21 |
| 14 | 6 | 2019-12-21 |
| 5 | 1 | 2012-02-21 |
| 7 | 2 | 2014-02-21 |
| 11 | 3 | 2013-02-21 |
| 12 | 4 | 2014-02-21 |
| 4 | 2 | 2010-12-21 |
| 8 | 5 | 2010-12-21 |
| 9 | 3 | 2010-12-21 |
| 15 | 7 | 2019-12-21 |
+----+---------+------------+
13 rows in set (0.02 sec)
mysql> select * from thing1 where id>=4 order by rand();
+----+---------+------------+
| id | conn_id | read_date |
+----+---------+------------+
| 10 | 4 | 2010-12-21 |
| 4 | 2 | 2010-12-21 |
| 6 | 2 | 2007-12-21 |
| 7 | 2 | 2014-02-21 |
| 5 | 1 | 2012-02-21 |
| 9 | 3 | 2010-12-21 |
| 12 | 4 | 2014-02-21 |
| 16 | 8 | 2010-12-21 |
| 8 | 5 | 2010-12-21 |
| 15 | 7 | 2019-12-21 |
| 13 | 5 | 2016-02-21 |
| 14 | 6 | 2019-12-21 |
| 11 | 3 | 2013-02-21 |
+----+---------+------------+
13 rows in set (0.05 sec)
Stored Proc
To have starting random position, until the end, random ordering
-- drop procedure getRandomStartToEnd;
delimiter $$
create procedure getRandomStartToEnd()
BEGIN
declare theCount int;
declare theStart int;
select count(*) into theCount from thing1;
set #theStart:=floor((rand()*#theCount)+1);
select * from thing1 where id>=#theStart order by rand();
END
$$
call getRandomStartToEnd; -- call stored proc

fetching required data using joins in mysql

My tables :
mysql> select * from professor;
+-------+--------+--------+--------+------+
| empid | name | status | salary | age |
+-------+--------+--------+--------+------+
| 1 | Arun | 1 | 2000 | 23 |
| 2 | Benoy | 0 | 3000 | 25 |
| 3 | Chacko | 1 | 1000 | 36 |
| 4 | Divin | 0 | 5000 | 32 |
| 5 | Edwin | 1 | 2500 | 55 |
| 7 | George | 0 | 1500 | 46 |
+-------+--------+--------+--------+------+
6 rows in set (0.00 sec)
mysql> select * from works;
+----------+-------+---------+
| courseid | empid | classid |
+----------+-------+---------+
| 1 | 1 | 10 |
| 2 | 2 | 9 |
| 3 | 3 | 8 |
| 4 | 4 | 10 |
| 5 | 5 | 9 |
| 6 | 1 | 9 |
| 2 | 3 | 10 |
| 2 | 1 | 7 |
| 4 | 2 | 6 |
| 2 | 4 | 6 |
| 2 | 5 | 2 |
| 7 | 5 | 6 |
| 3 | 5 | 2 |
| 6 | 4 | 10 |
+----------+-------+---------+
14 rows in set (0.00 sec)
mysql> select * from course;
+----------+------------+--------+
| courseid | coursename | points |
+----------+------------+--------+
| 1 | Maths | 5 |
| 2 | Science | 1 |
| 3 | English | 6 |
| 4 | Social | 4 |
| 5 | Malayalam | 20 |
| 6 | Arts | 25 |
| 7 | Biology | 20 |
+----------+------------+--------+
7 rows in set (0.00 sec)
Question is :
Return the name(s) of the professor(s) who taught the most number of
courses in Class 10
Query i tried is :
select professor.name,works.courseid,works.empid,works.classid from professor
inner join works
on professor.empid=works.empid
where works.classid=10
group by works.courseid
I know its imcomplete/incorrect. Pls help me to the required result.
select
professor.name, count(works.courseid)
from
works
inner join
professor on
professor.empid = works.empid
where
work.classid = 10
group by
professor.name
order by count(works.courseid) desc
limit 1
change this in your select statment
works.courseid
to
count(works.courseid) as courseid

What is self join in mysql used for

What is this self join and why do we need this self join?. I have till date never used self joins.
See if these links helps you...
http://www.udel.edu/evelyn/SQL-Class3/SQL3_self.html
http://awads.net/wp/2006/07/11/back-to-basics-self-joins/
http://www.sqltutorial.org/sqlselfjoin.aspx
Good Luck!!!
there are number of reasons, and tons of examples are available on web
http://www.udel.edu/evelyn/SQL-Class3/SQL3_self.html
mysql> SELECT * FROM pr WHERE id>80;
+----+------+--------+
| id | ids | status |
+----+------+--------+
| 81 | 4 | 4 |
| 82 | 2 | 3 |
| 83 | 2 | 4 |
+----+------+--------+
3 rows in set (0.00 sec)
mysql> SELECT * FROM pr WHERE id<18;
+----+------+--------+
| id | ids | status |
+----+------+--------+
| 1 | 1 | 1 |
| 5 | NULL | 2 |
+----+------+--------+
2 rows in set (0.01 sec)
identical requests :
mysql> SELECT * FROM pr AS t1 ,pr AS t2 WHERE t1.id<18 AND t2.id>80;
+----+------+--------+----+------+--------+
| id | ids | status | id | ids | status |
+----+------+--------+----+------+--------+
| 1 | 1 | 1 | 81 | 4 | 4 |
| 5 | NULL | 2 | 81 | 4 | 4 |
| 1 | 1 | 1 | 82 | 2 | 3 |
| 5 | NULL | 2 | 82 | 2 | 3 |
| 1 | 1 | 1 | 83 | 2 | 4 |
| 5 | NULL | 2 | 83 | 2 | 4 |
+----+------+--------+----+------+--------+
6 rows in set (0.00 sec)
mysql> SELECT * FROM pr AS t1 JOIN pr AS t2 ON t1.id<18 AND t2.id>80;
+----+------+--------+----+------+--------+
| id | ids | status | id | ids | status |
+----+------+--------+----+------+--------+
| 1 | 1 | 1 | 81 | 4 | 4 |
| 5 | NULL | 2 | 81 | 4 | 4 |
| 1 | 1 | 1 | 82 | 2 | 3 |
| 5 | NULL | 2 | 82 | 2 | 3 |
| 1 | 1 | 1 | 83 | 2 | 4 |
| 5 | NULL | 2 | 83 | 2 | 4 |
+----+------+--------+----+------+--------+
6 rows in set (0.00 sec)

join with a group by?

i have a table called rc_language_type_table with:
id language
1 english
2 Xhosa
3 afrikaans
etc
then i have a table rc_language_type_assoc_table with:
profile_id | language_type_id |
+------------+------------------+
| 3 | 1 |
| 13 | 1 |
| 15 | 1 |
| 16 | 1 |
where i have profiles and each profile is connected to a language id in a 1 to many
so then i did:
select *,count(*) from rc_language_type_assoc_table group by language_type_id;
+------------+------------------+----------+
| profile_id | language_type_id | count(*) |
+------------+------------------+----------+
| 3 | 1 | 96 |
| 3 | 2 | 19 |
| 3 | 3 | 18 |
| 64 | 4 | 51 |
| 94 | 5 | 10 |
| 37 | 6 | 26 |
| 3 | 7 | 21 |
| 3 | 8 | 4 |
| 3 | 9 | 6 |
| 88 | 10 | 4 |
| 3 | 11 | 3 |
+------------+------------------+----------+
what i want now is: instead having the language_type_id i want to display the actual language...how would i do this please???
i tried:
select *, count(*)
from rc_language_type_assoc_table, rc_language_type_table
group by language_type_id
where rc_language_type_assoc_table.language_type_id = rc_language_type_table.id;
but i get a syntax error...
please help??
thank you
GROUP BY should be "after" the WHERE statement and not before
select *, count(*)
from rc_language_type_assoc_table, rc_language_type_table
where rc_language_type_assoc_table.language_type_id = rc_language_type_table.id
group by language_type_id ;