Professional Documents
Culture Documents
Lecture 4: 1-Retrieving Data Using the SQL SELECT Statement 2-Restricting and Sorting Data
Objectives
Selection
Table 1 Join
Table 1
Table 1
Table 2
SELECT identifies the columns to be displayed FROM identifies the table containing those columns
Arithmetic Expressions
Create expressions with number and date data by using arithmetic operators.
Operator Description
+
* /
Add
Subtract Multiply Divide
Operator Precedence
SELECT last_name, salary, 12*salary+100 FROM employees;
A column alias:
Renames a column heading Is useful with calculations Immediately follows the column name (There can also be the optional AS keyword between the column name and alias.) Requires double quotation marks if it contains spaces or special characters or if it is case-sensitive
Concatenation Operator
A concatenation operator:
Links columns or character strings to other columns Is represented by two vertical bars (||) Creates a resultant column that is a character expression
SELECT FROM
Specify your own quotation mark delimiter Choose any delimiter Increase readability and usability
SELECT department_name || q'[, it's assigned Manager Id: ]' || manager_id AS "Department and Manager" FROM departments;
Duplicate Rows
Summary
Practice 1:
1- The following SELECT statement executes successfully: SELECT last_name, job_id, salary AS Sal FROM employees; True/False 2-The following SELECT statement executes successfully: SELECT * FROM job_grades; True/False 3-There are four coding errors in the following statement. Can you identify them? SELECT employee_id, last_name sal x 12 ANNUAL SALARY FROM employees;
Practice 1 :
4-The HR department needs a query to display all unique job codes from the EMPLOYEES table. 5-The HR department has requested a report of all employees and their job IDs. Display the last name concatenated with the job ID (separated by a comma and space) and name the column Employee and Title.
ORACLE
Objectives
Comparison Conditions
Operator = > Meaning Equal to Greater than
>=
< <= <> BETWEEN ...AND... IN(set) LIKE IS NULL
SELECT last_name, salary FROM employees WHERE salary BETWEEN 2500 AND 3500 ;
SELECT employee_id, last_name, salary, manager_id FROM employees WHERE manager_id IN (100, 101, 201) ;
Logical Conditions
Operator AND OR NOT Meaning Returns TRUE if both component conditions are true Returns TRUE if either component condition is true Returns TRUE if the following condition is false
Rules of Precedence
Operator 1 2 3 4 5 6 7 8 9 Meaning Arithmetic operators Concatenation operator Comparison conditions IS [NOT] NULL, LIKE, [NOT] IN [NOT] BETWEEN Not equal to NOT logical condition AND logical condition OR logical condition
Rules of Precedence
SELECT FROM WHERE OR AND last_name, job_id, salary employees job_id = 'SA_REP' job_id = 'AD_PRES' salary > 15000;
last_name, job_id, salary employees (job_id = 'SA_REP' job_id = 'AD_PRES') salary > 15000;
Sorting
Sorting in descending order:
SELECT last_name, job_id, department_id, hire_date FROM employees 1 ORDER BY hire_date DESC ;
Summary
SELECT *|{[DISTINCT] column|expression [alias],...} FROM table [WHERE condition(s)] [ORDER BY {column, expr, alias} [ASC|DESC]] ;
Practice 2:
1-The HR departments needs to find high-salary and low-salary employees. display the last name and salary of employees who earn between $5,000 and $12,000 and are in department 20 or 50. Label the columns Employee and Monthly Salary, respectively. 2- Create a report to display the last name, salary, and commission of all employees who earn commissions. Sort data in descending order of salary and commissions. 3- Display the last name of all employees who have both an a and an e in their last name. 4- Display the last name, job, and salary for all employees whose job is SA_REP or ST_CLERKand whose salary is not equal to $2,500, $3,500, or $7,000.