03-Oracle Made Easy-Version-1.5 - SQL
03-Oracle Made Easy-Version-1.5 - SQL
(Version 1.5)
Practice Sheet
Version 1.5
W3coder.com
Web : W3coder.com, Email : [email protected]
Phone : +88 017 46044306, +88 01616044306
SELECT Query
INSERT
UPDATE
Data Manipulation Language (DML)
DELETE
MERGE
CREATE
ALTER
DROP Data Definition Language (DDL)
RENAME
TRUNCATE
GRANT
Data Control Language (DCL)
REVOKE
COMMIT
ROLLBACK Transaction Control
SAVEPOINT
To describe a table
To re-execute a command
SQL> /
SQL> @all_emp
Assuming you have made a lot of queries during a session and you want to save
everthing at once
Any other command you type will be stored inside the <filename>.txt. But before
this can happen, you need to do this:
So when you do the above, you can then exit sqlplus, and cat output.txt. The result will
show you all the commands or work you have done while in sqlplus.
SQL> ed
SQL> select empno,ename,job,sal,comm,from emp where sal > &&x and comm
> &&x
SQL> passw
SL Use Statements
Creating Users CREATE USER scott IDENTIFIED BY tiger;
• An application developer, for example, may have the following
system privileges:
– CREATE SESSION
1 User System Privileges – CREATE TABLE
– CREATE SEQUENCE
– CREATE VIEW
– CREATE PROCEDURE
CREATE ROLE manager;
Creating and Granting
2 GRANT create table, create view TO manager;
Privileges to a Role
GRANT manager TO DEHAAN, KOCHHAR;
Changing Your ALTER USER scott IDENTIFIED BY lion;
3
Password
GRANT select ON employees TO sue, rich;
Granting Object
4
Privileges GRANT update (department_name, location_id)
ON departments
SL Use Statements
1 CREATE TABLE dept
(deptno NUMBER(2),
dname VARCHAR2(14),
Creating Tables loc VARCHAR2(13));
DESCRIBE dept
2 SELECT table_name
FROM user_tables ;
SELECT *
FROM user_catalog ;
3 CREATE TABLE dept80
AS
SELECT employee_id, last_name,
Creating a Table by
salary*12 ANNSAL,
Using a Subquery
hire_date
FROM employees
WHERE department_id = 80;
4. Including Constraints:
NOT NULL
UNIQUE
PRIMARY KEY
FOREIGN KEY
CHECK
SL Use Statements
CREATE TABLE employees(
employee_id NUMBER(6),
first_name VARCHAR2(20),
1 Defining Constraints ...
job_id VARCHAR2(10) NOT NULL,
CONSTRAINT emp_emp_id_pk
PRIMARY KEY (EMPLOYEE_ID));
CREATE TABLE employees(
employee_id NUMBER(6),
last_name VARCHAR2(25) NOT NULL,
The NOT NULL salary NUMBER(8,2),
2
Constraint commission_pct NUMBER(2,2),
hire_date DATE
CONSTRAINT emp_hire_date_nn
NOT NULL,
CREATE TABLE employees(
employee_id NUMBER(6),
last_name VARCHAR2(25) NOT NULL,
email VARCHAR2(25),
The UNIQUE
3 salary NUMBER(8,2),
Constraint
commission_pct NUMBER(2,2),
hire_date DATE NOT NULL,
...
CONSTRAINT emp_email_uk UNIQUE(email));
CREATE TABLE departments(
The PRIMARY KEY department_id NUMBER(4),
4 Constraint department_name VARCHAR2(30)
CONSTRAINT dept_name_nn NOT NULL,
manager_id NUMBER(6),
5. Creating Views:
SL Use Statements
CREATE VIEW empvu80
AS SELECT employee_id, last_name, salary
FROM employees
1 Creating Views
WHERE department_id = 80;
DESCRIBE empvu80;
CREATE VIEW salvu50
Create a view by using AS SELECT employee_id ID_NUMBER, last_name NAME,
2 column aliases in the salary*12 ANN_SALARY
subquery. FROM employees
WHERE department_id = 50;
Retrieving Data from a SELECT *
3
View FROM salvu50;
SL Use Statements
CREATE SEQUENCE dept_deptid_seq
INCREMENT BY 10
START WITH 120
1 Creating a Sequence
MAXVALUE 9999
NOCACHE
NOCYCLE;
SELECT sequence_name, min_value, max_value,
increment_by, last_number
FROM user_sequences;
2 Confirming Sequences
The LAST_NUMBER column displays the next available sequence
number if NOCACHE is specified.
INSERT INTO departments(department_id,
department_name, location_id)
3 Using a Sequence VALUES (dept_deptid_seq.NEXTVAL, 'Support', 2500);
7. Manipulating Data:
SL Use Statements
INSERT INTO departments(department_id, department_name,
1 Inserting New Rows manager_id, location_id)
VALUES (70, 'Public Relations', 100, 1700);
INSERT INTO departments (department_id,
department_name )
Inserting Rows with VALUES (30, 'Purchasing');
2
Null Values
INSERT INTO departments
VALUES (100, 'Finance', NULL, NULL);
INSERT INTO employees (employee_id,
first_name, last_name,
email, phone_number,
hire_date, job_id, salary,
commission_pct, manager_id,
3 Inserting Special Values department_id)
VALUES (113,
'Louis', 'Popp',
'LPOPP', '515.124.4567',
SYSDATE, 'AC_ACCOUNT', 6900,
NULL, 205, 100);
INSERT INTO employees
VALUES (114,
Inserting Specific Date 'Den', 'Raphealy',
4
Values 'DRAPHEAL', '515.127.4561',
TO_DATE('FEB 3, 1999', 'MON DD, YYYY'),
'AC_ACCOUNT', 11000, NULL, 100, 30);
SL Use Statements
1 SELECT *
Selecting All Columns
FROM departments;
SL Use Statements
SELECT employee_id, last_name, job_id, department_id
1 Using the WHERE Clause FROM employees
WHERE department_id = 90 ;
Character strings and date values are enclosed in single quotation
marks. Character values are case sensitive, and date values are format
sensitive.
2 Character Strings and Dates Example :
SELECT last_name, job_id, department_id
FROM employees
WHERE last_name = 'Whalen';
= Equal To
> Greater than
>= Greater than or equal to
3 Comparison Conditions < Less than
<= Less than or equal to
<> Not equal to.
!= Not equal to.
SELECT last_name, salary
Using Comparison
4 FROM employees
Conditions
WHERE salary <= 3000;
Between .. And … Between two values (inclusive)
Other Comparison In Match any of a list of values
5
Conditions Like Match a character pattern
Is Null Is a null value.
SELECT last_name, salary
Using the BETWEEN
6 FROM employees
Condition
WHERE salary BETWEEN 2500 AND 3500;
SELECT employee_id, last_name, salary, manager_id
7 Using the IN Condition FROM employees
WHERE manager_id IN (100, 101, 201);
SELECT first_name
8 Using the LIKE Condition FROM employees
WHEREfirst_name LIKE 'S%';
SELECT last_name, manager_id
9 Using the NULL Conditions FROM employees
WHERE manager_id IS NULL;
AND Returns TRUE if both component conditions are true
10 Logical Conditions OR Returns TRUE if either component condition is true
NOT Returns TRUE if the following condition is false
SELECT employee_id, last_name, job_id, salary
11 Using the AND Operator FROM employees
WHERE salary >=10000
SL Use Statements
LOWER
Case-manipulation
1 UPPER
functions
INITCAP
CONCAT
SUBSTR
Character-manipulation LENGTH
2 INSTR
functions
LPAD | RPAD
TRIM
REPLACE
LOWER('SQL Course') sql course
Case Manipulation
3 UPPER('SQL Course') SQL COURSE
Functions
INITCAP('SQL Course') Sql Course
SELECT employee_id, last_name, department_id
FROM employees
WHERE last_name = 'higgins';
Using Case
4 no rows selected
Manipulation Functions
SELECT employee_id, last_name, department_id
FROM employees
WHERE LOWER(last_name) = 'higgins';
5 Character-Manipulation CONCAT('Hello', 'World') HelloWorld
Functions SUBSTR('HelloWorld',1,5) Hello
LENGTH('HelloWorld') 10
INSTR('HelloWorld', 'W') 6
LPAD(salary,10,'*') *****24000
RPAD(salary, 10, '*') 24000*****
11. Subqueries :
SL Use Statements
SELECT last_name
FROM employees
Using a Subquery WHERE salary >
1
(SELECT salary
FROM employees
WHERE last_name = 'Abel');
SELECT last_name, job_id, salary
FROM employees
WHERE job_id =
(SELECT job_id
Executing Single-Row
2 Subqueries FROM employees
WHERE employee_id = 141)
AND salary >
(SELECT salary
FROM employees
WHERE employee_id = 143);
SELECT last_name, job_id, salary
FROM employees
Using Group Functions
WHERE salary =
3 in a Subquery
(SELECT MIN(salary)
FROM employees);
SL Use Statements
A Cartesian product is formed when:
– A join condition is omitted
– A join condition is invalid
1 Cartesian Products – All rows in the first table are joined to all rows in the
second table
To avoid a Cartesian product, always include a valid join condition in a
WHERE clause.
Equijoin
Oracle Proprietary Non-equijoin
2
Joins (8i and prior): Outer join
Self join
Cross joins
SQL: 1999 Natural joins
3 Compliant Joins: Using clause
Full or two sided outer joins
Arbitrary join conditions for outer joins
SELECT employees.employee_id, employees.last_name,
employees.department_id, departments.department_id,
Retrieving Records
4 departments.location_id
with Equijoins
FROM employees, departments
WHERE employees.department_id = departments.department_id;
SELECT e.employee_id, e.last_name, e.department_id,
d.department_id, d.location_id
5 Using Table Aliases
FROM employees e , departments d
WHERE e.department_id = d.department_id;
SELECT e.last_name, e.salary, j.grade_level
FROM employees e, job_grades j
Retrieving Records
6 WHERE e.salary
with Non-Equijoins
BETWEEN j.lowest_sal AND j.highest_sal;
SL Use Statements
• AVG
• COUNT
Types of Group
1 • MAX
Functions
• MIN
• SUM
SELECT AVG(salary), MAX(salary),
Using the AVG and MIN(salary), SUM(salary)
2
SUM Functions FROM employees
WHERE job_id LIKE '%REP%';
Using the MIN and SELECT MIN(hire_date), MAX(hire_date)
3
MAX Functions FROM employees;
SELECT COUNT(*)
FROM employees
WHERE department_id = 50;