ORACLE
DML DDL TCL DCL
(data manipulation): (data definition lang.): (transaction control lang.): (data control lang.):
Auto commit-off Auto commit on
Insert Create Commit Grant
Update Alter Rollback revoke
Delete Drop Save point
merge truncate
RETRIVAL: select
SQL is not case sensitive but it is case sensitive within the table
Describe : is used to see structure of a table
• SELECT - extracts data from a database
• UPDATE - updates data in a database
• DELETE - deletes data from a database
• INSERT INTO - inserts new data into a database
Distinct : Used to remove duplicate values in column.
Ex: select distinct deptno from emp .it displays distinct or unique department numbers
Where clause: used for filtering records.
Ex: select * from emp
Where >,<,=,not NULL,and, or…etc
AND: both left side and right side should be true to return a value.
OR: returns a value if either of conditions is ture.
LIKE: This allows you to perform pattern matching.
% allows you to match any string of any length (including zero length)
_ allows you to match on a single character
IN: The IN function helps reduce the need to use multiple OR conditions.
SELECT columns
FROM tables
WHERE column1 in (value1, value2, .... value_n);
Orderby:used to arrange records in ascending or descending order, by default sql arranges in ascending order.
Group by: The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns.
WHERE clause constraints are evaluated on the data before it is grouped.
HAVING clause constraints are evaluated on the results of the data after it has been grouped together. You can specify multiple columns in a GROUP BY clause
SELECT STAMETENT and group statement should have same number of columns.
Sql functions: performs calculations on data, convert data to different forms. All sql funitons returns a value sometime they accept input parameters. Sql funtions are of 2 types
http://www.w3schools.com/sql/sql_functions.asp
SQL Aggregate Functions
SQL aggregate functions return a single value, calculated from values in a column.
Useful aggregate functions:
• AVG() - Returns the average value
• COUNT() - Returns the number of rows
• FIRST() - Returns the first value
• LAST() - Returns the last value
• MAX() - Returns the largest value
• MIN() - Returns the smallest value
• SUM() - Returns the sum
SQL Scalar functions
SQL scalar functions return a single value, based on the input value.
Useful scalar functions:
• upper() - Converts a field to upper case
• lower() - Converts a field to lower case
length() - Returns the length of a text field
• ROUND() - Rounds a numeric field to the number of decimals specified or whole no.
Dual: is a dummy functions used with one row and one column.
Convertion functions :
1. Implicit convertion: automatically converts one type to another
A=number b=varchar
b=a , implicit convertion automatically converts number datatype to varchar.
And varchar to date
2. Explicit conversion:
To_char from(date or number)-> char
To_number from(varchar)->number
To_date
To_lob
Count: it counts number of rows in column and will not count null values.
DECODE: works like if,else if ,else condition
Ex: select job, decode(job, ’analyst’,10,’manager’,20,40) from emp
It assigns 10 to analyst
20 to manager
40 t o others.
CASE: this is similar to decode but its advanced version over it. Decode is a deprecated method and not preferred to used in latest versions.
Case when job=’analyst’ then 10
When job=’manager’ then 20
Else 40
End case
From employe.
select distinct job,
case JOB when 'ANALYST' then 10
when 'MANAGER' then 20
else 40
END CASE FROM EMP
TRANSLATE AND REPLACE:
Translate:does by character by character
or example:
translate('1tech23', '123','456'); would return '4tech56'
translate('222tech', '2ec', '3it'); would return '333tith'
Replace: replaces entire string
Joins: used to get data form multiple tables
Different types of joins are:
1.Cartesian join
Equi joint+ non equi joint
2.inner join
a.equi join: Return rows when there is at least one match in both tables
3.outer join: matched rows plus ummatched rows from either of table. (+) used for outer join
a.left outer joint: Return all rows from the left table, even if there are no matches in the right table
b. right outer joint: Return all rows from the right table, even if there are no matches in the left table
c.full outer oint: Return rows when there is a match in one of the tables
4.self joint
SETS: union, union all, intersection ,minus
Set operator should satisy
1. Number of columns in each select statement should be same
2. Corresponding data type should be same
UNION ALL
Combines the results of two SELECT statements into one result set.
UNION
Combines the results of two SELECT statements into one result set, and then eliminates any duplicate rows from that result set.
MINUS
Takes the result set of one SELECT statement, and removes those rows that are also returned by a second SELECT statement.
INTERSECT
Returns only those rows that are returned by each of two SELECT statements.
.
• We cannot use orderby in header select statement
• Always header select statement has priority
Sub query: a sub query is a select statement that is embedded in one of the following classes of a select statement. Sub query may be single row or multi row..
Where,having,from….
WHERE clause
Most often, the subquery will be found in the WHERE clause. These subqueries are also called nested subqueries.
FROM clause
A subquery can also be found in the FROM clause. These are called inline views.
For example:
select suppliers.name, subquery1.total_amt
from suppliers,
(select supplier_id, Sum(orders.amount) as total_amt
from orders
group by supplier_id) subquery1,
where subquery1.supplier_id = suppliers.supplier_id;
In this example, we've created a subquery in the FROM clause as follows:
(select supplier_id, Sum(orders.amount) as total_amt
from orders
group by supplier_id) subquery1
This subquery has been aliased with the name subquery1. This will be the name used to reference this subquery or any of its fields.
SELECT clause
A subquery can also be found in the SELECT clause
The trick to placing a subquery in the select clause is that the subquery must return a single value. This is why an aggregate function such as SUM, COUNT, MIN, or MAX is commonly used in the subquery.
Pseudo-column
A pseudo-column is an Oracle assigned value (pseudo-field) used in the same context as an Oracle Database column, but not stored on disk
1. SYSDATE
2. SYSTIMESTAMP
3. ROWID
4. ROWNUM
5. USER
6. UID
7. LEVEL
8. CURRVAL
9. NEXTVAL
1. SYSDATE
It shows the Current date from the local or remore database . We can use the CURRENT_DATE also with for the same purpose.
Code : SELECT SYSDATE FROM DUAL
2. SYSTIMESTAMP
Systimestamp function returns the current system date and time (including fractional seconds and time zone) on your database.
Code :SELECT SYSTIMESTAMP FROM DUAL
3. ROWID
Rowid is a pseudo column that uniquely identifies a row within a table, but not within a database. It is possible for two rows of two different tables stored in the same cluster to have the same rowid
Code :SELECT ROWID FROM EMP
4. ROWNUM
Rownum numbers the records in a result set. The first record that meets the where criteria in a select statement is given rownum=1, and every subsequent record meeting that same criteria increases rownum.
Code :SELECT ROWNUM FROM EMP WHERE ROWNUM <=10
5. USER
User is a pseudo column that returns the name of the user currently connected to the session.
Code :SELECT USER FROM DUAL
6. UID
Uid is a pseudo column that returns the id number of a user currently connected to the session.
Code :SELECT UID FROM DUAL
7. LEVEL
LEVEL pseudo-column is an indication of how deep in the tree one is. It is used in hierarchical queries along with CONNECT by clause.
Code :SELECT level, empno, ename, mgrFROM emp
CONNECT BY PRIOR empno = mgr
START WITH mgr IS NULL
8. NEXTVAL
NEXTVAL is used to invoke a sequence. If nextval is invoked on a sequence, it makes sure that a unique number is generated.
Code :SELECT .NEXTVAL FROM DUAL
9. CURRVAL
CURRVAL can only be used if a session has already called nextval on a trigger. currval will then return the same number that was generated with nextval.
Code :SELECT .CURRVAL FROM DUAL
DELETE STATEMENT: The DELETE statement allows you to delete a single record or multiple records from a table.
The syntax for the DELETE statement is:
DELETE FROM table
WHERE predicates;
TRUNCATE STATEMENT: The SQL TRUNCATE command is used to delete all the rows from the table and free the space containing the table.
Syntax to TRUNCATE a table:
TRUNCATE TABLE table_name;
TCL :
Commit
If the transaction is commited, the database ensures that the modified data can always be found, even if the database or the hard disk crashes. Of course, for the latter to be true, a decent backup and recovery concept must be in place.
Rollback
If a transaction is rolled back, the database system ensures that no trace of the modified data will be found, that is, the database behaves as though the transaction had never been started.
Oracle uses undo to execute a rollback.
DELETE (DML) delete a single record or multiple records from a table.
TRUNCATE(DDL) all the rows from the table
DROP(DDL) Drops table
INSERT Statement
The INSERT statement allows you to insert a single record or multiple records into a table.
INSERT INTO VALUE
INSERT INTO table (column-1, column-2, ... column-n)
VALUES(value-1, value-2, ... value-n);
INSERT INTO SELECT
INSERT INTO "table1" ("column1", "column2", ...)
SELECT "column3", "column4", ...
FROM "table2"
UPDATE statement
The UPDATE statement allows you to update a single record or multiple records in a table.
The syntax for the UPDATE statement is:
UPDATE tableNAME
SET column = expression
WHERE predicates;
CREATE TABLE Statement
The CREATE TABLE statement allows you to create and define a table.
The basic syntax for a CREATE TABLE statement is:
CREATE TABLE table_name
( column1 datatype null/not null,
column2 datatype null/not null,
...
);
Creating table from another table with data:
Create table as