Question
Given the following SQL query: SELECT department
_ id, COUNT(*) AS employee _ count FROM employees GROUP BY department_id HAVING COUNT(*) > 5; What is the purpose of the HAVING clause in this query?Solution
The HAVING clause is used in SQL to filter groups created by the GROUP BY clause based on an aggregate function. 1. Aggregate Filtering: Unlike the WHERE clause, which filters rows before grouping, HAVING applies conditions on grouped data. 2. Purpose in Query: In this query, the HAVING clause ensures that only those departments with more than 5 employees (as determined by COUNT(*)) are included in the result set. 3. Execution Process: o The GROUP BY groups the data by department_id. o The COUNT(*) calculates the number of employees in each department. o The HAVING clause filters out groups where COUNT(*) is less than or equal to 5. This differentiation between WHERE and HAVING is crucial for effective data querying. Why Other Options Are Incorrect: • A) To filter rows before grouping them: The WHERE clause performs pre-grouping filtering, not HAVING. • C) To apply a condition to the entire table: HAVING only applies conditions to grouped results. • D) To apply conditions only to the COUNT(*) function: While COUNT(*) is used, the HAVING clause applies to groups as a whole. • E) To sort the rows by department_id: Sorting is achieved using the ORDER BY clause, not HAVING.
P invests Rs. 5,500 on compound interest at 16% p.a. (compounded semiannually) for 1 year. Find the interest earned by 'P'.
Krishna invests a certain amount at an annual simple interest rate of 9% for 2 years. The total amount he receives after this period is then reinvested ...
Atul has Rs.600 with him. He invested 30% of the amount at 5% p.a. for 6 years and rest at 15% p.a. for 4 years. Find the sum of simple interests receiv...
A person invested Rs. 75,000 at an annual compound interest rate of 12%, compounded annually for 12 months. How much more profit would he have earned if...
The interest earned on investing Rs. 1000 for 2 years at the rate of 20% p.a., compounded annually, is used to purchase an article. If the article is la...
The simple interest on a sum of money is 4/25 of the sum. If the number of years is numerically half the rate per annum, then find the rate percent per ...
Rs. 2,800 increases to Rs. 3,640 in 4 years at a simple interest rate of r% per annum. If Rs. 7,000 is put at 2r% per annum for 1 year, how much interes...
Suresh earned an interest of Rs. 1548 on principal amount of Rs. 4800 at some rate of compound interest in 2 years. How much more/less interest would he...
Akshay invested Rs. 1200 in two schemes P and Q in the respective ratio of 7:3. Scheme P and Q are offering simple interest at the rate of 10% per annum...
Simple interest received at the rate of 20% p.a. for 6 years on a principal amount of Rs. 9000 is twice of the simple interest received at 10% p.a. for ...