Win a 3 month subscription to Marco Behler Videos this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

sql GROUP BY and HAVING not working  RSS feed

 
Gbenga wale
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this sql query which is giving me a result which is not complete(half result).
I want to retrieve all students info from the same class which is from the student table, then group them by class.

This are the queries code.

    SELECT first_name, last_name, username FROM students GROUP BY class       
    HAVING class IN(SS3);
           
    SELECT first_name, last_name, class FROM students GROUP BY class
    HAVING class = 'JSS1';
    SELECT
    user_id,
     first_name,
     last_name,
     class,
    COUNT(*) AS total_students
    FROM
     students
    GROUP BY
    class
     HAVING class IN ('JSS1')

The last query also display just only one info, which is the first "jss1" class student name in the table
but this time shows that there are 17 students who are in jss1

How do I manipulate the query to display all the names of the students in the same class

This is the table structure

CREATE TABLE IF NOT EXISTS `students` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`class` varchar(10) NOT NULL,
`first_name` varchar(30) NOT NULL,
`last_name` varchar(50) NOT NULL,
`username` varchar(20) NOT NULL,
PRIMARY KEY (`user_id`)
)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!