School of Computing, Engineering and Information Sciences University of Northumbria Further ‘Group By’ plus Extend Operations Aim: To practise further SQL retrievals that implement the equivalent of the GroupBy and Extend relational algebra operations. Outline of Session: Do some queries that require SQL to treat an entire table as a single group. Do some SQL queries that incorporate the Having phrase to achieve Restrictions on the results of a GroupBy operation. Do some queries that do the SQL equivalent of the relational Extend operation. School of Computing, Engineering and Information Sciences University of Northumbria These exercises assume that you are using the ‘Company’ database. A COMPLETE TABLE AS A SINGLE GROUP If aggregate functions are put in an SQL SELECT phrase without a following GROUP BY phrase, the entire table is treated as one group and a GroupBy operation carried out on that, returning one row as the result. For example : SQL> SELECT AVG( SALARY ) AS AVG_SALARY 2 FROM EMP; gives the average salary of all the employees. SQL> SELECT COUNT( DISTINCT MARITAL_STATUS ) AS NO_OF-SALS 2 FROM EMP; reports how many different values of MARITAL_STATUS occur among all the employees. Exercise: execute the above example SQL queries to satisfy yourself that they work, and give the correct answer. Exercise: develop and execute retrievals for the following queries. Make sure each aggregate column has its own proper name. Check each answer returned to ensure that it is correct. 1. Find the company’s total salary bill, i.e. the sum of the salaries of all employees. 2. Find the earliest deadline among all the projects. 3. Find the maximum and minimum salaries (in a single query). 4. Find the average, minimum and maximum departmental budge, plus the sum total of all the department budgets. (Do this in one query). . RESTRICTING GROUPED DATA Since Relational Algebra allows us to perform a Restriction on any relation, it allows us to perform a Restriction on the relation that results from a GroupBy operation. In order to do the same, SQL uses two different syntaxes. One syntax is used if the Restriction is performed before the GroupBy operation, and the other is used if the Restriction is performed after the GroupBy operation. The WHERE phrase is used in the former case, the HAVING phrase in the latter. Thus the HAVING phrase is used if the Restriction condition uses any value(s) calculated with an aggregate function(s), since such value(s) is/are created by the GroupBy operation. 2 School of Computing, Engineering and Information Sciences University of Northumbria For example, to find those values of Marital Status for which there are precisely two employees, we could write the query : SQL> 2 3 4 SELECT MARITAL_STATUS, COUNT(*) AS NO_OF_EMPLOYEES FROM EMP GROUP BY MARITAL_STATUS HAVING COUNT(*) = 2; Exercise: execute the above example SQL query to satisfy yourself that it works, and gives the correct answer. Note that the HAVING condition does not need to use the same aggregate function as appears in the SELECT phrase; it could be a diferent aggregation. Logically we could also refer to the grouping attribute(s) in the HAVING phrase, but then logically it would make no difference whether we applied a Restriction condition to it/them before or after the GroupBy. For example consider the following two queries. Logically they should both return the same result. SQL> 2 3 4 SELECT MARITAL_STATUS, COUNT(*) AS NO_OF_MARRIED_EMPLOYEES FROM EMP GROUP BY MARITAL_STATUS HAVING MARITAL_STATUS = ‘M’; SQL> 2 3 4 SELECT MARITAL_STATUS, COUNT(*) AS NO_OF_MARRIED_EMPLOYEES FROM EMP WHERE MARITAL_STATUS = ‘M’ GROUP BY MARITAL_STATUS ; In such cases, typically the Restriction is applied first in SQL, using the WHERE phrase. Exercise: execute these 2 SQL queries to satisfy yourself that they do work, and give the same answer. The following queries need the HAVING phrase. Exercise: develop and execute retrievals for the following queries. Make sure each aggregate column has its own proper name. Check each answer returned to ensure that it is correct. 1. List the projects, by project number, with the number of employees working on each, but only for those projects with less than three employees. 2. List those employees, by employee number, who work on more than one project, including the number of projects they work on. 3. For each instance in which two or more employees are paid exactly the same salary, get the amount of that salary and the number of employees receiving it. 4. List those departments, by department number, who have more than one employee, including the number of employees they employ. 3 School of Computing, Engineering and Information Sciences University of Northumbria ‘EXTEND’ OPERATIONS : CREATING NEW COLUMNS When it is necessary to calculate new values in tuples from existing values in tuples, Relational Algebra uses the Extend operation to add another attribute(s) to a retrieved relation, the aditional attribute(s) containing the results of the calculation(s). The equivalent operation is achieved in SQL by specifying additional column(s) in the SELECT phrase as expression(s) followed by AS and the new column name(s). The expressions can include the names of existing columns, arithmetic operators (e.g. * or +) and SQL's built in functions. For instance to show the salaries after a 10% rise, a new column could be created as follows : SQL> SELECT EMP_NO, EMP_NAME, SALARY * 1.1 AS NEW_SALARY, SALARY AS OLD_SALARY 2 FROM EMP; This example shows salaries multiplied by 1.1 and labelled as NEW_SALARY, with the existing SALARY column relabelled as OLD_SALARY for comparison purposes. Exercise: develop and execute retrievals for the following queries. Make sure each aggregate column has its own proper name. Check each answer returned to ensure that it is correct. 1. For each project, display its project number and its length in days. (Note that the number of days between 2 dates is derived by subtracting one date from the other ). 2. For each department, display its department number and name in a single column in the format 'number : name' and with a column heading of DEPARTMENT. (Note that text strings are concatenated by the || operator; e.g the expression S1 || S2 || S3 results in a single text string containing the text strings S1, S2 and S3). 3. For each department, calculate what it’s budget would be if it were to receive an increase of 20% plus a base supplement of £10,000. ‘GROUP BY’ & EXTEND EXERCISES THAT INCLUDE THE SQL EQUIVALENTS OF OTHER ALGEBRA OPERATIONS Exercise: check each answer returned to ensure that it is correct : 1. Get the names of departments who have more than one employee and whose salary bill exceeds £32,500. (Include the number of employees and salary bill for each department to help you check your result). 2. How many employees are working on projects that started after 01-Jun-2005 ? 3. Get the department numbers of those departments whose budget is less than £100,000 and whose salary bill is greater than £32,000. 4. As for question 3, but include both the department’s name and its number in the answer. 5. Get the department numbers of those departments whose budget is greater than £150,000 or whose salary bill is less than £33,000. Can this be done ? If not, why not ? 6. Every employee working on project P4 is to get a bonus equal to 25% of their salary. List these employees by name with the amount of their bonus. 4
© Copyright 2026 Paperzz