SQL Functions Aggregate Functions Some Basic Aggregate Functions OUTPUT COUNT() The number of rows containing non-null values MIN() The minimum attribute value encountered in a given column MAX() The maximum attribute value encountered in a given column SUM() The sum of all values for a given column AVG() The arithmetic mean (average) for a given column Table 1 COUNT [WHERE COUNT (columnname) tablename conditionlist] Example: How many students are born in New York? COUNT (*) WHERE CITY LIKE New York MIN and MAX Figure 1 Example (see Figure 1): 1. Which product has the highest price? MAX (P_PRICE) PRODUCT NOTE: The aggregate functions MAX(columnlist) and MIN(columnlist) can be used only in the column list of a statement 2. Which product has the lowest price? MIN (P_PRICE) PRODUCT
SUM Find the total value of all items carried in the inventory. SUM(P_QOH * P_PRICE) AS TOTALVALUE PRODUCT AVG AVG(P_PRICE) PRODUCT Date/Time Functions Selected Date/Time Functions YEAR(date_value) MONTH(date_value) DAY(date_value) DATE() NOW() Returns a four digit year Returns a two-digit month code Returns the number of the day Displays the current date Displays the current date and time. YEAR: List all students born in 1985 * WHERE YEAR(STUD_DOB) = 1985 MONTH: List all students born in November * WHERE MONTH(STUD_DOB) = 11 DAY: List all students born on the 14 th day of the month. * WHERE DAY(STUD_DOB) = 14 DATE(): List how many days are left until Christmas: #25-12-2014# - DATE() Add/subtract a number of days to a given date DATE() +/- 90
Numeric functions Selected Numeric Functions ABS(numeric_value) ROUND(numeric_value, p) p= precision INT(numeric_value) FIX(numeric_value) Returns the absolute value of a number Rounds a value to a specified precision Returns the integer portion of a number. if number is negative, INT returns the first negative integer less than or equal to number Returns the integer portion of a number. if number is negative, FIX returns the first negative integer greater than or equal to number ABS(-5.6) Output: 5.6 ROUND(PROD_PRICE, 1) PRODUCT INT(-5,6) Output: -6 FIX(-5.6) Output: -5 String Functions Selected String Function Concatenation string_value+ string_value UCASE and LCASE MID(string_value, p,l) p- start position ; l length of characters LEN(string_value) Concatenates date from two different character columns and returns a single value Returns a string with all capitals or lowercase letters Returns a substring or a part of a given string parameter Returns the number of characters in a string value List all student names (concatenated) STUD_FNAME +' '+ STUD_LNAME AS STUDNAME STUDENT List all student names in all capital letters UCASE(STUD_FNAME) +' '+ UCASE(STUD_LNAME) AS STUDNAME List the first three characters of all students phone numbers. MID(STUD_PHONE, 1, 3) AS PREFIX
List all students last names and the number of their characters in descending order by last name length. STUD_LNAME, LEN(STUD_LNAME) ORDER BY LEN(STUD_LNAME) DESC Problems: Use figure P1 to solve the following problems: Figure P1 Construct database 1. Write the SQL code to find the average bonus percentage in the EMP_2 table you created. 2. Write a query to count the number of employees hired from the company. 3. Write a query to count the number of projects with balance greater than 1 000 000. 4. Write a query to show the value of the most expensive project 5. Write a query to show the value of the most expensive project and the project s name. (Figure 2)(Hint: Use a nested query) Figure 2 6. Write a query to show first and last names, and hire date of the newly hired employee. Figure 3
7. Write the SQL code to fix the values in ASSIGN_CHARGE column in ASSIGNMENT table as shown in Figure P1 (Use numeric function ) 8. Count the number of different job codes assigned to project with number 15. (Hint: Use a nested query (see: lab 2) ) 9. Write the queries for the rest of the projects. (PROJ_NUM : 18, 22, 25) 10. List all employees names concatenated in all capital letters 11. Calculate the total money charged to the employees on 22.March.2012. 12. Calculate the total money charged to the employees on 22.March.2012 worked on Starflight project.