0

I have 2 tables (Students and Cellphone).

Columns in Students: StudentID, FNAME, PHONEID
Columns in Cellphone: PhoneID, Phonename

I want to create this:

• Run a query to show the most popular phone used by students

My answer is:

select D.phoneid, c.phonename, d.numberp
from
(select * from 
(SELECT m.phoneid, 
 COUNT(*) AS numberp
 FROM students m
GROUP BY m.phoneid
ORDER BY numberp DESC)
where rownum = 1) D, cellphone c
where d.phoneid = c.phoneid

Can this be optimized or a simpler way of achieving this? May be using TOP ? or HAVING ? JOINS ?

Danilo Braga
1,4282 gold badges14 silver badges22 bronze badges
asked Apr 12, 2019 at 2:16

1 Answer 1

0
SELECT c.phoneid, COUNT( /* DISTINCT*/ s.StudentID)
FROM Students s, Cellphone c
WHERE s.phoneid = c.phoneid
GROUP BY c.phoneid
ORDER BY 2 DESC
LIMIT FIRST 1 ROW /* WITH TIES */

?

answered Apr 12, 2019 at 4:53

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.