Introduction To SQL: Stone Apple Solutions Pte LTD
Introduction To SQL: Stone Apple Solutions Pte LTD
Introduction To SQL: Stone Apple Solutions Pte LTD
4/15/2018 1
Agenda
Day 1
• SQL Introduction with RDBMS concept
• SQL Data Selection
• SQL Restriction and Sorting Data
• SQL Functions
• SQL Joining Tables
Day 2
• SQL Sub-query
• SQL Grouping Functions
• SQL Data Manipulation Language
• SQL Index
• SQL Object
SQL Introduction with RDBMS concept
• Relational Database
Terminology
For example you might want to store information about all the employees in your
company, in a relational database. You could create several tables to store different
pieces of information about your employees.
• Relational Database
Terminology
Example:
One To Many Relationship
Each Emp may have zero, one or multiple orders. But an order can belong to
only one Emp.
Many To Many Relationship
The Items_Orders table has only one purpose, and that is to create a "Many
to Many" relationship between the items and the orders.
Many To Many Relationship
Example:
• Connect to database: For testing, use scott as user name and tiger as
password.
Connection String
SQL Syntax
SQL Syntax is easy and most of the database action you can done
using SQL statement. Following are fewer most frequently useful
SQL Query syntax,
SQL Syntax
SQL Syntax
SQL Syntax
SQL Syntax
SQL Syntax
SQL Syntax
SQL Syntax
SQL Syntax
SQL Syntax
SQL Syntax
SQL Syntax
Exercise
• 1.
• 2
Exercise
• 3
SQL Restricting and Sorting
Restricting Data
Restricting Data
SQL WHERE Clause Statement
Syntax
Considering following syntax that help you to understanding WHERE clause.
SELECT * FROM table_name WHERE condition;
Example
SELECT * FROM items WHERE item_name = ‘Laptop‘;
SQL WHERE Clause Statement
Exercise:
Create sql for deptno equal to 20 on emp table.
The output should shows like below:
SQL IN
SQL IN condition used to allow multiple value in a WHERE clause
condition. SQL IN condition you can use when you need to use
multiple OR condition.
SQL IN condition allow only specific value in INSERT, UPDATE,
DELETE, SELECT statement.
Syntax
WHERE column_name IN (value1, value2, ...);
Example
SELECT * FROM items WHERE item_id IN (100,101);
SQL IN
Exercise:
Create sql to shows only deptno ‘10’ and ‘20’ on
emp table.
The output should shows like below:
SQL Not IN
SQL NOT IN condition used to exclude the defined multiple value in a
WHERE clause condition. SQL NOT IN condition also identify by NOT
operator.
Syntax
NOT IN condition use with WHERE clause to exclude defined multiple
values from record data.
WHERE column_name NOT IN (value1, value2, ...);
Syntax
Considering following syntax that help you to
understanding BETWEEN clause,
SELECT * FROM table_name WHERE column_name
BETWEEN lower_bound_value AND upper_bound_value;
SQL Between
When SQL query with BETWEEN clause parse into SQL Buffer,
it will automatically expand out into separate comparison
clauses.
SELECT * FROM table_name
WHERE column_name <= lower_bound_value AND
column_name >= upper_bound_value;
Example
In this example select all items table rows with range between
no column value 101 to 103.
SELECT * FROM items WHERE item_id BETWEEN 101 AND
103;
SQL Between
Exercise:
Create sql to shows deptno between ’10’ and
‘20’ on emp table.
The output should shows like below:
SQL Like
Null Condition
Logical Condition
Logical Condition
Logical Condition
Sorting
Sorting
Exercise
• 1
• 2
Exercise
• 3
SQL Functions
SQL Functions
Oracle SQL provide built-in SQL functions. SQL Functions
take some values as a arguments, perform some function
logic and returning some values. SQL built-in function are
so many that all are help us for no need to implementing
your own logic.
Example:
Select upper(ename) from emp;
Exercise
1. Create sql to change dept name ‘Accounting’
to ‘Oracle Developer’ on dept table.
The output should be like below:
select REPLACE (dname, 'ACCOUNTING', 'Oracle Developer') DNAME
from dept
Example:
Select avg(sal) from emp;
SQL Numeric Functions
Exercise:
Create sql to calculate all amount on orders
table.
The output should be like below:
SQL Date Functions
SQL Date Functions
SQL Date Functions
SQL Date Functions
General Functions
NVL Function
NVL2 Function
NVLLIF Function
Coalesce Function
Conditional Expressions
Case Expression
Decode Functions
Nesting Functions
Data Type Conversion
Exercise
• 1
• 2
• 3
Exercise
• 4
SQL Joining Tables
SQL Joining Tables
Type of Join
Kemungkinan untuk one to many lebih besar, jadi join yang gk terlihat
secara langsung joinnya
Equijoin
Equijoin
Non - Equijoin
Non - Equijoin
Exercise
Exercise:
Create sql Equi join on emp table and dept table.
The output should shows like below:
SQL Outer Join
Outer Join is a join two table involving common
attributes from two tables. But tables (Table A)
does not require to have a matching value to
other table (Table B).
SQL Left Join (Left Outer Join) always contains all
records of left table (Table A) even of join
condition does not find any matching record in
right table (Table B).
SQL Outer Join
SQL Outer Join
Example Statement :
SELECT * FROM emp
LEFT OUTER JOIN dept
ON emp.deptno = dept.deptno;
Example Statement :
SELECT * FROM emp, dept
WHERE emp.deptno = dept.deptno(+);
SQL Outer Join
SQL Outer Join
Exercise:
Create sql outer join to show all data on emp table and relate it to the dept
table.
The output should be like below:
a) b)
SQL Self Join (sama dgn outer join
hanya ke tbl yang sama)
SQL Self join joining same table to itself. SQL Self
join possible only when table stored records are
in hierarchical relationship between them.
You must use table alias name otherwise you
can't distinguish columns name (referenced of
which table's).
Logically assume that same table have two
different copies but not actually different copies.
SQL Self Join
SQL self join example statement :
SELECT A.order_number, A.order_date, B.order_number, B.order_date,
B.amount
FROM orders A, orders B
WHERE A.order_number = B.order_number;
Example :
If you want the number of employees in a particular department, the
query would be:
Select count(*)
From emp
Where deptno = 20;
Exercise:
Create sql to calculate all amount on orders
table with employee’s dept is ‘Accounting’.
The output should be like below:
SQL GROUP Functions
SQL GROUP Functions
SQL GROUP Functions
Exercise
• 2
SQL Data Manipulation Language
SQL Data Manipulation Language
SQL Data Manipulation Language
SQL INSERT
Syntax
When inserting data into table no need to specify the column
names if value is table structure wise (column wise).
INSERT INTO table_name VALUES (value1, value2, value3, ...);
Example
INSERT INTO items VALUES (106, ‘Helm’, ‘AGV‘,);
INSERT INTO statement
Syntax
When you inserting data into table and you haven't know
table structure you must specify the column name.
INSERT INTO table_name [ (column_name1,
column_name2, ...) ]
VALUES (value1, value2, ...);
Example
INSERT INTO items (item_id, item_name, description)
VALUES (107, ‘Trouser', ‘LEA');
INSERT ALL statement
Using INSERT ALL statement to insert more then one records into
table.
Syntax
Now we insert more then one record insert in single SQL INSERT
statement.
INSERT ALL
INTO table_name [ (column_name1, column_name2, ...) ] VALUES
(record1_value1, record1_value2, ...)
INTO table_name [ (column_name1, column_name2, ...) ] VALUES
(record2_value1, record2_value2, ...)
INTO table_name [ (column_name1, column_name2, ...) ] VALUES
(record3_value1, record3_value2, ...) ....
INSERT ALL statement
Example
INSERT ALL
INTO items (item_id, item_name) VALUES (108,
‘Chair')
INTO items (item_id, item_name) VALUES (109,
‘Glass')
INTO items (item_id, item_name) VALUES (110,
‘Umbrella')
INTO items (item_id, item_name) VALUES (111,
‘Cupboard');
INSERT INTO SELECT Statement
INSERT INTO SELECT Statement to insert data that data you are getting from
another table.
Syntax:
INSERT INTO new_table_name [(column_name1,column_name2,...)]
SELECT column_name1, column_name1 ... FROM another_table_name
[WHERE condition];
Example Statement:
CREATE TABLE demo_tbl( item_id NUMBER(4), item_name VARCHAR2(30),
description varchar2(100) );
Syntax :
UPDATE table_name
SET column_name1 = value1, column_name2 = value2, ...;
Example Statement :
UPDATE orders SET amount = 5000;
UPDATE Conditionally use WHERE Clause
UPDATE table data only where WHERE Clause condition match. User
can optionally use LIMIT number to update only limited record starting
from first.
Syntax :
UPDATE table_name
SET column_name1 = value1, column_name2 = value2, ...
[ WHERE condition ] [ LIMIT number];
Example Statement :
UPDATE orders
SET amount = 4000
WHERE order_number = ‘B1234’;
UPDATE with Sub Queries Statement
The sub query can be used in conjunction with the UPDATE statement.
Either single or multiple columns in a table can be updated when using
a sub query with the UPDATE statement.
Example:
Assuming, we have ORDERS_BKP table available which is backup of
ORDERS table.
UPDATE with Sub Queries Statement
UPDATE ORDERS
SET AMOUNT = AMOUNT * 0.25
WHERE ORDER_NUMBER IN (SELECT ORDER_NUMBER FROM
ORDERS_BKP WHERE ORDER_NUMBER = ‘A1234’ );
SQL DELETE Statement
SQL DELETE Statement is used to delete one or more then one row
removed from table.
SQL DELETE Statement use following two way,
• Remove all TABLE rows
• Remove only specific TABLE row
Syntax :
DELETE FROM table_name
[ WHERE condition ] [ LIMIT number ];
Example Statement :
DELETE FROM items WHERE item_id = 100;
Remove all TABLE rows
Syntax :
DELETE FROM table_name;
Example Statement :
DELETE FROM items;
DELETE with Sub Queries Statement
The sub query can be used in conjunction with the DELETE statement like
with any other statements mentioned above.
The basic syntax is as follows:
DELETE FROM TABLE_NAME
[ WHERE OPERATOR [ VALUE ]
(SELECT COLUMN_NAME FROM TABLE_NAME)
[ WHERE) ]
Example:
Assuming, we have ORDERS_BKP table available which is backup of ORDERS
table.
Following example delete records from ORDERS table for all orders whose
amount is greater to 1000:
DELETE with Sub Queries Statement
Syntax :
CREATE INDEX index_name
ON table_name (column_name)
[ storage_setting ];
Storage setting specifies the table space explicitly. This are the optional storage setting
if you are not specifies automatically default storage setting used.
Example Statement :
CREATE INDEX index_item_ids ON items (item_id);
We are creating simple index on name column of the items table. In this column allow
duplicate values of the column.
Composite INDEX
Syntax :
CREATE INDEX index_name
ON table_name (column_name, column_name)
[ storage_setting ];
Example Statement :
CREATE INDEX index_items
ON items (item_id, item_name);
Syntax :
CREATE UNIQUE INDEX index_name
ON table_name (column_name)
[ storage_setting ];
Example Statement :
CREATE UNIQUE INDEX index_item_id ON items (item_id);
Fungsi view: Selanjutnya tinggal panggil EMP_V untuk fungsi yang sama
Create or Replace EMP_V as
Select ……
SQL Create/Drop Table
SQL CREATE DATABASE Statement
SQL CREATE DATABASE statement to create new database. Oracle SQL
you have to first create new database. Before you learn how to create
new database you must have to know about SQL structure.
SQL structure is hierarchically, You first create database. Select
database for USE. Now you are in selected database are. you are
access all table as well as make new table and so many things you can
done.
Syntax
You have to create new database use following SQL CREATE DATABASE
syntax,
Truncate untuk mengkosongkan table, beda dengan delete adalah delete dapat
di rollback dan di commit, tetapi di truncate langsung hilang tidak disimpan lagi
Exercise