1 / 54

Database Design

Marge Hohly. 2. Concatenation. A concatenation operator:Concatenates columns or charter strings to other columnsIs represented by two vertical bars (||)Creates a resultant column that is a character expression. Marge Hohly. 3. Practice. SELECT last_name ||', '|| first_name FROM employees;Using the DJ on Demand EVENT table, create and display : "On date, the event was name

gore
Download Presentation

Database Design

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


    1. Database Design Sections 17 & 18 – Concatenations, DISTINCT, DESCRIBE, Logical Operators, Order of Operations, Sorting 10/3/06 10/3/06

    2. Marge Hohly 2 Concatenation A concatenation operator: Concatenates columns or charter strings to other columns Is represented by two vertical bars (||) Creates a resultant column that is a character expression

    3. Marge Hohly 3 Practice SELECT last_name ||', '|| first_name FROM employees; Using the DJ on Demand EVENT table, create and display : "On date, the event was name“ Run the command again using an alias Events for the column heading Create SQL to get the phase like “King earns $24000” from the employees table SELECT doesn’t change data Note that we have formatted the name to put the last name first and then the first name separated with a comma SELECT 'On ' ||event_date || ' the event was ' ||name FROM d_events; SELECT 'On ' ||event_date || ' the event was ' ||name AS "Events" FROM d_events point out “ around an alias, note the column heading is formatted. SELECT last_name ||' earns ' ||'$'||salary AS "Salary" FROM employees; Note: spaces inside the literal ‘ earns ‘ Note that we have formatted the name to put the last name first and then the first name separated with a comma SELECT 'On ' ||event_date || ' the event was ' ||name FROM d_events; SELECT 'On ' ||event_date || ' the event was ' ||name AS "Events" FROM d_events point out “ around an alias, note the column heading is formatted. SELECT last_name ||' earns ' ||'$'||salary AS "Salary" FROM employees; Note: spaces inside the literal ‘ earns ‘

    4. Marge Hohly 4 Practice database UCLA CREATE table UCLA (id number, name varchar2(20)); DESC ucla; INSERT INTO ucla values(1,'Joe'); values(2,‘Mary'); SELECT * FROM ucla; We can use the ucla table for practice during lecture Note name is case sensitiveNote name is case sensitive

    5. Marge Hohly 5 Using Literal Character Strings A literal is a character, a number, or a date included in the SELECT list. Date and character literal values must be enclosed within single quotation marks. Each character string is output once for each row returned.

    6. Marge Hohly 6 Using Literals SELECT id, 'hello‘ FROM ucla; hello is a literal This inserts values in a column of the UCLA table with the value ‘hello’ Try it. Try this: SELECT id, 15 FROM ucla

    7. Marge Hohly 7 DISTINCT: Eliminating duplicate rows The default display of queries is all rows, including duplicate rows. Use the DISTINCT keyword in the SELECT clause to eliminate duplicate rows. DISTINCT must appear just after the SELECT keyword.

    8. Marge Hohly 8 DISTINCT practice SELECT job_id FROM employees; Note the large number of rows returned Revise using DISTINCT SELECT DISTINCT job_id FROM employees; When DISTINCT is after the SELECT it applies to all other fields in the SELECT SELECT DISTINCT job_id, salary FROM employees;

    9. Marge Hohly 9 Limiting rows SELECT*|{[DISTINCT] column|expression [alias],...} FROM table [WHEREcondition(s)]; Example: SELECT DISTINCT year AS “Year” FROM d_cds WHERE year < 2000; Run command. Run command.

    10. Marge Hohly 10 Practice 17.1.10 Each statement below has errors. Correct the errors and execute the query in HTML DB. SELECT first name FROM f_staffs; SELECT first_name |"  " | last_name AS “DJs on Demand Clients” FROM d_clients; SELECT DISCTINCT f_order_lines FROM quantity; SELECT order number FROM f_orders; 2. a. Missing underscore first_name b. Incorrect single bar concatenation operator and double quotation marks c. DISTINCT incorrectly spelled and column and table names in wrong place d. Missing underscore order_number 2. a. Missing underscore first_name b. Incorrect single bar concatenation operator and double quotation marks c. DISTINCT incorrectly spelled and column and table names in wrong place d. Missing underscore order_number

    11. Marge Hohly 11 Using DESCRIBE (DESC) Use the DESCRIBE command to display the structure of a table. DESC[RIBE] tablename

    12. Marge Hohly 12 Practice 17.1.11 Which of the following is TRUE about the following query? SELECT first_name, DISTINCT birthdate FROM f_staffs; Only two rows will be returned. Four rows will be returned. Only Fred 05-JAN-88 and Lizzie 10-NOV-87 will be returned. No rows will be returned. d. No rows will be returned. DISTINCT must appear directly after SELECT. d. No rows will be returned. DISTINCT must appear directly after SELECT.

    13. Marge Hohly 13 Practice 17.2.4 True/False -- The following SELECT statement executes successfully: SELECT last_name, job_id, salary AS Sal FROM employees; True/False -- The following SELECT statement executes successfully: SELECT * FROM job_grades; There are four coding errors in this statement. Can you identify them? SELECT employee_id, last_name sal x 12 ANNUAL SALARY FROM employees; In the arithmetic expression salary*12 - 400, which operation will be evaluated first? Answers: 1. True 2. True 3. Missing comma after last_name, sal*12 and "ANNUAL SALARY", sal should be salary 4. salary*12 Answers: 1. True 2. True 3. Missing comma after last_name, sal*12 and "ANNUAL SALARY", sal should be salary 4. salary*12

    14. Marge Hohly 14 Practice 17.2.5 Which of the following can be used in the SELECT statement to return all columns of data in the Global Fast Foods f_staffs table? column names * DISTINCT id both a and b Using SQL to choose the columns in a table uses which capability? selection projection partitioning join SELECT department_id, first_name, last_name AS "Employee". The column heading in the query result will appear as: EMPLOYEE employee Employee "Employee: Answers: 5. d. both a and b 6. b. projection 7. c. Employee Answers: 5. d. both a and b 6. b. projection 7. c. Employee

    15. Marge Hohly 15 Practice 17.2.6 Which statement below will return a list of employees in the following format? Mr./Ms. Steven King is an employee of our company. SELECT "Mr./Ms."||first_name||' '||last_name 'is an employee of our company.' AS "Employees“ FROM employees; SELECT 'Mr./Ms. 'first_name,last_name ||' '||'is an employee of our company.' FROM employees; SELECT 'Mr./Ms. '||first_name||' '||last_name ||' '||'is an employee of our company.' AS "Employees“ FROM employees SELECT Mr./Ms. ||first_name||' '||last_name ||' '||"is an employee of our company." AS "Employees“ FROM employees Which expression below will produce the largest value? SELECT salary*6 + 100 SELECT salary* (6 + 100) SELECT 6(salary+ 100) SELECT salary+6*100 8. c. SELECT 'Mr./Ms. '||first_name||' '||last_name ||' '||'is an employee of our company.' AS "Employees" FROM employees 9. b. SELECT salary* (6 + 100) 8. c. SELECT 'Mr./Ms. '||first_name||' '||last_name ||' '||'is an employee of our company.' AS "Employees" FROM employees 9. b. SELECT salary* (6 + 100)

    16. Marge Hohly 16 Practice 17.2.7 Which is true about SQL statements? SQL statements are case-sensitive SQL clauses should not be written on separate lines. Keywords cannot be abbreviated or split across lines. SQL keywords are typically entered in lowercase; all other words in uppercase. Which query will return three columns each with UPPERCASE column headings? SELECT "Department_id", "Last_name", "First_name“ FROM employees; SELECT DEPARTMENT_ID, LAST_NAME, FIRST_NAME FROM employees; SELECT department_id, last_name, first_name AS UPPER CASE FROM employees SELECT department_id, last_name, first_name FROM employees; Answers: 10. c. Keywords cannot be abbreviated or split across lines. 11. b. SELECT DEPARTMENT_ID, LAST_NAME, FIRST_NAME FROM employees;      d. SELECT department_id, last_name, first_name FROM employees;Answers: 10. c. Keywords cannot be abbreviated or split across lines. 11. b. SELECT DEPARTMENT_ID, LAST_NAME, FIRST_NAME FROM employees;      d. SELECT department_id, last_name, first_name FROM employees;

    17. Marge Hohly 17 Practice 17.2.8 Which statement below will likely fail? SELCT * FROM employees; Select * FROM employees; SELECT * FROM EMPLOYEES; SelecT* FROM employees; You are now ready to take Quiz #7 12. a. SELCT * FROM employees;12. a. SELCT * FROM employees;

    18. Marge Hohly 18 Limiting Rows with WHERE The WHERE clause can compare values in columns, literal values, arithmetic expressions, or functions. It consists of three elements: Column name Comparison condition Expressions, constant, or list of values

    19. Marge Hohly 19 WHERE clause The WHERE clause follows the FROM clause in a SQL statement. An alias cannot be used in the WHERE clause! Example: SELECT last_name, first_name, department_id FROM employees WHERE department_id = 50;

    20. Marge Hohly 20 Comparison Operators The symbols != and ^= can also represent the not equal to condition.

    21. Marge Hohly 21 WHERE clauses Using comparison operators SELECT last_name,first_name,department_id FROM employees WHERE department_id >= 60; SELECT last_name,first_name,department_id FROM employees WHERE department_id <> 60; Write a SELECT statement to produce the following results: "The song title is "Another one bites the dust. Where ‘Another one bites the dust’ is the title of a song. Remember Oracle SQL is case sensitive SELECT 'The song title is " ' || title ||' " '||'.' FROM d_songs WHERE id = 51;SELECT 'The song title is " ' || title ||' " '||'.' FROM d_songs WHERE id = 51;

    22. Marge Hohly 22 BETWEEN ... AND BETWEEN...AND operator is used to select and display rows based on a range of values. BETWEEN ... AND are inclusive SELECT last_name,first_name,department_id FROM employees WHERE department_id BETWEEN 60 AND 90; SELECT last_name,first_name,department_id FROM employees WHERE department_id NOT BETWEEN 60 AND 90; SELECT title, year FROM   d_cds WHERE  year  BETWEEN '1999' AND '2001'; For BETWEEN 60 AND 90 Inclusive means that the 60 and 90 are included. 17.5.3 example point out Note that the year column is defined as a VARCHAR2 datatype. Because of this , the SQL statement could have been written without quotes around the year values: SELECT title, year FROM d_cds WHERE year BETWEEN 1999 AND 2001; The reason for this is because when Oracle compares a numeric literal (i.e., 1999) against a VARCHAR2 column, Oracle implicitly converts the data into a character (VARCHAR2) datatype.For BETWEEN 60 AND 90 Inclusive means that the 60 and 90 are included. 17.5.3 example point out Note that the year column is defined as a VARCHAR2 datatype. Because of this , the SQL statement could have been written without quotes around the year values: SELECT title, year FROM d_cds WHERE year BETWEEN 1999 AND 2001; The reason for this is because when Oracle compares a numeric literal (i.e., 1999) against a VARCHAR2 column, Oracle implicitly converts the data into a character (VARCHAR2) datatype.

    23. Marge Hohly 23 More Complex WHERE clause Example: "I would like to know Global Fast Foods employees whose manager ID is 19 and earn a salary less than $12.00 per hour. SELECT * FROM f_staffs WHERE manager_id = 19 and salary <12.00;

    24. Marge Hohly 24 WHERE clause using LIKE LIKE condition allows you to select rows that match either literal characters or number patterns. % and underscore (_ ) are wildcard characters, used to construct a search string. % symbol used to represent any sequence of zero or more characters. Underscore (_ ) symbol used to represent a single character.

    25. Marge Hohly 25 LIKE examples SELECT last_name,first_name,department_id FROM employees WHERE last_name LIKE 'A%'; SELECT last_name,first_name,department_id FROM employees WHERE last_name LIKE '%s‘; SELECT last_name,first_name,department_id FROM employees WHERE last_name LIKE '%s%‘; SELECT last_name,first_name,department_id FROM employees WHERE last_name LIKE ‘_ _ _ _ _s%‘ Using 5 underscores

    26. Marge Hohly 26 Using IS NULL, IS NOT NULL NULL is unavailable, unassigned, unknown, or inapplicable. No presence of data. NOT NULL test for presence of data in column/field. Examples: SELECT last_name,first_name,department_id, commission_pct FROM employees WHERE commission_pct is NULL; SELECT last_name,first_name,department_id, commission_pct FROM employees WHERE commission_pct is NOT NULL; NOTE: you can’t use = you must use is. Commission_pct = NULL (will not work)NOTE: you can’t use = you must use is. Commission_pct = NULL (will not work)

    27. Marge Hohly 27 17.5.11 & 17.5.12 Display the location type and comments for all DJs on Demand venues that are Private Home. Who are the partners of DJs on Demand who do not get an authorized expense amount? 2. SELECT  loc_type, comments FROM d_venues WHERE loc_type LIKE 'Private Home'; NOTE: limiting field/column is not displayed 5. SELECT  first_name, last_name FROM d_partners WHERE auth_expense_amt IS NULL;2. SELECT  loc_type, comments FROM d_venues WHERE loc_type LIKE 'Private Home'; NOTE: limiting field/column is not displayed 5. SELECT  first_name, last_name FROM d_partners WHERE auth_expense_amt IS NULL;

    28. Marge Hohly 28 IN is like OR’s IN is like a string of OR clauses. Example: SELECT last_name,first_name,department_id FROM employees WHERE department_id IN(50, 60, 90);

    29. Marge Hohly 29 Logical Comparisons A logical condition combines the results of two or more conditions to produce a single result. A row is returned ONLY IF the overall result of the condition is true. AND – Returns TRUE if both conditions are true. OR – Returns TRUE if either conditions are true. NOT – Returns TRUE if the condition is false.

    30. Marge Hohly 30 Precedence using AND & OR Evaluate these two examples. What is the difference? SELECT last_name, job_id, salary FROM employees WHERE job_id = ‘SA_REP’ OR job_id=‘AD_PRES’ AND salary>15000; SELECT last_name, job_id, salary FROM employees WHERE (job_id = ‘SA_REP’ OR job_id=‘AD_PRES’) AND salary>15000; AND before OR

    31. Marge Hohly 31 Logical Comparisons Logical conditions (a few examples) SELECT last_name, job_id, department_id FROM employees WHERE job_id = ‘SA_REP’ OR department_id = 10; SELECT last_name, job_id, SALARY FROM employees WHERE job_id = ‘SA_REP’ AND salary > 15000; Can you explain what rows are to be returned in each?

    32. Marge Hohly 32 Logical operator Write a select statement to find an address in the DJs on Demand database d_venues table that has the word "Primrose" in the description. SELECT  address FROM  d_venues WHERE address LIKE '%Primrose%‘; Variations: …WHERE cd_id NOT IN(105, 206, 332); …WHERE cd_id != 105 and cd_id != 206 and cd_id!= 332; Look at the data in the d_venues table before trying to do this problem.Look at the data in the d_venues table before trying to do this problem.

    33. Marge Hohly 33 Examples: Below are phrases and conditions in which they would not be true employee_id = 100 AND last_name LIKE 'S%'   Ms. Smith whose employee_id is 50 Employee_id 100 whose last name is King department = 10 AND employee_id  = 100; Employee_id 100 is in department 20 Employee_id 50 is in department 10 Take each example and find cases in which they would be FALSE. The false choices are listed below the conditions.Take each example and find cases in which they would be FALSE. The false choices are listed below the conditions.

    34. Marge Hohly 34 Complex AND and OR clause Take an arc Start with the arc condition then look at next slide. Start with the arc condition then look at next slide.

    35. Marge Hohly 35 Complex AND and OR clause Example Table: This again represents the ARC in a tabular form See next slideThis again represents the ARC in a tabular form See next slide

    36. Marge Hohly 36 Complex AND and OR clause WHERE clause example for ARC: WHERE ((A is NOT NULL and B is NULL and C is NULL) OR (A is NULL and B is NOT NULL and C is NULL) OR (A is NULL and B is NULL and C is NOT NULL)) /*--- comment ---*/ Precedence is AND before OR This would be the Compound AND/OR representing the ARC condition The results would be the same without the () because of precedence.This would be the Compound AND/OR representing the ARC condition The results would be the same without the () because of precedence.

    37. Marge Hohly 37 Rules of Precedence The rules of precedence determine the order in which expressions are evaluated and calculated. The table lists the default order, which can be overridden by using parentheses. KNOW THIS ONE. Might want to print it out to have when coding.KNOW THIS ONE. Might want to print it out to have when coding.

    38. Marge Hohly 38 Order of Operations Examples What order will these process in? SELECT last_name, specialty, auth_expense_amt FROM   d_partners WHERE  specialty ='All Types‘ OR     specialty IS NULL AND   auth_expense_amt = 300000; SELECT last_name, specialty, auth_expense_amt FROM   d_partners WHERE  (specialty ='All Types‘ OR     specialty IS NULL) AND   auth_expense_amt = 300000; The order of operations is: 1. Specialty IS NULL AND auth_expense_amt = 300000. Both these conditions must be met to be returned. 2. Any instance of specialty = 'All Types' will be returned. The order of operations is: 1. The values in the parentheses are selected. 2. All instances of the values in the parentheses that also match auth_expense_amt = 300000 will be returned.The order of operations is: 1. Specialty IS NULL AND auth_expense_amt = 300000. Both these conditions must be met to be returned. 2. Any instance of specialty = 'All Types' will be returned. The order of operations is: 1. The values in the parentheses are selected. 2. All instances of the values in the parentheses that also match auth_expense_amt = 300000 will be returned.

    39. Marge Hohly 39 Examples 18.1.8 & 18.1.9 “I need to know who the Global Fast Foods employees are that make more than $6.50/hour and their position is not Order Taker.“ What's my email address? Because I have been working for Oracle since the beginning of 1996, I make more than $9000 per month. Because I make so much money, I don't get a commission. 3. SELECT last_name FROM f_staffs WHERE salary > 6.50 AND staff_type != 'Order Taker'; 8. SELECT email FROM employees WHERE  hire_date BETWEEN '01-JAN-96' AND '31-DEC-96' AND salary > 9000 AND commission_pct IS NULL; MHARTSTE An alternative solution for #8 is: SELECT email FROM employees WHERE  hire_date > '01-JAN-96' AND salary > 9000 AND commission_pct IS NULL;3. SELECT last_name FROM f_staffs WHERE salary > 6.50 AND staff_type != 'Order Taker'; 8. SELECT email FROM employees WHERE  hire_date BETWEEN '01-JAN-96' AND '31-DEC-96' AND salary > 9000 AND commission_pct IS NULL; MHARTSTE An alternative solution for #8 is: SELECT email FROM employees WHERE  hire_date > '01-JAN-96' AND salary > 9000 AND commission_pct IS NULL;

    40. Marge Hohly 40 Examples: AND and OR conditions What are the titles of the jobs whose minimum salary is 4000 and whose maximum salary is 9000? Try this with OR and then AND Rewritten query including the salary columns to verify that both conditions are met: Note: you might want to review the jobs table fields/columns etc. 1. SELECT job_title,min_salary, max_salary (or just job_title) FROM jobs WHERE min_salary = 4000 or max_salary = 9000; 2. SELECT job_title,min_salary, max_salary FROM jobs WHERE min_salary = 4000 AND max_salary = 9000;1. SELECT job_title,min_salary, max_salary (or just job_title) FROM jobs WHERE min_salary = 4000 or max_salary = 9000; 2. SELECT job_title,min_salary, max_salary FROM jobs WHERE min_salary = 4000 AND max_salary = 9000;

    41. Marge Hohly 41 Sorting rows ORDER BY Information sorted by the column referenced. ORDER BY clause follows the FROM clause and is at the end of the SELECT statement. ORDER BY clause sorts in ascending order by default. Only limit to how many columns can be added to the ORDER BY clause is the number of columns in the table.

    42. Marge Hohly 42 ORDER BY Example of single value sort SELECT department_id, job_id, last_name, salary FROM employees ORDER BY department_id desc; Note: desc was used to denote descending order because ascending is the default.Note: desc was used to denote descending order because ascending is the default.

    43. Marge Hohly 43 ORDER BY multiple values SELECT department_id, job_id, last_name, salary FROM employees ORDER BY department_id, job_id, salary; SELECT department_id, job_id, last_name, salary FROM employees ORDER BY department_id, job_id desc, salary; SELECT department_id, job_id, last_name, salary FROM employees ORDER BY 3; The 3 is field from SELECT SELECT department_id, job_id, last_name, salary FROM employees ORDER BY 2, last_name; Note sort order for results. Note the use of a number in sort clause which denotes the field in that numeric position of the Select statement.Note sort order for results. Note the use of a number in sort clause which denotes the field in that numeric position of the Select statement.

    44. Marge Hohly 44 Sort order The default sort order is ascending. Numeric values are displayed lowest to highest. Date values are displayed with the earliest value first. Character values are displayed in alphabetical order. Null values are displayed last in ascending order and first in descending order.

    45. Marge Hohly 45 ORDER BY multiple values Place the following hire dates in descending order: 22-MAY-85, null, 10-JAN-04, 17-NOV-55, 21-DEC-98 Write a query to find the answer, using the employees table and hire_date column Answer: null, 10-JAN-04, 21-DEC-98, 22-MAY-85, 17-NOV-55 SELECT hire_date FROM employees ORDER BY hire_date DESC;Answer: null, 10-JAN-04, 21-DEC-98, 22-MAY-85, 17-NOV-55 SELECT hire_date FROM employees ORDER BY hire_date DESC;

    46. Marge Hohly 46 Order of execution in a SELECT statement The order of execution of a SELECT statement is as follows: FROM clause -- locates the table that contains the data WHERE clause -- restricts the rows to be returned SELECT clause -- selects from the reduced data set the columns requested ORDER BY -- orders the results set

    47. Marge Hohly 47 Example SELECT employee_id, first_name FROM employees WHERE employee_id < 105 ORDER BY last_name; What order does this execute in? FROM clause -- locates the table that contains the data WHERE clause -- restricts the rows to be returned SELECT clause -- selects from the reduced data set the columns requested ORDER BY -- orders the results setFROM clause -- locates the table that contains the data WHERE clause -- restricts the rows to be returned SELECT clause -- selects from the reduced data set the columns requested ORDER BY -- orders the results set

    48. Marge Hohly 48 18.2.7 & 18.2.8 Create a query that will return all the DJ on Demand CD titles ordered by year with titles in alphabetical order. Write a SQL statement using the employees table and the ORDER BY clause that could retrieve the information in the following table. Return only those employees with employee_id<125. 2. SELECT title, year FROM d_cds ORDER BY year, title; 5. SELECT department_id, last_name, manager_id FROM employees WHERE employee_id < 125 ORDER BY department_id DESC, last_name DESC; Discuss the graphics with students. Write SELECT statements for each. SELECT DISTINCT item_number, item_names ASC FROM inventory; SELECT last_name, salary DESC FROM employees ORDER BY salary DESC; 2. SELECT title, year FROM d_cds ORDER BY year, title; 5. SELECT department_id, last_name, manager_id FROM employees WHERE employee_id < 125 ORDER BY department_id DESC, last_name DESC; Discuss the graphics with students. Write SELECT statements for each. SELECT DISTINCT item_number, item_names ASC FROM inventory; SELECT last_name, salary DESC FROM employees ORDER BY salary DESC;

    49. Marge Hohly 49 Function classification Single-row and multi-row functions Single-row functions operate on a single row and return only one result per row. Multiple-row functions can manipulate groups of rows to give one result per group of rows. This section provides a brief overview of this topic. It is covered in depth in the next section.

    50. Marge Hohly 50 SQL Single row functions Character Converting character values from uppercase to lowercase Number rounding off Manipulate output for groups of rows by finding an average or sum for several rows Date Format dates and numbers for display Conversion Convert column data types such as converting a character string to a number or date General

    51. Marge Hohly 51 Single-row functions They can be used in SELECT, WHERE, and ORDER BY clauses. They return ONE result per row. They can return a data value of a different type. They can possibly expect one or more arguments or values sent to them. They act on each row returned in the query.

    52. Marge Hohly 52 Examples SELECT 'The most popular song in our collection is ' || UPPER(title)AS "Most Requested” FROM d_songs WHERE id = 47; SELECT CONCAT(title, year) FROM d_cds; 1. Most Requested (Column heading from Alias) The most popular song in our collection is HURRAH FOR TODAY 2. TITLE (column heading uses the first column in Concat.) The Celebrants Live in Concert1997 Party Music for All Occasions20001. Most Requested (Column heading from Alias) The most popular song in our collection is HURRAH FOR TODAY 2. TITLE (column heading uses the first column in Concat.) The Celebrants Live in Concert1997 Party Music for All Occasions2000

    53. Marge Hohly 53 Examples Use ‘ single quote when referring to data in a database Use “ “ for text as an alias if text has space or specific case etc. In an ORDER BY clause you can use an alias, expression, number, field names SELECT department_id, job_id AS job, last_name, salary*12 FROM employees ORDER BY job; SELECT department_id, job_id AS job, last_name, salary*12 FROM employees ORDER BY salary*12; SELECT department_id, job_id AS job, last_name, salary*12 FROM employees ORDER BY 3; Note ORDER BY was by ALIAS Note ORDER BY is using an expression 3 is third field in select statementNote ORDER BY was by ALIAS Note ORDER BY is using an expression 3 is third field in select statement

    54. Marge Hohly 54 Examples Fields used in ORDER BY don’t have to be used in SELECT clause SELECT last_name FROM employees WHERE salary > 4000 ORDER BY job_id; In development you might want to include the ORDER BY field in SELECT to verify that your getting the correct results, then remove the field.

    55. Marge Hohly 55 Single-row function example The single-row function is done for every row What would be returned from this statement? SELECT last_name || ' ' || first_name, salary,round(salary,-3) FROM employees WHERE salary>400; Round function has two arguments, first the field and 2nd the position to round off to. -3 rounds off to thousands (three to left of decimal)Round function has two arguments, first the field and 2nd the position to round off to. -3 rounds off to thousands (three to left of decimal)

More Related