Basic select statement LECTURE FIVE Basic select statement Basic SELECT Statements
Basic SELECT statement The simplest form of the SELECT statement must include; The SELECT clause; this specifies the columns that are to be retrieved from a table/relation The FROM clause; this specifies the table from which the specified columns are to be retrieved. Basic SELECT Statements
Basic SELECT Statements General syntax The generalised syntax of a basic select statement is as follows: SELECT expressions_and_columnList FROM table_name [WHERE some condition(s) is true] [GROUP BY columnList] [HAVING condition] [ORDER BY columnList [ASC | DESC]] ; Basic SELECT Statements
SELECT Statement Cont’d When writing the select statement, the following should be at the back of ones mind; SQL statements can be written on one or more lines SQL statements are not case sensitive, unless indicated; keywords are usually written in uppercase and other words in lowercase. New lines and indents can be used to readability purposes e.g. clauses are usually written on separate lines for readability purposes. Keywords can not be split or abbreviated. Basic SELECT Statements
SELECT Statement Cont’d Notes: SELECT and FROM clauses are mandatory The order of execution is FROM table(s) [WHERE condition] [GROUP BY group_by_expression] [HAVING group_condition] SELECT column_list [ORDER BY column_list] Basic SELECT Statements
Capabilities of a SELECT Statement Join – combines information from two or more tables. Projection – specifying columns to be displayed (achieved through the use of the SELECT clause). Selection – specifying rows to be retrieved (achieved through the use of the WHERE clause). Basic SELECT Statements
Confirming Relational Schemas in an Oracle Relational DB To view existing relations (tables): Select * from user_tables; N.B.: The purpose of each table is to store data that describes exactly one entity - a thing of significance about which information needs to be known. To view a particular relation schema: Describe employees; N.B.: Identify the various attribute data types. Basic SELECT Statements
Data Retrieval from one Table Four major scenarios of data retrieval from a single table: Retrieving all columns and all rows; Retrieving specific columns and all rows; Retrieving all columns and specific rows; Retrieving specific columns and specific rows. Basic SELECT Statements
(i) Retrieving all columns and all rows from the employees table. SELECT * FROM employees; Alternatively specify all columns of the employees table; separate columns with commas, order of columns does not matter. Basic SELECT Statements
Basic SELECT Statements (ii) Retrieving specific columns and all rows from the employees table. SELECT employee_id, job_id, last_name FROM Employees; Basic SELECT Statements
Basic SELECT Statements (iii) Retrieving all columns and specific rows from the employees table. SELECT * FROM Employees WHERE job_id = 'IT_PROG'; Basic SELECT Statements
Basic SELECT Statements (iv) Retrieving specific columns and specific rows from the employees table. SELECT employee_id, job_id, last_name FROM Employees WHERE Job_id = 'IT_PROG'; Basic SELECT Statements
Basic SELECT Statements SELECTION This is the SQL capability of specifying the rows one is interested in retrieving from a table (Row selection) This is done using the WHERE clause. General syntax: SELECT clause FROM table WHERE condition; Comparison operators are used to define conditions in the where clause. Basic SELECT Statements
Basic SELECT Statements Row Selection Cont’d Note Character strings and dates in the where clause must be enclosed in single quotation marks (‘ ‘). Numeric constants should not be enclosed in single quotation marks. All character searches are case sensitive. Basic SELECT Statements
Basic SELECT Statements Row Selection Cont’d Note Character strings and dates in the where clause must be enclosed in single quotation marks (‘ ‘). Numeric constants should not be enclosed in single quotation marks. All character searches are case sensitive. Basic SELECT Statements
Basic SELECT Statements Row Selection Cont’d Logical Conditions A logical condition combines the result of two component conditions to produce a single result based on them or inverts the result of a single condition. A row is returned only if the overall result of the condition is true. The three logical operators available in SQL are: NOT AND OR Basic SELECT Statements
Basic SELECT Statements Quiz Describe the intension of the following SQL queries: SELECT first_name,last_name, job_id, department_id FROM employees WHERE first_name >'Susan'; SELECT employee_id, last_name, job_id, salary FROM employees WHERE salary >= 2500 and job-id like '%MAN%'; SELECT last_name, job-id FROM employees WHERE job-id not in ('AC_ACCOUNT', 'FI_ACCOUNT'); Basic SELECT Statements
Using the DISTINCT Keyword Purpose – enables the retrieval of a unique set of tuple(s) or row(s). SELECT DISTINCT department_id FROM employees; You can specify multiple columns after the DISTINCT qualifier which results into distinct tuples. SELECT DISTINCT department_id, job_id Basic SELECT Statements
Arithmetic operators and Expressions The Arithmetic operators present in SQL include: +, -, *, / These can be used in any clause except the FROM clause; Same operator precedence rules that apply in SQL statements Arithmetic expression can contain column names, constant values and the arithmetic operator. Basic SELECT Statements
Using Expressions in a SELECT Clause Date expressions (+, –) SELECT last_name, hire_date, hire_date + 3 FROM employees WHERE job_id='ST_MAN'; Numeric expressions (+, –, * , / ) SELECT last_name, salary, salary + 300 FROM employees; Character expressions ( || ) SELECT first_name || last_name N.B: An expression that involves a null value returns a null value. Basic SELECT Statements
Basic SELECT Statements Using Column Aliases A column heading is changed by using a column aliase with the purpose of making the column heading more descriptive. Double quotation marks must be used when the aliase has spaces, is case sensitive or has special characters. Is useful for calculations May be defined using the AS keyword. SELECT first_name || ' ' || last_name "Name", salary*12 "Annual Salary" FROM employees; Basic SELECT Statements
Literal character strings A literal character is a character, number or date included in a SELECT list A data and character literal values must be enclosed in single quotation marks The literal character string value is output one for each row that is retrieved. Basic SELECT Statements
Using the ORDER BY clause Facilitates sorting of data results in a particular order / direction. The order by clause consists of column identifies that the result is to be sorted on, separated by commas. The column identifier can be a column name / expression / alias / position. Sorting is done in ascending (ASC) or descending (DESC) order on any combination of columns, regardless of whether that column appears in the result. Sorting is by default ascending. Basic SELECT Statements
Basic SELECT Statements ORDER BY Clause Cont’d Data Sorting Example: SELECT last_name, job_id, department_id, hire_date FROM employees WHERE job_id like '_A% ' ORDER BY hire_date; Note: The ORDER BY clause must be the last clause of the SQL statement. You can sort by multiple columns. Basic SELECT Statements