How to find most popular genre of music? - mysql

So I have two tables, table 1:Music Styles being with fields with StyleID and StyleName, with StyleName being a genre and table 2:entertainers with fields SyleID and EntertainerID.
I need help finding is:
1) What’s the most popular genre of music style in the database of entertainers?
This is what i have so far:
SELECT StyleID, StyleName
from EA_Music_Styles
WHERE StyleID = (SELECT StyleID
from EA_Entertainer_Styles
WHERE StyleID = (SELECT MAX(StyleID)
FROM EA_Entertainer_Styles));
But i got an error code of "Subquery returns more than 1 row"
I need help, don't know exactly what to do? First time beginner.
Here are the tables:
Table 1:
StyleID StyleN
1 40's Ballroom Music
2 50's Music
3 60's Music
4 70's Music
5 80's Music
6 Country
7 Classical
8 Classic Rock & Roll
9 Rap
10 Contemporary
11 Country Rock
12 Elvis
13 Folk
14 Chamber Music
15 Jazz
16 Karaoke
17 Motown
18 Modern Rock
19 Rhythm and Blues
20 Show Tunes
21 Standards
22 Top 40 Hits
23 Variety
24 Salsa
25 90's Music
Table 2:
StyleID EntertainerID
3 1003
3 1008
4 1010
6 1007
6 1008
7 1009
7 1011
7 1012
8 1003
10 1001
10 1013
11 1007
13 1004
13 1012
14 1009
14 1011
15 1005
15 1013
17 1002
19 1002
19 1005
20 1001
20 1011
21 1001
21 1009
21 1010
22 1006
22 1010
23 1002
23 1006
24 1005
24 1006
SO the result would output the two StyleID's, 7 & 21 and StyleName which would be Classical & Standards

You just need to join your two tables and get the max count for entertainers
SELECT ms.StyleName, count(*) most_popular
from EA_Music_Styles ms
INNER JOIN A_Entertainer_Styles es
ON ms.StyleID = es.StyleID
group by ms.StyleName
order by 2 desc
limit 1
This will give you the most popular (I take that it is the one that has more rows) StyleName. I count the ocurrences of StyleName and order it desc getting only the first result of this.

Here is how you can solve the problem.
In your entertainers table user group by StyleID while also selecting the count. This will give you the styleID count which you can sort by descending and limit 1 to find to most popular one
select StyleID, count(StyleID) c from EA_Entertainer_Styles
group by StyleID
order by c desc
limit 1;
Now use the styleID form the first step to get the StyleName from your table Music.
select StyleName from EA_Music_Styles
where StyleID = id_from_step1
You can use this two in a single query by joining them as in the answer shown by Jorge Campos

Max gives you the highest number in the database, not the most used. You should use count for that.
Next example should give correct result.
select StyleName from EA_Music_Styles
where StyleID = (
select StyleID
from EA_Entertainer_Styles
group by StyleID
order by count(StyleID) desc
limit 1)

Related

create a new column based on the conditions applied on the calculated average value in mysql, finally join the tables

I have two tables in mysql database
subjectids
id subject
11 Physics
12 Chemistry
13 Maths
14 Biology
15 History
16 Geography
studentsScores
id student subjectid score
1 Ahaan 11 45
2 Ahaan 12 33
3 Ahaan 13 49
4 Ivaan 11 41
5 Ivaan 12 38
6 Ivaan 13 46
7 Ann 11 40
8 Ann 12 30
9 Ann 13 50
I am trying to find the average of each subject and give a tag of easy , medium, hard based on the average value, like hard if avg<35, medium if avg between 35 and 45 and easy if avg greater than 45.
My expected result is
subject subjectid avg_score level
physics 11 42 medium
chemistry 12 33 hard
math 13 48 easy
I am new to sql, it would be great if you can help.
A simple JOIN and GROUP BY is enough to get your wanted result
SELECT `subject`, `subjectid`, ROUND(AVG(`score`),0) avg_score,
CASE
WHEN AVG(`score`) < 35 THEN 'hard'
WHEN AVG(`score`) between 35 and 45 then 'medium'
WHEN AVG(`score`) > 45 THEN 'easy'
end as level
FROM studentsScores ss JOIN subjectids si ON ss.`subjectid` = si.`id`
GROUP BY `subject`,`subjectid`
subject
subjectid
avg_score
level
Physics
11
42
medium
Chemistry
12
34
hard
Maths
13
48
easy
fiddle
A simple case statement would do the trick.
select si.subject,
si.id,
AVG(ss.score) as avg_score,
case when AVG(ss.score) < 35 then 'hard'
when AVG(ss.score) between 35 and 45 then 'medium'
when AVG(ss.score) > 45 then 'easy'
end as level
from subjectids si
inner join studentsScores ss on si.id=ss.subjectid
group by si.subject,si.id ;
https://dbfiddle.uk/IDS43R9W

SQL Find All user that have two history in a certain order

I'm looking for a SQL request that I can't find in internet (and I didn't found a solution myself).
I have two different table user and history and a table user_history that link the two tables.
For example :
USER
id name
1 John
2 Edie
3 France
4 Gabriel
5 Ellen
History
id date_entered type
1 2017-07-01 36
2 2017-07-02 52
3 2017-07-03 25
4 2017-07-04 69
5 2017-07-05 85
6 2017-07-06 74
7 2017-07-07 45
8 2017-07-08 85
9 2017-07-09 25
10 2017-07-10 78
USER_HISTORY
id id_user id_history
1 1 1
2 1 2
3 1 3
4 1 4
5 2 5
6 2 6
7 1 7
8 1 8
9 2 9
10 1 10
In this example, all history are made by user 1 and 2 (user 2 have history 5,6 and 9).
So the question is :
What is the SQL request that get me all the users that have in their history an history type 25 and then some days LATER an history type 85 ?
In this example, only user 1 (John) is ok because he has a history type 25 on 2017-07-03 and then an history type 85 on 2017-07-08.
User 2 (Edie) is not ok because even if he has an history 25 and 85, the first one was 85 and the 25.
Is that clear ?
Can you help me please ?
You need to JOIN twice with HISTORY table, e.g.:
SELECT h1.id_user
FROM (
SELECT u.id_user, h.date_entered
FROM user_history u
JOIN history h ON u.id_history = h.id
WHERE h.type = 25) h1
JOIN (
SELECT u.id_user, h.date_entered
FROM user_history u
JOIN history h ON u.id_history = h.id
WHERE h.type = 85
) h2 ON h1.id_user = h2.id_user
WHERE h1.date_entered < h2.date_entered;
Here's the SQL Fiddle.

MYSQL - Joining Multiple tables with COUNT()

I was asked to create a database for both students' tardiness and school policy's violations.
Now, I have three separate tables:
tbl_ClassList:
Student_ID Student_Name
1000 Lee, Jonder
1001 Chow, Stephen
1002 Kim, Martin
1003 Johns, Kevin
1004 Hearfield, Olivia
1005 Jarrs, Marlon
tbl_Tardy:
Record_No Student_ID
1 1001
2 1001
3 1000
4 1003
5 1002
6 1003
7 1001
8 1001
9 1002
10 1004
tbl_Violation:
Record_No Student_ID
1 1000
2 1000
3 1004
4 1005
5 1001
6 1002
7 1003
What I was asked to do is to generate a list that combines that contains information about the students including his/her ID, name, number of tardiness and the number of violations. Something like this:
Student_ID Student_Name No. of Tardy No. of Violation
1000 Lee, Jonder 1 2
1001 Chow, Stephen 4 1
1002 Kim, Martin 2 1
1003 Johns, Kevin 2 1
1004 Hearfield, Olivia 1 1
1005 Jarrs, Marlon 0 1
Is there any type of Joins I can use to achieve the output? Please help me.
You can find separate aggregates for tardy and violations inside subqueries and left join them with classlist table. Use coalesce to get zero in case there is no row for tardy/violation.
select c.*,
coalesce(t.count_tardy, 0) as no_of_tardy,
coalesce(v.count_violations, 0) as no_of_violations,
from tbl_classlist c
left join (
select student_id,
count(*) as count_tardy
from tbl_tardy
group by student_id
) t on c.student_id = t.student_id
left join (
select student_id,
count(*) as count_violations
from tbl_violation
group by student_id
) v on c.student_id = v.student_id;

MySQL: SUM data from two different tables

I have a db for a menu which tracks clicks on it. The menu has categories and subcategories and I'm trying to get the amount of clicks for each category but in the db, the clicks will register to the subcategory if the item is in one, otherwise the clicks are counted in the category. I have a query that will get clicks for all subcategories (category_type 3) but I need to add them with the clicks from their parent category (category_type 2). There is a table called CategoryHierarchy that maps each category to it's parent category. This is what I have:
SELECT IFNULL(SUM(`MenuEntryAnalytics`.`opened`), 0) AS `clicks`,
`Categories`.`id`,
`Categories`.`name`,
`Categories`.`category_type`,
`CategoryHierarchy`.`parent_id` AS `parent`
FROM `MenuEntryAnalytics`
INNER JOIN `MenuEntries`
ON `MenuEntryAnalytics`.`menu_entry_id` = `MenuEntries`.`id`
LEFT JOIN `MenuEntryToCategory`
ON `MenuEntryAnalytics`.`menu_entry_id` = `MenuEntryToCategory`.`menu_entry_id`
RIGHT JOIN `Categories`
ON `MenuEntryToCategory`.`category_id` = `Categories`.`id`
RIGHT JOIN `CategoryHierarchy`
ON `Categories`.`id` = `CategoryHierarchy`.`category_id`
WHERE `Categories`.`category_type` = 3
GROUP BY `id`;
Results:
clicks id name type parent
=============================================
2032 3 Appetizers 3 2
455 4 Salads 3 2
680 6 Sandwiches 3 5
424 7 Burgers 3 5
584 9 Pizza 3 8
466 10 Kids Menu 3 8
1445 12 Soda 3 11
1089 13 Signature Cocktails 3 11
391 14 Bottled Beer 3 11
167 15 Wine 3 11
0 17 Events 3 16
0 18 Sponsors 3 16
186 19 Dessert 3 11
621 26 Restaurants 3 22
263 27 Bars 3 22
112 28 Services 3 25
254 29 Amenities 3 25
67 30 Exclusive Benefits 3 25
190 31 Area Attractions 3 24
14 32 Entertainment 3 24
2 33 Shopping 3 24
117 34 Transportation & Tours 3 24
471 35 Mixed Drinks 3 11
541 36 Draft Beer 3 11
if I GROUP BY parent then I can get most of what I need (all the clicks from subcategories of each category) but this doesn't get the clicks counted towards categories (as opposed to subcategories, i.e. category_type 2). I'm stuck trying to add that part in, all I can think of is using a subquery but there's no way of identifying which category I'm looking at, thus I get a subquery with multiple rows.
PS I do not have permission to restructure the db.
Since the parent ID is in the same namespace as the ID, you can simply use IFNULL to pick the parent if it exists, or otherwise the ID. And use that as your grouping strategy.
You may also want to select the same data out as an actual column.
GROUP BY
IFNULL(CategoryHierarchy.parent_id, Categories.id, CategoryHierarchy.parent_id)

SQL query syntax to retrieve data from 2 tables

I have 2 tables in Mysql: authors and articles.
I have authors with several articles for each of them.
I need sql query with the result of this query:
retrieve 3 authors ordered by age with all articles which belong each of them.
In my example it will be
ID FIRSTNAME LASTNAME AGE AUTHORID TITLE PRICE
6 Salido Gomes 90 6 All 3 1
6 Salido Gomes 90 6 All 3 33
6 Salido Gomes 90 6 All 3 3
5 Vitora Mantora 45 5 Total 3 99
5 Vitora Mantora 45 5 Total 3 33
5 Vitora Mantora 45 5 Total 3 12
3 Joe Smith 43 3 Python 5
3 Joe Smith 43 3 Python 2 22
3 Joe Smith 43 3 Python 3 44
3 Joe Smith 43 3 Python 4 12
3 Joe Smith 43 3 Python 5 67
http://sqlfiddle.com/#!2/718c4/1
I use
select * from authors join articles on authors.id = articles.authorId
join (select authors.id from authors order by age DESC limit 3) as t
on t.id = authors.id
with wrong result
ID FIRSTNAME LASTNAME AGE AUTHORID TITLE PRICE
6 Salido Gomes 90 6 All 3 1
6 Salido Gomes 90 6 All 3 33
6 Salido Gomes 90 6 All 3 3
If you only want to include authors who have written any articles in the top 3 oldest authors try doing a join between authors and articles in the sub query to get the 3 oldest authors, and DISTINCT to eliminate duplicates:-
SELECT *
FROM
(
SELECT DISTINCT authors.id, authors.firstname, authors.lastname, authors.age
FROM authors
JOIN articles
ON authors.id = articles.authorId
ORDER BY authors.age
DESC
LIMIT 3
) author_sub
JOIN articles
ON author_sub.id = articles.authorId