Oracle Database 11g SQL Fundamentals Lab 1
Transcript of Oracle Database 11g SQL Fundamentals Lab 1
![Page 1: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/1.jpg)
Oracle Database 11g
SQL Fundamentals – Lab 1
![Page 2: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/2.jpg)
2
Lab Rules
• You MUST attend in your section.
• Please commit to the lab start time.
• Oracle 11g INSTALLATION:
– Refer to “ Installation – Database.ppt” for database installation
![Page 3: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/3.jpg)
Structured Query Language
(SQL)
![Page 4: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/4.jpg)
4
SQL, PL/SQL, and SQL*PLUS
• SQL: Structured Query Language, What to do -NOT - How to do.
• PL/SQL: Procedural Language SQL, a completelanguage that contains loops, if conditions,variables, cursors, procedures andfunctions…etc.
• SQL Developer: An execution environment to writeSQL and PL/SQL (the program itself).
![Page 5: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/5.jpg)
Data retrieval command (DRC)
![Page 6: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/6.jpg)
6
Basic SELECT Statement
SELECT *|{[DISTINCT] column|expression [alias],...}
FROM table;
In its simplest form, a SELECT statement mustinclude the following:
• SELECT identifies what columns
• FROM identifies which table
![Page 7: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/7.jpg)
7
SELECT *
FROM departments;
Selecting All Columns
![Page 8: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/8.jpg)
8
Selecting Specific Columns
SELECT department_id, location_id
FROM departments;
In the SELECT clause, specify the columns that you want,
in the order in which you want them to appear in the output.
![Page 9: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/9.jpg)
9
Using Arithmetic Operators
SELECT last_name, salary, salary + 300
FROM employees;
…
• Note that the resultant calculated column SALARY+300 is not a new
column in the EMPLOYEES table; it is for display only. By default, the
name of a new column comes from the calculation “salary+300”
![Page 10: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/10.jpg)
10
* / + -
Operator Precedence
• Multiplication and division take priority over addition
and subtraction.
• Operators from the same priority are evaluated from
left to right.
• Parentheses are used to enforce prioritized
evaluation and to clarify statements.
![Page 11: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/11.jpg)
11
Operator Precedence
SELECT last_name, salary, 12*salary+100
FROM employees;
…
SELECT last_name, salary, salary+100*12
FROM employees;
![Page 12: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/12.jpg)
12
Using Parentheses
SELECT last_name, salary, 12*(salary+100)
FROM employees;
…
You can override the rules of precedence by using parentheses
to specify the order in which operators are executed.
![Page 13: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/13.jpg)
13
Defining a Null Value
• A null is a value that is unavailable, unassigned,unknown, or inapplicable.
• A null is not the same as zero or a blank space.
SELECT last_name, job_id, salary, commission_pct
FROM employees;
…
…
![Page 14: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/14.jpg)
14
SELECT last_name, 12*salary*commission_pct
FROM employees;
Null Values
in Arithmetic Expressions
Arithmetic expressions containing a null value evaluate to null.
…
…
![Page 15: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/15.jpg)
15
Defining a Column Alias
A column alias:
• Renames a column heading
• Is useful with calculations
• Immediately follows the column name
• The optional AS keyword may be used between the
column name and alias
• Requires double quotation marks if it contains spacesor special characters or is case sensitive
![Page 16: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/16.jpg)
16
Using Column Aliases
SELECT last_name "Name", salary*12 "Annual Salary"
FROM employees;
SELECT last_name AS name, commission_pct comm
FROM employees;
…
…
![Page 17: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/17.jpg)
17
Concatenation Operator
A concatenation operator:
• Concatenates columns or character strings toother columns
• Is represented by two vertical bars (||)
• Creates a resultant column that is a characterexpression
![Page 18: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/18.jpg)
18
Using the Concatenation Operator
SELECT last_name || job_id AS "Employees"
FROM employees;
…
![Page 19: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/19.jpg)
19
Literal Character Strings
• A literal is a character, a number, or a dateincluded in the SELECT list.
• Date and character literal values must be enclosedwithin single quotation marks.
• Each character string is output once for eachrow returned.
![Page 20: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/20.jpg)
20
Using Literal Character Strings
SELECT last_name || ' is a ' || job_id
AS "Employee Details"
FROM employees;
…
![Page 21: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/21.jpg)
21
Duplicate Rows
The default display of queries is all rows, including
duplicate rows.
SELECT department_id
FROM employees;
…
![Page 22: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/22.jpg)
22
Eliminating Duplicate Rows
Eliminate duplicate rows by using the DISTINCT keyword in the SELECT clause.
SELECT DISTINCT department_id
FROM employees;
![Page 23: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/23.jpg)
23
Eliminating Duplicate Rows
• You can specify multiple columns after the
DISTINCT qualifier. The DISTINCT qualifier affects
all the selected columns, and the result is every
distinct combination of the columns.
• You can not specify columns before the
DISTINCT qualifier.
![Page 24: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/24.jpg)
Restricting Data
![Page 25: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/25.jpg)
25
Limiting the Rows Selected
• Restrict the rows returned by using the WHERE clause.
• The WHERE clause follows the FROM clause. It consists
of three elements:
– Column name
– Comparison operator
– Column name, constant, or list of values
SELECT *|{[DISTINCT] column|expression [alias],...}
FROM table
[WHERE condition(s)];
![Page 26: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/26.jpg)
26
Using the WHERE Clause
SELECT employee_id, last_name, job_id, department_id
FROM employees
WHERE department_id = 90 ;
A WHERE clause contains a condition that must be met.
If the condition is true, the row meeting the condition is returned.
= Equal , < Less than , > Greater than , <> Not equal
![Page 27: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/27.jpg)
27
Character Strings and Dates
• Character strings and date values are enclosedin single quotation marks.
• Character values are case sensitive
• Date values are format sensitive.
SELECT last_name, job_id, department_id
FROM employees
WHERE last_name = 'Whalen';
► All character searches are case sensitive.
![Page 28: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/28.jpg)
28
SELECT last_name, salary
FROM employees
WHERE salary <= 3000;
Using Comparison Conditions
![Page 29: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/29.jpg)
29
Other Comparison Conditions
Operator
BETWEEN
...AND...
IN(set)
LIKE
IS NULL
Meaning
Between two values (inclusive),
Match any of a list of values
Match a character pattern
Is a null value
![Page 30: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/30.jpg)
30
The BETWEEN Condition
Use the BETWEEN condition to display rows based on
a range of values.
SELECT last_name, salary
FROM employees
WHERE salary BETWEEN 2500 AND 3500;
Lower limit Upper limit
Values specified with the BETWEEN condition are inclusive.
You must specify the lower limit first.
![Page 31: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/31.jpg)
31
SELECT employee_id, last_name, salary, manager_id
FROM employees
WHERE manager_id IN (100, 101, 201);
The IN Condition
▪ Use the IN membership condition to test for values in
a list.
▪ The IN operator can be used with any datatype.
![Page 32: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/32.jpg)
32
The LIKE Condition
• Use the LIKE condition to perform wildcard
searches of valid search string values.
• Search conditions can contain either literal characters or numbers:
– % denotes zero or many characters.
– _ denotes one character.
SELECT first_name
FROM employees
WHERE first_name LIKE 'S%';
![Page 33: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/33.jpg)
33
The LIKE Condition
– The following example displays the last namesand hire dates of all employees who joinedbetween January 1995 and December 1995:
SELECT last_name, hire_date
FROM employees
WHERE hire_date LIKE '%95';
![Page 34: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/34.jpg)
34
• You can combine pattern-matching characters.
Using the LIKE Condition
SELECT last_name
FROM employees
WHERE last_name LIKE '_o%';
![Page 35: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/35.jpg)
35
The NULL Conditions
Test for nulls with the IS NULL operator.
SELECT last_name, manager_id
FROM employees
WHERE manager_id IS NULL;
![Page 36: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/36.jpg)
36
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
You can use several conditions in one WHERE clause
using the AND and OR operators.
![Page 37: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/37.jpg)
37
Using the AND Operator
AND requires both conditions to be true.
SELECT employee_id, last_name, job_id, salary
FROM employees
WHERE salary >=10000
AND job_id LIKE '%MAN%';
![Page 38: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/38.jpg)
38
Using the OR Operator
SELECT employee_id, last_name, job_id, salary
FROM employees
WHERE salary >= 10000
OR job_id LIKE '%MAN%';
OR requires either condition to be true
![Page 39: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/39.jpg)
39
SELECT last_name, job_id
FROM employees
WHERE job_id
NOT IN ('IT_PROG', 'ST_CLERK', 'SA_REP');
Using the NOT Operator
![Page 40: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/40.jpg)
Sorting Data
![Page 41: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/41.jpg)
41
The ORDER BY Clause
• The ORDER BY clause is last in SELECT statement.
• The default sort order is ascending.
• You can sort by column name, expressions or aliases.
• Null values are displayed:
– Last for ascending order
– First for descending order
SELECT last_name, job_id, department_id, hire_date
FROM employees
ORDER BY hire_date;
![Page 42: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/42.jpg)
42
Sorting in Descending Order
SELECT last_name, job_id, department_id, hire_date
FROM employees
ORDER BY hire_date DESC ;
…
• The sort order can be reversed by using DESC.
![Page 43: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/43.jpg)
43
Sorting by Multiple Columns
• The order of ORDER BY clause list is order of sort.
• You can order by position, e.g. 2nd column in select clause.
• You can sort by a column that is not in the SELECT list.
SELECT last_name, job_id, department_id, hire_date
FROM employees
ORDER BY department_id, last_name;
SELECT last_name, job_id, department_id, hire_date
FROM employees
ORDER BY 2;
![Page 44: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/44.jpg)
44
General Syntax
SELECT { * | [DISTINCT] column [alias], …}
FROM table
[WHERE condition (s)]
[ORDER BY {column | exp | alias} [ASC|DESC]];
![Page 45: Oracle Database 11g SQL Fundamentals Lab 1](https://reader030.fdocuments.net/reader030/viewer/2022012016/615af564498c6100724454db/html5/thumbnails/45.jpg)
Thank You