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
Related
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;
Suppose I have a SELECT query of some sort which shows me 10 rows.
Is there a way I can change this query so that it shows each row twice, giving me 20 in total?
Furthermore, can some arbitrary identifier be added to the SELECT to allow distinguishing each pair of rows?
This should work:
select a, b, c, 1 as pair from myTable
union all
select a, b, c, 2 as pair from myTable
Yes you can do so with a UNION ALL. Just run.
<Your Query>
UNION ALL
<Your Query>
To elaborate on #cdaiga's answer and include a unique identifier per row...
SET #i := 0;
SELECT <your columns>, #i := #i + 1 as num from ...
UNION ALL
SELECT <your columns>, #i := #i + 1 as num from ...
Then you'll have a num column with a consecutive sequence number. Here's an example: fiddle.
Select columns twice with arbitrary identifier then unpivot/convert to rows using union all
for example given
MariaDB [sandbox]> select fb_uid,id,username from users;
+--------+----+----------+
| fb_uid | id | username |
+--------+----+----------+
| 1 | 1 | John |
| 2 | 2 | Jane |
| 3 | 3 | Ali |
| 6 | 6 | Bruce |
| 7 | 7 | Martha |
| 8 | 8 | Sidney |
| 10 | 10 | charlie |
| 12 | 12 | Elisa |
| 14 | 14 | Samantha |
| 15 | 15 | Hannah |
| 16 | 16 | Hannah |
| 17 | 17 | Kevin |
| 18 | 18 | Kevin |
| 19 | 19 | Ruth |
+--------+----+----------+
14 rows in set (0.00 sec)
select fb_uid,id1 id,uname1 username
from
(
select fb_uid,id id1,username uname1, id id2, username uname2 from users u
) u1
union all
select fb_uid,id2,uname2
from
(select fb_uid,id id1,username uname1, id id2, username uname2 from users u
) u2
order by id
result
+--------+----+----------+
| fb_uid | id | username |
+--------+----+----------+
| 1 | 1 | John |
| 1 | 1 | John |
| 2 | 2 | Jane |
| 2 | 2 | Jane |
| 3 | 3 | Ali |
| 3 | 3 | Ali |
| 6 | 6 | Bruce |
| 6 | 6 | Bruce |
| 7 | 7 | Martha |
| 7 | 7 | Martha |
| 8 | 8 | Sidney |
| 8 | 8 | Sidney |
| 10 | 10 | charlie |
| 10 | 10 | charlie |
| 12 | 12 | Elisa |
| 12 | 12 | Elisa |
| 14 | 14 | Samantha |
| 14 | 14 | Samantha |
| 15 | 15 | Hannah |
| 15 | 15 | Hannah |
| 16 | 16 | Hannah |
| 16 | 16 | Hannah |
| 17 | 17 | Kevin |
| 17 | 17 | Kevin |
| 18 | 18 | Kevin |
| 18 | 18 | Kevin |
| 19 | 19 | Ruth |
| 19 | 19 | Ruth |
+--------+----+----------+
28 rows in set (0.00 sec)
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
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 .
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)