What is wrong with my Subquery?

  • Thread starter Thread starter WWGD
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around a user's difficulty in constructing a SQL query to determine which employee has the most other employees reporting to them, using a subquery. The scope includes technical explanations of SQL syntax and installation issues related to SQL Server 2014.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Meta-discussion

Main Points Raised

  • One participant expresses frustration with installing MSSQL2014 and seeks alternatives for testing SQL queries.
  • Another participant suggests using MySQL and provides advice on installing a full stack package like WAMP, LAMP, XAMPP, or MAMP for easier database management.
  • A participant clarifies that the original query structure is incorrect and proposes a revised query that groups by ReportsTo instead of employeeID to count the number of employees reporting to each employee.
  • Further suggestions include using a subquery in the HAVING clause to find the employee with the maximum number of reports, with variations in syntax noted for different SQL implementations.
  • Some participants discuss the proprietary nature of certain SQL syntax like TOP 1 and suggest alternative approaches that are more universally applicable.
  • Several participants express interest in the original poster's installation issues with SQL Server 2014 and offer to assist with troubleshooting.

Areas of Agreement / Disagreement

Participants generally agree on the need to revise the SQL query structure, but there are multiple competing views regarding the best approach to achieve the desired result. The discussion on installation issues remains unresolved, with differing experiences shared.

Contextual Notes

Limitations include the original poster's inability to test queries due to installation issues, and the discussion includes various SQL syntax that may not be universally applicable across different database systems.

Who May Find This Useful

Readers interested in SQL query construction, troubleshooting database installation issues, or those seeking advice on database management tools may find this discussion beneficial.

WWGD
Science Advisor
Homework Helper
Messages
7,802
Reaction score
13,106
My apologies in advance: I have tried to install MSSQL2014 many times unsuccessfully (I get a bunch of .zip files, but no executable) so I cannot test my queries. I looked for places to " outsource" my queries, I was told of MySql fiddle , but I can't see how to run my database there. I am just going over a class video and trying to do things on my own and then check against the answer given. If someone knows of a public source for setting up databases and running queries, I would appreciate any ref.

Anyway, I have a table Employee containing , among others, EmpId and ReportsTo. I am trying to do a query to find out which employee has the most other employees reporting to them. I am using this subquery:

Select employeeID, Count(ReportsTo) from Employee
Group by employeeId having
Count(ReportsTo) >= (select max(Count(ReportsTo)) from employee group by EmpId)

The answer given basically swaps employeeID and ReportsTo.

I am trying to understand what I did wrong. Thanks for any comments.
 
Technology news on Phys.org
Are you installing it using the installer? I'm pretty sure MYSQL also runs as a service. The only executable you want to be manually starting is the workbench. From there you can start the server(service) and have a graphical view of the database.
If you can't get the installer to work then try installing a full stack package like WAMP, LAMP, XAMPP, or MAMP.

Executing queries is a trivial task; It can be done in most modern languages using MYSQL Connector.
Here is how to do it in python.
 
  • Like
Likes   Reactions: WWGD
Thank you, Thomas, do you mean I can execute queries in MySQL, including executing a query to fill my database, the one I want to query? I had no trouble installing MySQL, it is MSSQL2014 workbench I had trouble with. The two main mainstream news (really entertainment) channels, FOX and MSNBC sell radical politics, Right and Left respectively. It sells, so it continues. I see little real debate outside of that. Mixing news with business does not seem like a good idea.
 
Last edited:
WWGD said:
My apologies in advance: I have tried to install MSSQL2014 many times unsuccessfully (I get a bunch of .zip files, but no executable) so I cannot test my queries. I looked for places to " outsource" my queries, I was told of MySql fiddle , but I can't see how to run my database there. I am just going over a class video and trying to do things on my own and then check against the answer given. If someone knows of a public source for setting up databases and running queries, I would appreciate any ref.

Anyway, I have a table Employee containing , among others, EmpId and ReportsTo. I am trying to do a query to find out which employee has the most other employees reporting to them. I am using this subquery:

Select employeeID, Count(ReportsTo) from Employee
Group by employeeId having
Count(ReportsTo) >= (select max(Count(ReportsTo)) from employee group by EmpId)

The answer given basically swaps employeeID and ReportsTo.

I am trying to understand what I did wrong. Thanks for any comments.
"Select employeeID, Count(ReportsTo) from Employee Group by employeeId " will give you one record per employee. You actually want one record per reportsTo.
To count the number of employees that report to each employee you have to group by reportsTo:
"Select reportsTo, Count(Employee) from Employee Group by reportsTo"

If you want only the employee(s) who has the most employees reporting to him you can use a subquery in the having clause:
SELECT reportsTo, Count(employeeID) FROM Employee GROUP BY reportsTo
HAVING (Count(employeeID) In
(SELECT top 1 Count(employeeID) FROM Employee GROUP BY reportsTo ORDER BY Count(employeeID) DESC;));
 
Last edited:
  • Like
Likes   Reactions: WWGD
Samy_A said:
"Select employeeID, Count(ReportsTo) from Employee Group by employeeId " will give you one record per employee. You actually want one record per reportsTo.
To count the number of employees that report to each employee you have to group by reportsTo:
"Select reportsTo, Count(Employee) from Employee Group by reportsTo"

If you want only the employee(s) who has the most employees reporting to him you can use a subquery in the having clause:
SELECT reportsTo, Count(employeeID) FROM Employee GROUP BY reportsTo
HAVING (Count(employeeID) In
(SELECT top 1 Count(employeeID) FROM Employee GROUP BY reportsTo ORDER BY Count(employeeID) DESC;));
Noting that TOP 1 is proprietary syntax supported by 1/3 of major implementations (others use limit; SQL Standard has a more complex approach), you can even more simply express Samy_A's approach without this feature at all (the following would be supported on any plausible SQL implementation):

SELECT reportsTo, Count(employeeID) FROM Employee GROUP BY reportsTo
HAVING Count(employeeID) = (select max(cr) from
(SELECT Count(employeeID) as cr FROM Employee GROUP BY reportsTo ) v1)

You can also express this without using GROUP BY or HAVING, though there is no particular reason to avoid these:

select employeeID, (select count(*) from employee e where e.reportsto = eo.employeeID) cr
from employee eo
where (select count(*) from employee e where e.reportsto = eo.employeeID) =
(select max(cr) from (select (select count(*) from employee e where e.reportsto=eo.employeeID) cr from employee eo) v1)
 
Last edited:
PAllen said:
Noting that TOP 1 is proprietary syntax supported by 1/3 of major implementations (others use limit; SQL Standard has a more complex approach)
Oops, correct MySQL doesn't support TOP.
 
I am interested to know more about your failures to install SQL Server 2014; I have installed it multiple times on multiple machines with very few issues. If you would like to message me about that, please do. If you have a healthy Windows desktop of any recent version and plenty of disk space and RAM, this should work just fine. And then you would have access to all the free Microsoft SQL tools.
 
  • Like
Likes   Reactions: WWGD
IMHO the easiest way to a working RDMBS setup on Windows is:
  1. Install XAMPP
  2. Forget about the MySQL console, use phpMyAdmin (follow the link from the localhost home page, or click MySQL -> Admin in the XAMPP control panel)
 
  • Like
Likes   Reactions: WWGD
harborsparrow said:
I am interested to know more about your failures to install SQL Server 2014; I have installed it multiple times on multiple machines with very few issues. If you would like to message me about that, please do. If you have a healthy Windows desktop of any recent version and plenty of disk space and RAM, this should work just fine. And then you would have access to all the free Microsoft SQL tools.
Thank you for the offer, I will take you up on it ASAP.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
35
Views
7K
Replies
5
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K