• No results found

Oracle Database 11g SQL Fundamentals Lab 1

N/A
N/A
Protected

Academic year: 2021

Share "Oracle Database 11g SQL Fundamentals Lab 1"

Copied!
45
0
0

Loading.... (view fulltext now)

Full text

(1)

Oracle Database 11g

SQL Fundamentals – Lab 1

(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

(3)

Structured Query Language

(SQL)

(4)

SQL, PL/SQL, and SQL*PLUS

SQL: Structured Query Language, What to do - NOT - How to do.

PL/SQL: Procedural Language SQL, a complete language that contains loops, if conditions, variables, cursors, procedures and functions…etc.

SQL Developer: An execution environment to write

SQL and PL/SQL (the program itself).

(5)

Data retrieval command (DRC)

(6)

Basic SELECT Statement

SELECT *|{[DISTINCT] column|expression [alias],...}

FROM table;

In its simplest form, a SELECT statement must include the following:

SELECT identifies what columns

FROM identifies which table

(7)

SELECT *

FROM departments;

Selecting All Columns

(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.

(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”

(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.

(11)

Operator Precedence

SELECT last_name, salary, 12*salary+100 FROM employees;

SELECT last_name, salary, salary+100*12 FROM employees;

(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.

(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;

(14)

SELECT last_name, 12*salary*commission_pct FROM employees;

Null Values

in Arithmetic Expressions

Arithmetic expressions containing a null value evaluate to null.

(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 spaces

or special characters or is case sensitive

(16)

Using Column Aliases

SELECT last_name "Name", salary*12 "Annual Salary"

FROM employees;

SELECT last_name AS name, commission_pct comm FROM employees;

(17)

Concatenation Operator

A concatenation operator:

Concatenates columns or character strings to other columns

Is represented by two vertical bars (||)

Creates a resultant column that is a character

expression

(18)

Using the Concatenation Operator

SELECT last_name || job_id AS "Employees"

FROM employees;

(19)

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.

(20)

Using Literal Character Strings

SELECT last_name || ' is a ' || job_id AS "Employee Details"

FROM employees;

(21)

Duplicate Rows

The default display of queries is all rows, including duplicate rows.

SELECT department_id FROM employees;

(22)

Eliminating Duplicate Rows

Eliminate duplicate rows by using the DISTINCT keyword in the SELECT clause.

SELECT DISTINCT department_id FROM employees;

(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.

(24)

Restricting Data

(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)];

(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

(27)

Character Strings and Dates

Character strings and date values are enclosed in 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.

(28)

SELECT last_name, salary FROM employees

WHERE salary <= 3000;

Using Comparison Conditions

(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

(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.

(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.

(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%';

(33)

The LIKE Condition

The following example displays the last names and hire dates of all employees who joined between January 1995 and December 1995:

SELECT last_name, hire_date FROM employees

WHERE hire_date LIKE '%95';

(34)

You can combine pattern-matching characters.

Using the LIKE Condition

SELECT last_name FROM employees

WHERE last_name LIKE '_o%';

(35)

The NULL Conditions

Test for nulls with the IS NULL operator.

SELECT last_name, manager_id FROM employees

WHERE manager_id IS NULL;

(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.

(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%';

(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

(39)

SELECT last_name, job_id FROM employees

WHERE job_id

NOT IN ('IT_PROG', 'ST_CLERK', 'SA_REP');

Using the NOT Operator

(40)

Sorting Data

(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 orderFirst for descending order

SELECT last_name, job_id, department_id, hire_date FROM employees

ORDER BY hire_date;

(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.

(43)

Sorting by Multiple Columns

The order of ORDER BY clause list is order of sort.

You can order by position, e.g. 2

nd

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;

(44)

General Syntax

SELECT { * | [DISTINCT] column [alias], …}

FROM table

[WHERE condition (s)]

[ORDER BY {column | exp | alias} [ASC|DESC]];

(45)

Thank You

References

Related documents

Case statement makes sense if clause which we select and oracle determines which we would return null is selected from a condition is?. If arg1 is various character one then

Sensitive data that duplicate values of column names that table and a view definition must be used for example: in which columns of returned

This course provides the essential SQL skills that allow developers to write queries against single and multiple tables, manipulate data in tables, and create database

Exporting Schema using Oracle SQL Developer In Oracle SQL Developer click beyond the Tools menu then select create Database Export option The.. Oracle SQL Developer to generate an

Please advice would be using clause in oracle bulk limit clause in oracle database operations that something is act as used by index_row in bulk collect into t_fun_plsql

COUNT(*) returns the number of rows in a table that satisfy the criteria of the SELECT statement, including duplicate rows and rows containing null values in any of the

DEPARTMENTS table and drop the associated FOREIGN KEY constraint on the EMPLOYEES.DEPARTMENT_ID column.. ALTER TABLE

See select rows are skipped, sql server to skip header or query query wizard appears, google account and selecting the selection can neither be.. SQL select departmentid