Vrigin Kathleen A.

de Castro

ƒ

Grouping data is the process of combining columns with duplicate values in a logical order. Grouping data is accomplished through the use of the GROUP BY clause of a SELECT statement (query).

ƒ

ƒ

is used in collaboration with the SELECT statement to arrange identical data into groups follows the WHERE clause in a SELECT statement and precedes the ORDER BY clause

ƒ

The position of the GROUP BY clause in a query is as follows: SELECT FROM WHERE GROUP BY ORDER BY .

COLUMN2 ORDER BY COLUMN1. TABLE2 WHERE CONDITIONS GROUP BY COLUMN1. including the GROUP BY clause: SELECT COLUMN1.SELECT statement s syntax. COLUMN2 FROM TABLE1. COLUMN2 .

CITY ------------GREENWOOD INDIANAPOLIS WHITELAND INDIANAPOLIS INDIANAPOLIS INDIANAPOLIS 6 rows selected.You can see that there are three distinct cities in the EMPLOYEE_TBL table: SELECT CITY FROM EMPLOYEE_TBL. .

You see a count on each of the three distinct cities because you are using a GROUPBY clause: SELECT CITY.-------GREENWOOD INDIANAPOLIS WHITELAND 3 rows selected. COUNT(*) FROM EMPLOYEE_TBL GROUP BY CITY. CITY -------------.Select the city and a count of all records for each city. COUNT(*) 1 4 1 .

TINA LINDA BRANDON JACOB MARIAH TIFFANY PAY_RATE ----------14.A query from a temporary table SELECT * FROM EMP_PAY_TMP. CITY -----------GREENWOOD INDIANAPOLIS WHITELAND INDIANAPOLIS INDIANAPOLIS INDIANAPOLIS 6 rows selected.75 40000 20000 11 15 SALARY -----30000 Click . LAST_NAM -------STEPHENS PLEW GLASS GLASS WALLACE SPURGEON FIRST_NA ---------.

AVG(PAY_RATE) ------------13.Retrieve the average pay rate and salary on each distinct city using the aggregate function AVG. AVG(PAY_RATE).5833333 AVG(SALARY) ----------30000 20000 40000 . CITY -----------GREENWOOD INDIANAPOLIS WHITELAND 3 rows selected. AVG(SALARY) FROM EMP_PAY_TMP GROUP BY CITY. SELECT CITY.

WHITELAND ) GROUP BY CITY ORDER BY 2. AVG(PAY_RATE). AVG(SALARY) FROM EMP_PAY_TMP WHERE CITY IN ( INDIANAPOLIS . CITY -----------INDIANAPOLIS WHITELAND AVG(PAY_RATE) ------------13.SELECT CITY.5833333 AVG(SALARY) ----------20000 40000 .3.

MIN(SALARY) FROM EMP_PAY_TMP GROUP BY CITY.Shows the use of the MAX and MIN aggregate functions with the GROUP BY clause SELECT CITY. CITY -----------GREENWOOD INDIANAPOLIS WHITELAND 3 rows selected. MAX(PAY_RATE) ------------15 MIN(SALARY) ----------30000 20000 40000 . MAX(PAY_RATE).

SUM(SALARY) FROM EMPLOYEE_PAY_TBL GROUP BY 1.00 . SUM(SALARY) -----------------------40000.00 20000.00 30000.Like the ORDER BY clause. the GROUP BY clause can be ordered by using an integer to represent the column name. The following is an example of representing column names with numbers: SELECT YEAR(DATE_HIRE) as YEAR_HIRED. YEAR_HIRED ------------1989 1990 1991 1994 1996 1997 6 rows selected.

GROUP BY clause works the same as the ORDER BY clause in that both are used to sort data. the GROUP BY clause can be used to sort data the same as the ORDER BY clause. The GROUP BY clause also sorts data from a query to properly group the data. The ORDER BY clause is specifically used to sort data from a query. Therefore. .

CITY FROM EMPLOYEE_TBL GROUP BY LAST_NAME. FIRST_NAME ----------BRANDON JACOB LINDA TIFFANY TINA MARIAH CITY -----------WHITELAND INDIANAPOLIS INDIANAPOLIS INDIANAPOLIS GREENWOOD INDIANAPOLIS .SELECT statement to the GROUP BY clause: SELECT LAST_NAME. LAST_NAME -------GLASS GLASS PLEW SPURGEON STEPHENS WALLACE 6 rows selected. CITY. FIRST_NAME. FIRST_NAME.

CITY -----------GREENWOOD INDIANAPOLIS INDIANAPOLIS INDIANAPOLIS INDIANAPOLIS WHITELAND 6 rows selected. LAST_NAME FROM EMPLOYEE_TBL GROUP BY CITY.Shows a SELECT statement from EMPLOYEE_TBL and uses the GROUP BY clause to order by CITY: SELECT CITY. LAST_NAME. LAST_NAME --------STEPHENS GLASS PLEW SPURGEON WALLACE GLASS .

CITY ----------GREENWOOD WHITELAND INDIANAPOLIS COUNT(*) ----------1 1 4 . but ordered by the count on each city first: SELECT CITY.1.All employee records in the EMPLOYEE_TBL table are now counted. and the results are grouped by CITY. COUNT(*) FROM EMPLOYEE_TBL GROUP BY CITY ORDER BY 2.

Although GROUP BY and ORDER BY perform a similar function, there is one major difference. The GROUP BY clause is designed to group identical data, whereas the ORDER BY clause is designed merely to put data into a specific order. GROUP BY and ORDER BY can be used in the same SELECT statement, but must follow a specific order. The GROUP BY clause is always placed before the ORDER BY clause in the SELECT statement.

When used in conjunction with the GROUP BY clause in a SELECT statement, tells GROUP BY which groups to include in the output. HAVING is to GROUP BY as WHERE is to SELECT. In other words, the WHERE clause places conditions on the selected columns, and the HAVING clause places conditions on groups created by the GROUP BY clause.

The following is the position of the HAVING clause in a query:
SELECT FROM WHERE GROUP BY HAVING ORDER BY

including the HAVING clause: SELECT COLUMN1. COLUMN2 FROM TABLE1.The following is the syntax of the SELECT statement. TABLE2 WHERE CONDITIONS GROUP BY COLUMN1. COLUMN2 HAVING CONDITIONS ORDER BY COLUMN1. COLUMN2 .

you select the average pay rate and salary for all cities except GREENWOOD. CITY AVG(PAY_RATE) -----------------------WHITELAND 1 row selected. You group the output by CITY.In the following example. AVG(SALARY) FROM EMP_PAY_TMP WHERE CITY <> GREENWOOD GROUP BY CITY HAVING AVG(SALARY) > 20000 ORDER BY 3.000. AVG(PAY_RATE). AVG(SALARY) ----------40000 Click . You sort the results by average salary for each city: SELECT CITY. but only want to display those groups (cities) that have an average salary greater than $20.

.

For example. JOHN concatenated with SMITH produces JOHN SMITH.is the process of combining two separate strings into one string. you might want to concatenate an individual s first and last names into a single string for the complete name. .

Result: 'East Boston' .store_name) FROM Geography WHERE store_name = 'Boston'.Table Geography Example 1: MySQL/Oracle: SELECT CONCAT(region_name. Result: 'EastBoston' SELECT region_name + ' ' + store_name FROM Geography WHERE store_name = 'Boston'.

NAME ----------------STEPHENS. . MARIAH SPURGEON. SELECT LAST_NAME || . TINA PLEW. JACOB WALLACE. LINDA GLASS.This SQL Server statement concatenates the last name with the first name and inserts a comma between the two original values. || FIRST_NAME NAME FROM EMPLOYEE_TBL. TIFFANY 6 rows selected. BRANDON GLASS.

and then replaces that character with the new value. searches the replacement string at the same position. VALUE2) .searches a string of characters and checks for a specific character. makes note of the position found. VALUE1. The syntax is TRANSLATE(CHARACTER SET.

every occurrence of N with B. SELECT TRANSLATE CITY_TRANSLATION (CITY. ABC FROM EMPLOYEE_TBL) . and replaces all occurrences of D with C. IND .This SQL statement substitutes ever occurrence of I in the string with A.

ABC ) FROM EMPLOYEE_TBL. IND . CITY_TRANSLATION -----------GREEBWOOC ABCAABAPOLAS WHATELABC ABCAABAPOLAS ABCAABAPOLAS ABCAABAPOLAS . TRANSLATE(CITY. CITY -----------GREENWOOD INDIANAPOLIS WHITELAND INDIANAPOLIS INDIANAPOLIS INDIANAPOLIS 6 rows selected.SELECT CITY.

The use of this function is similar to the TRANSLATE function. except only one specific character or string is replaced within another string. .is used to replace every occurrence of a character(s) with a specified character(s).

This statement returns all of the cities in the employee table and the same cities with each I replaced with a Z: SELECT CITY. CITY -----------GREENWOOD INDIANAPOLIS WHITELAND INDIANAPOLIS INDIANAPOLIS INDIANAPOLIS 6 rows selected. Z ) FROM EMPLOYEE_TBL. REPLACE(CITY. I . REPLACE(CITY) ------------GREENWOOD ZNDZANAPOLZS WHZTELAND ZNDZANAPOLZS ZNDZANAPOLZS ZNDZANAPOLZS .

The UPPER function is used to convert lowercase letters to uppercase letters for a specific string. The syntax is as follows: UPPER(character string) .

This SQL statement converts all characters in the column to uppercase: SELECT UPPER(CITY) FROM EMPLOYEE_TBL. UPPER(CITY) ------------GREENWOOD INDIANAPOLIS WHITELAND INDIANAPOLIS INDIANAPOLIS INDIANAPOLIS 6 rows selected. .

The syntax is as follows: LOWER(character string) .is used to convert uppercase letters to lowercase letters for a specific string.

This SQL statement converts all characters in the column to lowercase: SELECT LOWER(CITY) FROM EMPLOYEE_TBL. LOWER(CITY) --------------greenwood indianapolis whiteland indianapolis indianapolis indianapolis 6 rows selected. .

For example. LENGTH) The concept of substring is the capability to extract part of a string.The syntax for SQL Server is SUBSTRING(COLUMN NAME. STARTING POSITION. or a sub of the string. the following values are substrings of JOHNSON:  J  JOHN  JO  ON  SON .

1. SUBSTRING(EMP_ID.3) FROM EMPLOYEE_TBL. EMP_ID ---------------311549902 442346889 213764555 313782439 220984332 443679012 6 rows affected. SUB -----------------311 442 213 313 220 443 .The following is an example that is compatible with Microsoft SQL Server and MySQL: SELECT EMP_ID.

.[ START POSITION [ . The syntax is as follows: INSTR(COLUMN NAME. OCCURRENCE ] ]).is used to search a string of characters for a specific set of characters and report the position of those characters. SET .

This SQL statement looks for the first occurrence of the letter A in the PROD_DESC column: Click .

1. INSTR(PROD_DESC.1) FROM PRODUCTS_TBL.1. A . INSTR(PROD_DESC.SELECT PROD_DESC.1) -----------------------------------0 3 2 10 1 2 10 3 1 7 2 . PROD_DESC ----------------------------------WITCHES COSTUME PLASTIC PUMPKIN 18 INCH FALSE PARAFFIN TEETH LIGHTED LANTERNS ASSORTED COSTUMES CANDY CORN PUMPKIN CANDY PLASTIC SPIDERS ASSORTED MASKS KEY CHAIN OAK BOOKSHELF 11 rows selected. A .

set ]) . The syntax is LTRIM(CHARACTER STRING [ .LTRIM ƒ is used to trim characters from the left of a string.

SALES ) FROM EMPLOYEE_PAY_TBL. POSITION ----------------------MARKETING TEAM LEADER SALES MANAGER SALESMAN SHIPPER SHIPPER 6 rows selected. ---------------------------MARKETING TEAM LEADER MANAGER MAN HIPPER HIPPER .This SQL statement returns the positions and also the returns the position with the word SALES trimmed from the left side of the character string: SELECT POSITION. LTRIM(POSITION. LTRIM(POSITION.

set ]) . The syntax is RTRIM(CHARACTER STRING [ .is used to trim characters. but this time from the right of a string.

RTRIM(POSITION.This SQL statement returns a list of the postions in the PAY_TBL as well as the positions with the letters ER trimmed from the right of the character string: SELECT POSITION. RTRIM(POSITION. ER ) FROM EMPLOYEE_PAY_TBL. POSITION --------------MARKETING TEAM LEADER SALES MANAGER SALESMAN SHIPPER SHIPPER 6 rows selected. MARKETING TEAM LEAD SALES MANAG SALESMAN SHIPP SHIPP .

an alternative string is displayed as part of the query results. The syntax is DECODE(COLUMN NAME. RETURN1 . DEFAULT VALUE ]) .[ SEARCH2 . RETURN2 . SEARCH1 . and if the string is found.is used to search a string for a value or string.

DECODE is used on the values for CITY in EMPLOYEE_TBL: SELECT CITY. GREENWOOD .In the following example. INDIANAPOLIS . INDY . DECODE(CITY. CITY -----------------GREENWOOD INDIANAPOLIS WHITELAND INDIANAPOLIS INDIANAPOLIS INDIANAPOLIS 6 rows selected. DECOD --------------------GREEN INDY OTHER INDY INDY INDY . GREEN . OTHER ) FROM EMPLOYEE_TBL.

is a common function used to find the length of a string. date. number. The syntax is ƒ LENGTH(CHARACTER STRING) . or expression in bytes.LENGTH .

This SQL statement returns the product description and also its corresponding length: Click .

LENGTH(PROD_DESC) FROM PRODUCTS_TBL.SELECT PROD_DESC. PROD_DESC --------------------------WITCHES COSTUME PLASTIC PUMPKIN 18 INCH FALSE PARAFFIN TEETH LIGHTED LANTERNS ASSORTED COSTUMES CANDY CORN PUMPKIN CANDY PLASTIC SPIDERS ASSORTED MASKS KEY CHAIN OAK BOOKSHELF 11 rows selected. LENGTH(PROD_DESC) -------------------------------15 23 19 16 17 10 13 15 14 9 13 .

the value and the substitute must be the same data type.is used to return data from one expression if another expression is NULL. The syntax is IFNULL( VALUE . IFNULL can be used with most data types. however. SUBSTITUTION ) .

9999999999) FROM EMPLOYEE_TBL. PAGER -------------------3175709980 8887345678 IFNULL(PAGER. -------------------------9999999999 9999999999 3175709980 8887345678 9999999999 9999999999 6 rows selected.This SQL statement finds NULL values and substitutes 9999999999 for any NULL values: SELECT PAGER. IFNULL(PAGER. .

. can accept a whole set of values and checks each one in order until it finds a non-NULL result. COALESCE returns a NULL value.is similar to the IFNULL function in that it is used to specifically replace NULL values within the result set. however. If a nonNULL result is not present. The COALESCE function.

EMP_ID -------------------------213764555 220984332 311549902 313782439 442346889 443679012 6 rows selected.00 1000.75 15.00 11.00 .The following example demonstrates the COALESCE function by giving us the first non-NULL value of BONUS.PAY_RATE) FROM EMPLOYEE_PAY_TBL.PAY_RATE) --------------------------------------------------2000.SALARY. COALESCE(BONUS.00 14.SALARY.00 40000. SALARY. COALESCE(BONUS. and PAY_RATE: SELECT EMP_ID.

totalling 30 characters between the actual value and padded periods: . The syntax is LPAD(CHARACTER SET) The following example pads periods to the left of each product description.LPAD (left pad) is used to add characters or spaces to the left of a string.

........FALSE PARAFFIN TEETH .................SELECT LPAD(PROD_DESC.................................. PRODUCT ----------------------------------------......................CANDY CORN .OAK BOOKSHELF 11 rows selected............ASSORTED MASKS ..LIGHTED LANTERNS ..........................KEY CHAIN .............30..... . ) PRODUCT FROM PRODUCTS_TBL..PUMPKIN CANDY .ASSORTED COSTUMES ........PLASTIC SPIDERS .......... ....WITCHES COSTUME ..PLASTIC PUMPKIN 18 INCH ...

totalling 30 characters between the actual value and padded periods: . The syntax is RPAD(CHARACTER SET) The following example pads periods to the right of each product description.The RPAD (right pad) is used to add characters or spaces to the right of a string.

..... ) PRODUCT FROM PRODUCTS_TBL........... ASSORTED COSTUMES.... ........ PLASTIC SPIDERS....... LIGHTED LANTERNS........ PLASTIC PUMPKIN 18 INCH.. OAK BOOKSHELF.... 11 rows selected....... FALSE PARAFFIN TEETH......... CANDY CORN......SELECT RPAD(PROD_DESC... PRODUCT ------------------------------------------WITCHES COSTUME..... ASSORTED MASKS............................................. ....30................................ KEY CHAIN........ PUMPKIN CANDY...

1. whereas character string data types are left. Arithmetic expressions and functions can be used on numeric values. Numeric .justified in the output results.You should notice two things regarding the differences between numeric data types and character string data types: 1. values are right-justified.

The following is an example of a numeric conversion using an Oracle conversion function: SELECT EMP_ID. TO_NUMBER(EMP_ID) FROM EMPLOYEE_TBL. EMP_ID ----------------311549902 442346889 213764555 313782439 220984332 443679012 6 rows selected. ` TO_NUMBER(EMP_ID) --------------------------311549902 442346889 213764555 313782439 220984332 443679012 .

75 18.5 14. NEW_PAY = STR(PAY_RATE) FROM EMPLOYEE_PAY_TBL WHERE PAY_RATE IS NOT NULL.75 18.SELECT PAY = PAY_RATE. PAY -------17. NEW_PAY ----------------17.8 11 15 .8 11 15 6 rows affected.25 12.5 14.25 12.

.Thank you and God Bless us All!...

Sign up to vote on this title
UsefulNot useful