2. SELECT Data retrieval
INSERT
UPDATE
DELETE
Data manipulation language
(DML)
CREATE
ALTER
DROP
RENAME
TRUNCATE
Data definition language (DDL)
COMMIT
ROLLBACK
SAVEPOINT
Transaction control
GRANT
REVOKE
Data control language (DCL)
3. SELECT Retrieves data from the database
INSERT
UPDATE
DELETE
Enters new rows, changes existing rows, and removes unwanted rows
from tables in the database, respectively. Collectively known as
Data manipulation language(DML).
CREATE
ALTER
DROP
RENAME
TRUNCATE
Set up, change, and remove data structures from tables. Collectively
known as data definition language(DDL).
COMMIT
ROLLBACK
SAVEPOINT
Manage the changes made by DML statements. Changes to the data can
be grouped together into logical transactions
GRANT
REVOKE
Give or remove access rights to both the Oracle database and the
structures within it. Collectively known as data control language(DCL).
4. In its simplest form, a SELECT statement must include the following:
•A SELECT clause , which specifies the columns to be displayed
•A FROM clause, which specifies the table containing the columns listed
in the SELECT clause
SELECT is a list of one or more columns
* selects all columns
DISTINCT suppresses duplicates
column|expression selects the named column or the expression
alias gives selected columns different headings
FROM table specifies the table containing the columns
6. • SQL statements are not case sensitive.
• SQL statements can be on one or more lines.
• Keywords cannot be abbreviated or split across lines.
• Clauses are usually placed on separate lines.
• Indents are used to enhance readability.
7. SELECT *
FROM departments;
Selecting All Columns of All Rows
You can display all columns of data in a table by following the SELECT
keyword with an asterisk (*). In the example on the slide, the department
table contains four columns:
DEPARTMENT_ID,DEPARTMENT_NAME,MANAGER_ID, and LOCATION_ID.
The table contains seven rows, one for each department.
8. You can also display all columns in the table by listing all the columns after
the SELECT keyword. For example, the following SQL statement, like the
example on the slide, displays all columns and all rows of the
DEPARTMENTS table:
SELECT department_id, department_name, manager_id, location_id
FROM departments;
9. Create expressions with number and date data by using arithmetic
operators.
Operator Description
+ Add
- Subtract
* Multiply
/ Divide
11. Multiplication and division take priority over addition and subtraction.
•Operators of the same priority are evaluated from left to right.
•Parentheses are used to force prioritized evaluation and to clarify
statements.
* / + -
13. 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 is case sensitive
SELECT last_name AS name, commission_pct comm
FROM employees;
SELECT last_name "Name", salary*12 AS "Annual Salary"
FROM employees;
14. 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
SELECT last_name||job_id AS "Employees"
FROM employees;
SELECT last_name ||' is a '||job_id AS "Employee Details"
FROM employees;
SELECT last_name ||‘ : 1 Month salary = '||salary Monthly
FROM employees;
15. Eliminate duplicate rows by using the DISTINCT keyword in the SELECT clause.
SELECT DISTINCT department_id
FROM employees
SELECT DISTINCT department_id, job_id
FROM employees
16. Use the iSQL*Plus DESCRIBE command to display the structure of a table.
DESCRIBE employees;
17. Syntax
SELECT *|{[DISTINCT] column|expression alias],...}
FROM table
[WHERE condition(s)];
The WHERE clause can compare values in columns, literal values, arithmetic
expressions, or functions. It consists of three elements:
•Column name
•Comparison condition
•Column name, constant, or list of values
19. • Character strings and date values are enclosed in single quotation marks.
•Character values are case sensitive, and date values are format sensitive.
•The default date format is DD-MON-RR.
SELECT last_name, job_id, department_id
FROM employees
WHERE last_name ='Goyal';
20. Operator Meaning
= Equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
<> Not equal to
BETWEEN
...AND...
Between two values (inclusive)
IN(set) Match any of a list of values
LIKE Match a character pattern
IS NULL Is a null value
21. SELECT last_name, salary
FROM employees
WHERE salary <= 3000;
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);
22. SELECT employee_id, manager_id, department_id
FROM employees
WHERE last_name IN ('Hartstein’, 'Vargas');
% Represents any sequence of zero or more characters
_ Represents any single character
SELECT first_name
FROM employees
WHERE first_name LIKE ‘_S%';
SELECT first_name
FROM employees
WHERE first_name LIKE ‘%AR_';
23. Operator Meaning
AND Returns TRUE if both component
conditions are true
OR Returns TRUE if either component
conditions are true
NOT Returns TRUE if the following
condition is false
27. •Sort rows with the ORDER BY clause
– ASC : ascending order (the default order)
– DESC : descending order
•The ORDER BY clause comes last in the SELECT statement.
SELECT last_name, job_id, department_id, hire_date
FROM employees
ORDER BY hire_date;
SELECT last_name, job_id, department_id, hire_date
FROM employees
ORDER BY hire_date DESC;
28. SELECT employee_id, last_name, salary*12 annsal
FROM employees ORDER BY annsal;
SELECT last_name, department_id, salary
FROM employees
ORDER BY department_id, salary DESC;