Question

In: Computer Science

Write a query that returns the department id and the number of employees (in the associated...

Write a query that returns the department id and the number of employees (in the associated department) salary is over $2500 and, at the end, result page has to show only department ids that have more than 5 employees in it. Please Note: (we don't interest all of department ids in our grouping action and also we don’t want to see any kind of total employee number in the result page we want to see limited result set)

Hint1: SELECT part has to be like below. SELECT department, COUNT(*) as "Number of employees"

Hint2: Don't forget the use of WHERE and please distinguish the WHERE and HAVING

SQL SCHEMA (oracle):
https://pastebin.com/raw/vaesFLWM

Solutions

Expert Solution

When I checked the tables in your database I get to know that there are employees table and department table. employees table contains department_id and salary. Since we need only department_id and not it's name so no need to use department table. Only with employees table we can execute the query.

Since we need to group the total count with respect to department_id we use GROUP BY on department_id.

To check whether the code is working properly or not I created a table names employees with only 2 columns department_id and salary.

The query:

SELECT department_id, COUNT(*) as "Number of Employees" from employees WHERE salary>25000 GROUP BY department_id;

will produce the department_id and their respective count of employees whose salary is more than 25000.

Since we need only department ids where the employees count is more than 5.

We can use HAVING clause to show the result but it prints both columns as shown below

SELECT department_id,COUNT(*) AS "Number of employees" FROM `employees` WHERE salary>25000 GROUP BY department_id HAVING COUNT(*)>5

If it is fine then we can use the above query.

But if we want only department id but not count column then we can use VIEWS.

CREATE VIEW view_employees_department_id AS SELECT department_id, COUNT(*) as "Number of Employees" from employees WHERE salary>25000 GROUP BY department_id HAVING COUNT(*)>5;

The above query will generate the VIEW with the given select statement. It contains 2 columns namely department_id and Number of employees where count>5.

Now since we need only department_id column we will use the above view as our new table(here the VIEW table is a virtual one). i.e;

SELECT department_id from view_employees_department_id;

Difference between WHERE and HAVING:

  1. Apart from SELECT queries, you can use WHERE clause with UPDATE and DELETE clause but HAVING clause can only be used with SELECT query.
  2. WHERE clause is used for filtering rows and it applies on each and every row, while HAVING clause is used to filter groups in SQL.
  3. One syntax level difference between WHERE and HAVING clause is that, former is used before GROUP BY clause, while later is used after GROUP BY clause.
  4. When WHERE and HAVING clause are used together in a SELECT query with aggregate function,  WHERE clause is applied first on individual rows and only rows which pass the condition is included for creating groups. Once group is created, HAVING clause is used to filter groups based upon condition specified.

If you have doubts kindly ask me. Thank you


Related Solutions

Write a query that returns all of the main information that are associated with the purchase...
Write a query that returns all of the main information that are associated with the purchase orders that have an amount greater than 70% of the average amount of all purchase orders, the output should be sorted by the largest purchase order amount
PostgreSQL 1. Write a query to join two tables employees and departments to display the department...
PostgreSQL 1. Write a query to join two tables employees and departments to display the department name, first_name and last_name, hire date, and salary for all managers who have more than 15 years of experience. 2. Write a query to join the employees and departments table to find the name of the employee including the name and last name, department ID and department name. 3. Write a SQL query to join three tables of employees, departments, and locations to find...
Write an SQL query that will output the employee id, first name and hire date from...
Write an SQL query that will output the employee id, first name and hire date from the employee table. Pick only those employees whose employee ID is specified in the employee table (no nulls). If the employee id is 777, name is ABC and hire date is 01-JAN-2016, the output should be like - ' ABC (Employee ID - 777) was hired on 1, January of 2016'. Note - The date should not have preceding zeros.
List department name, employee id, and employee name for all employees in department name order. Repeat...
List department name, employee id, and employee name for all employees in department name order. Repeat for department #10 only. List the course ID, course name, section, instructor name, day, time, and room for all course sections. List the course ID, course name, section, student ID, and student name for CRN 1003. Display the list in ascending order of student last and first names. DROP TABLE registration; DROP TABLE sections; DROP TABLE courses; DROP TABLE students; DROP TABLE instructors; CREATE...
5. Write the SQL query that accomplishes the task in the ZAGI Retail Company Sales Department...
5. Write the SQL query that accomplishes the task in the ZAGI Retail Company Sales Department Database: 5.1.3. Display the CustomerName and CustomerZip for all customers, sort alphabetically by CustomerName. 5.1.4. Display the RegionID of regions where we have stores (use only table STORES and do not display the same information more than once). 5.1.5 Display all the information for all stores whose ReigionID value is C 5.1.8 Display the ProductID, ProductName, ProductPrice, and VendorName for all products. Sort the...
Write a SQL query that displays the number of customers from Mexico, USA, and Canada. Hint:...
Write a SQL query that displays the number of customers from Mexico, USA, and Canada. Hint: The result of the SQL query should look like the following table: CustomerCountNorthAmerica 21
Employee ID First Name Last Name email Title Address Extension Department Department ID Hiring Date Department...
Employee ID First Name Last Name email Title Address Extension Department Department ID Hiring Date Department Phone # 0001 John Smith jsmith Accountant 1300 West st 5775 Accounting 2100 8/1998 407-366-5700 0002 Brian Miller badams Admin Assistant 1552 Palm dr 5367 Human resource 2300 4/1995 407-366-5300 0003 James Miller miller Inventory Manager 2713 Buck rd 5432 Production 2520 8/1998 407-366-5400 0004 John Jackson jackson_sam Sales Person 433 tree dr 5568 Sales 2102 6/1997 407-366-5500 0005 Robert Davis Davis Manager 713...
Write a program of doubly Circular linked list to maintain records of employees. Take employee ID,...
Write a program of doubly Circular linked list to maintain records of employees. Take employee ID, name and salary as data of each employee. Search a particular record on ID and display the previous and next records as well. Whichever ID it give, it should display all the records because of being circular. Code needed in Java.
Problem 44 Write a query to display the employee number, last name, first name, and sum...
Problem 44 Write a query to display the employee number, last name, first name, and sum of invoice totals for all employees who completed an invoice. Sort the output by employee last name and then by first name (Partial results shown in Figure P7.44).
Write a function that takes a number as input, and returns the character A if the...
Write a function that takes a number as input, and returns the character A if the input is 90 and above, B if it’s 80 and above but less than 90, C if it’s at least 70 but less than 80, D if it’s at least 60 but less than 70, and F if it’s less than 60. If the input is not a number or is negative, the function should exit 1 with an error (by calling the Matlab...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT