Consider the Orders database and the Employee table. 1\. Write a query to list the employee who does not report to anyone. 2\. Write a query to list each employee and the number of employees they supervise.

Short Answer

Expert verified
The query to list the employee who does not report to anyone is: \[ SELECT * FROM Employee WHERE reports_to IS NULL; \] and the query to list each employee and the number of employees they supervise is: \[ SELECT E1.emp_name, COUNT(E2.emp_name) AS NumberOfSubordinates FROM Employee E1 LEFT JOIN Employee E2 ON E2.reports_to = E1.emp_id GROUP BY E1.emp_name; \]

Step by step solution

01

Identify Employees Without Supervisors

For the task of identifying employees who don't report to anyone, use a SELECT statement. If the 'reports_to' column stores this information, and it's assumed that an employee without a supervisor will have a null value here, the query should look like this: \[ SELECT * FROM Employee WHERE reports_to IS NULL; \] This query selects and displays all records from the 'Employee' table where the 'reports_to' column is null, which means the employee doesn't report to anyone.
02

List Employees and the Number of Employees They Supervise

To list each employee along with the number of employees they supervise, you need to use a combination of the 'SELECT', 'COUNT', and 'GROUP BY' statements. Suppose the 'reports_to' field in the Employee table specifies who an employee reports to, the query should be: \[ SELECT E1.emp_name, COUNT(E2.emp_name) AS NumberOfSubordinates FROM Employee E1 LEFT JOIN Employee E2 ON E2.reports_to = E1.emp_id GROUP BY E1.emp_name; \] The 'LEFT JOIN' ensures that all employees are listed, even if they do not supervise anyone (these will get a count of 0). The 'GROUP BY' statement groups the result set by employee names.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

Consider the Orders database. 1\. Create a query to list each customer and their orders (order id and order date). Are there any customers who have not placed an order? 2\. Modify the above query to list each customer and the number of orders they have placed (include all customers).

Consider the University database: 1\. For each department list the department code and the largest value for credit hours. 2\. For each department list the department code, department name, and the number of courses.

Consider the Company database and its Employee table. 1\. The empId field is assigned values sequentially starting at 1. What is the last empId value that was used? (What is the empId for the last employee added to the table?) 2\. Write a query to determine the name of the oldest employee. 3\. Write a query to list all of the employee last names. If at least two employees have the same last name then this list will be shorter that a list of employees. 4\. Suppose there is a field hireDate which holds the date when an employee was hired. Write a query to determine the name of the employee who was most recently hired?

Consider the University database. 1\. Create a query to list all courses in a department (for which the user supplies the department code). 2\. Create a query to list all course titles where the user supplies both the department code and the credit hours. Note that two separate criteria, each with their own parameter, must be specified.

Consider the Library database. 1\. Write a query to list books due on a specific date (a parameter). 2\. Write a query to list books written by a specific author (a parameter).

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free