What is wrong with my Subquery?

  • Thread starter WWGD
  • Start date
In summary, the conversation discusses the difficulties the individual has faced while trying to install MSSQL2014 and their search for alternative options to run their queries. They also mention a specific query they are trying to execute and their confusion about the results. Some suggestions are given on how to improve the query and possible solutions for their installation problems, including using XAMPP and phpMyAdmin.
  • #1
WWGD
Science Advisor
Gold Member
7,003
10,423
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
  • #2
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 WWGD
  • #3
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:
  • #4
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 WWGD
  • #5
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:
  • #6
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.
 
  • #7
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 WWGD
  • #8
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 WWGD
  • #9
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.
 

What is wrong with my Subquery?

There are a few possible issues that could be causing problems with your subquery. Here are some common questions that may help you troubleshoot the issue:

1. Why am I getting an error message when I try to use a subquery?

There are a few potential reasons for this. First, make sure that your subquery is syntactically correct and properly nested within your main query. Also, check to see if your subquery is returning more than one row - if so, you may need to use a different type of subquery or a different approach entirely.

2. Why is my subquery not returning any results?

If your subquery does not return any results, it could be due to a few different reasons. One possibility is that the subquery's conditions are not being met, so no rows are being selected. Another possibility is that the subquery is returning NULL values, which may not be included in the final result set depending on how your query is structured.

3. How can I optimize my subquery for better performance?

If your subquery is taking a long time to run, there are a few steps you can take to improve its performance. First, make sure that you are only selecting the columns that you need in your subquery - selecting unnecessary columns can slow down the query. You can also try rewriting the subquery as a JOIN, which may be more efficient in some cases.

4. Is it possible to use a subquery in a WHERE clause?

Yes, you can use a subquery in a WHERE clause to filter the results of your main query. However, there are some limitations to be aware of - for example, if your subquery returns more than one row, you may encounter errors or unexpected results. It's important to carefully structure your subquery and ensure that it returns the correct data for your needs.

5. Can I use a subquery in an INSERT or UPDATE statement?

Yes, you can use a subquery in an INSERT or UPDATE statement to insert or update data based on the results of another query. However, there are some restrictions on the types of subqueries that can be used in these statements - for example, you may need to use a subquery that only returns a single row. It's important to carefully consider the structure and limitations of your subquery when using it in an INSERT or UPDATE statement.

Similar threads

  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
7
Views
400
  • Programming and Computer Science
Replies
16
Views
1K
  • STEM Academic Advising
Replies
1
Views
670
  • Programming and Computer Science
Replies
21
Views
462
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
5
Views
968
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
18
Views
3K
Back
Top