Hallo an alle,
ich habe heute an einem Test von "Expertrating.com" teilgenommen und bin beim SQL-Test an einer Frage kläglich gescheitert, und es hat mir keine Ruhe gelassen, diese Aufgabe zu lösen, aber irgendwie stehe ich auf dem Schlauch.
Eventuell könnt ihr mir ja helfen
Hier die Frage:
Mit folgendem SQL-Query bekomme ich zumindest alle Daten angezeigt:
Wenn ich nun den letzten Teil der Aufgabe lösen will, muss ich ja mit nem Count() an die Sache rangehen, aber mit dem folgenden Query bekomme ich nur NULL als Ergebnis.
Könnt ihr mir sagen, wo mein Denkfehler ist?
Vielen Dank vorab für eure Hilfe!
Hier noch ein MySQL-Dump für den Fall, dass ihr ihn braucht:
ich habe heute an einem Test von "Expertrating.com" teilgenommen und bin beim SQL-Test an einer Frage kläglich gescheitert, und es hat mir keine Ruhe gelassen, diese Aufgabe zu lösen, aber irgendwie stehe ich auf dem Schlauch.
Eventuell könnt ihr mir ja helfen
Hier die Frage:
Consider the following tables:
department
----------
deptid (type: INT)
deptname (type: TEXT)
hours (type: INT)
active (type: BIT)
employee
--------
empid (type: INT)
empname (type: TEXT)
deptid (type: INT)
designation (type: TEXT)
salary (type: INT)
Write a query to return the columns empname and deptname of the employees belonging to those departments that have a head count of 4 or more. The records should be returned in alphabetical order of empname.
department
----------
deptid (type: INT)
deptname (type: TEXT)
hours (type: INT)
active (type: BIT)
employee
--------
empid (type: INT)
empname (type: TEXT)
deptid (type: INT)
designation (type: TEXT)
salary (type: INT)
Write a query to return the columns empname and deptname of the employees belonging to those departments that have a head count of 4 or more. The records should be returned in alphabetical order of empname.
Code:
SELECT employee.empname, department.deptname FROM employee INNER JOIN department ON employee.deptid = department.deptid GROUP BY employee.empid;
Könnt ihr mir sagen, wo mein Denkfehler ist?
Vielen Dank vorab für eure Hilfe!
Code:
SELECT employee.empname, department.deptname FROM employee INNER JOIN department ON employee.deptid = department.deptid GROUP BY employee.empid HAVING COUNT(employee.deptid) >= 4;
Code:
-- -- Table structure for table `department` -- DROP TABLE IF EXISTS `department`; CREATE TABLE IF NOT EXISTS `department` ( `deptid` int(11) DEFAULT NULL, `deptname` text, `hours` int(11) DEFAULT NULL, `active` bit(1) DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1; -- -- Dumping data for table `department` -- INSERT INTO `department` (`deptid`, `deptname`, `hours`, `active`) VALUES (1, 'department 01', 10, b'1'), (2, 'department 02', 10, b'1'), (3, 'department 03', 10, b'1'), (4, 'department 04', 10, b'1'); -- -------------------------------------------------------- -- -- Table structure for table `employee` -- DROP TABLE IF EXISTS `employee`; CREATE TABLE IF NOT EXISTS `employee` ( `empid` int(11) DEFAULT NULL, `empname` text, `deptid` int(11) DEFAULT NULL, `designation` text, `salary` int(11) DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1; INSERT INTO `employee` (`empid`, `empname`, `deptid`, `designation`, `salary`) VALUES (1, 'employee 01', 1, NULL, NULL), (2, 'employee 02', 1, NULL, NULL), (3, 'employee 03', 1, NULL, NULL), (4, 'employee 04', 1, NULL, NULL), (5, 'employee 05', 1, NULL, NULL), (6, 'employee 06', 1, NULL, NULL), (7, 'employee 07', 1, NULL, NULL), (8, 'employee 08', 2, NULL, NULL), (9, 'employee 09', 2, NULL, NULL), (10, 'employee 10', 2, NULL, NULL);
Kommentar