Simple Queries Chapter 1 Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates Sort data by using an ORDER BY clause Basic SELECT statement SELECT expression1, expression2, … FROM table An expression may involve one or more column(s), literal(s), and/or arithmetic operator(s), … ; eg. last_name, salary * 1.1 Example: SELECT last_name, first_name, commission_pct, commission_pct*1.2 FROM employees An * in the SELECT clause represents all columns, eg: SELECT * FROM employees; Aliases An alias can be provided for any expression in a SELECT clause and is used as an alternate column heading Keyword AS is optional when specifying alias Alias follows expression in SELECT clause If alias is made up of more than a single word then alias must be enclosed within double quotation marks Example: SELECT last_name, first_name, commission_pct*1.2 AS “Increased Comm” FROM employees; Literals Literals are actual values that can be used in an SQL statement May be numeric value, eg 2, 1.2 May be character strings, eg ‘Employee:’, ‘2’ Concatenation operator Concatenation operator || is used to join strings and is often used to format output Example: SELECT first_name || last_name || ‘ has commission rate of’ || commission_pct FROM employees; Normally good to use alias for expressions involving || operation for meaningful heading: SELECT first_name || last_name || ‘ has commission rate of’ || commission_pct AS “EMPLOYEE COMMISSIONS” FROM employees; DISTINCT Duplicate values may be returned by a SELECT statement. Use of DISTINCT allows duplicate rows of output to be eliminated. Example: list the department for all employees: SELECT department_id FROM employees; Output from query contains duplicate values of department # and is not clear. Use of DISTINCT in query removes duplicate rows from output: SELECT DISTINCT department_id FROM employees; ORDER BY Sort output by using an ORDER BY clause May sort by one or more expressions First expression is primary sort field, second expression is then used, … Example of listing employee names in alphabetical order: SELECT first_name || ‘ ’ || last_name AS NAME FROM employees ORDER BY last_name, first_name; But is this output in alphabetical order by last name and first name? Can’t tell because there are not 2 employees with the same last name! Testing of SQL Statements SQL is a programming language SQL statements must be tested just as you must test C or C++ programs It is not okay to assume that output and/or results produced by an SQL statement you write is correct! Many important business decisions may be made based on the output produced by SQL statements that you write – therefore every statement you write must be thoroughly tested. To test SQL statements you must have a wide range of data values in your test database Testing of SQL Statements (ctd) You should determine what results you expect based on your client’s requirements and the data contained in the database Then develop your SQL statement (often in stages) Then execute your statement Compare the output and/or results with what you predicted. If they are not the same then verify if the results you predicted are correct. If the predicted results are correct then you must find and correct a logical error in your SQL statement It is very easy to write syntactically valid but logically incorrect SQL statements Testing of ORDER BY Example of listing employee names in alphabetical order: SELECT first_name || ‘ ’ || last_name FROM employees ORDER BY last_name, first_name; How to test if the output produced by the query is in alphabetical order by last and first name? Must add another employee with same last name as an existing employee INSERT INTO employees VALUES(15, ‘John',‘King', ‘JKing@senecac.on.ca', NULL,'12-NOV-04', 'IT_PROG', NULL, NULL, NULL, NULL); Now rerun query and verify results ORDER BY clause (ctd) Expression(s) used in ORDER BY clause may or may not be included in SELECT clause Example: list employee names in order by department id and then for any employees located in the same department in alphabetical order by last and first name: SELECT first_name || ‘ ’ || last_name AS NAME FROM employees ORDER BY department_id, last_name, first_name; ORDER BY clause (ctd) Data may be sorted in ascending or descending order by using the keywords ASC or DESC Default order is ascending Example: list employee names from the highest numbered employee: SELECT first_name || ‘ ’ || last_name AS NAME FROM employees ORDER BY employee_id DESC