Classes More Than 5 StudentsThere is a table courses with columns: student and classPlease list out all classes which have more than or equal to 5 students.For example, the table:studentABCDEFGHIclassMathEnglishMathBiologyMathComputerMathMathMathShould output:classmathNote:The students should not be counted duplicate in each course.Optionsselect classfrom coursesgroup classhaving count(distinct student) >= 5;select classfrom coursesgroup by studenthaving count(distinct student) >= 5;select classfrom coursesclasshaving count(distinct student) >= 5;select classfrom coursesgroup by classhaving count(distinct student) >= 5;Finish
Question
Classes More Than 5 StudentsThere is a table courses with columns: student and classPlease list out all classes which have more than or equal to 5 students.For example, the table:studentABCDEFGHIclassMathEnglishMathBiologyMathComputerMathMathMathShould output:classmathNote:The students should not be counted duplicate in each course.Optionsselect classfrom coursesgroup classhaving count(distinct student) >= 5;select classfrom coursesgroup by studenthaving count(distinct student) >= 5;select classfrom coursesclasshaving count(distinct student) >= 5;select classfrom coursesgroup by classhaving count(distinct student) >= 5;Finish
Solution
The correct SQL query to list out all classes which have more than or equal to 5 students, without counting duplicate students in each course, is:
select class
from courses
group by class
having count(distinct student) >= 5;
Here's how this query works:
-
select class from courses: This part of the query selects the 'class' column from the 'courses' table. -
group by class: This part groups the selected data by 'class'. This means that all rows with the same class are grouped together. -
having count(distinct student) >= 5: This is a condition that the groups must meet to be included in the result. Thecount(distinct student)part counts the number of unique students in each group. The>= 5part means that only groups with 5 or more unique students are included in the result.
Similar Questions
Give the database schema the following tables:Student (studentId, firstName, lastName, gender, birthday, phone, address, classId)Class (classId, className)Which code snippet will list the id and names of class with more than 15 students?SELECT c.classId, c.className, COUNT(*) AS amountFROM dbo.Class c INNER JOIN dbo.Student s ON c.classId = s.classIdGROUP BY c.classId, c.classNameHAVING COUNT(*) > 15;SELECT c.classId, c.className, COUNT(*) AS amountFROM dbo.Class c INNER JOIN dbo.Student sWHERE c.classId = s.classId AND amount > 15GROUP BY c.classId, c.className;SELECT c.classId, c.className, COUNT(*) AS amountFROM dbo.Class c INNER JOIN dbo.Student s ON c.classId = s.classIdGROUP BY c.classId, c.classNameHAVING amount > 15;SELECT c.classId, c.className, COUNT(*) AS amountFROM dbo.Class c INNER JOIN dbo.Student s ON c.classId = s.classIdWHERE COUNT(*) > 15;
Give the database schema the following tables: Student (studentId, firstName, lastName, gender, birthday, phone, address, classId) Class (classId, className) Which code snippet will display records with the following 3 columns: classId, className and amount (amount of students)?SELECT c.classId, c.className, COUNT(*) AS amountFROM dbo.Class c INNER JOIN dbo.Student s ON c.classId = s.classId;SELECT c.classId, c.className, COUNT(*) AS amountFROM dbo.Class c INNER JOIN dbo.Student s WHERE c.classId = s.classIdGROUP BY c.classId, c.className;SELECT c.classId, c.className, COUNT(*) AS amountFROM dbo.Class c INNER JOIN dbo.Student s ON c.classId = s.classIdGROUP BY c.classId;SELECT c.classId, c.className, COUNT(*) AS amountFROM dbo.Class c INNER JOIN dbo.Student s ON c.classId = s.classIdGROUP BY c.classId, c.className;
Given 3 tables, students(id, name) , courses(id, name) , grades(id, course_id, student_id, grade), find the name of the most popular course (the one with the most enrolled students and students should be passing, i.e. achieving a grade of at least 50) and if there is a tie, get the course name that's lexicographically smaller. Your query should output a table with the following columns (name)
This task concerns the following tables:PersonsidnameaddressageeyeColorgenderTakesClassesidperson_id → Personsclass_id → ClassesWrite a query that returns a table with columns name and nrOfClasses that contains the list of all names of people and the number of classes that this person takes.NoteYou can give names to columns in the SELECT clause by SELECT … as someName.
Give a table:Student (studentId, name, major) Primary key is [studentId, major].Which code snippet will list out students that the major not equals 'IT'?SELECT * FROM Student HAVING major <> 'IT'All of the aboveSELECT * FROM Student s WHERE s.major != 'IT'SELECT * FROM Student GROUP BY major HAVING major <> 'IT'
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.