0% found this document useful (0 votes)
20 views13 pages

SQL

The document discusses SQL and various SQL statements like DDL, DML, DRL, DCL and TCL. It also discusses differences between DELETE, TRUNCATE and DROP statements, types of constraints, data types, aggregate functions, date functions, numeric functions and CASE statements.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
20 views13 pages

SQL

The document discusses SQL and various SQL statements like DDL, DML, DRL, DCL and TCL. It also discusses differences between DELETE, TRUNCATE and DROP statements, types of constraints, data types, aggregate functions, date functions, numeric functions and CASE statements.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 13

SQL

 Structured Query Language


 Language with which we query the database

SQL Statements

 Request to Oracle database is given in the form of SQL statements


 There are 5 types of SQL statements
1. Data Definition Language (DDL) – CREATE, ALTER, DROP, TRUNCATE, RENAME
Manipulate structure of the table
2. Data Manipulation Language (DML) – INSERT, UPDATE, DELETE
Manipulate actual data
3. Data Retrieval Language (DRL) - SELECT
4. Data Control Language (DCL) – GRANT, REVOKE etc
Grant or revoke permissions on objects created by User to someone
5. Transaction Control Language (TCL) – COMMIT, ROLLBACK, SAVEPOINT
Controls the transactions

Difference between DELETE, TRUNCATE and DROP:

1. Delete is DML and Truncate and Drop is DDL


2. From Delete data can be recovered using rollback command and from truncate data cannot be
recovered
3. Delete does not release the memory occupied by records of the table where Truncate does
release the memory
4. Delete table is a logged operation. So the deletion of each row gets logged in the transaction
log, which makes it slow. Truncate table also deletes all the rows in a table, but it won't log the
deletion of each row instead it logs the deallocation of the data pages of the table, which makes
it faster
5. Delete – delete some or all rows based on where condition. Truncate – Delete all rows. Drop –
removes all rows including table definition, indexes, triggers, grants and storage parameters
6. Triggers fired on delete but not on truncate and drop
7. Efficiency is more on Truncate than Delete and Drop. In Drop dropping and recreating the table
requires to re-grant object privileges, recreate indexes, constraints etc

Types of Constraints:

1. Primary Key Constraints - Unique and Not Null – column and table level declaration
acc_no Number(5) CONSTRAINT acc_no_pk PRIMARY KEY

2. Foreign Key Constraints – referential integrity constraint, it allows NULL values in child table
We cannot delete parent table column data which is referenced as foreign key in child table
Primary key and foreign key can exist in same table also
project_id NUMBER(5) CONSTRAINT emp_projid_fk REFERENCES PROJECT(project_id)

3. Unique Constraints – makes column data unique but NULL can be inserted - column level
declaration
acc_no Number(5) CONSTRAINT acc_no_unique UNIQUE

4. Check Constraints – gives restriction to what value can go into particular column – column level
declaration
EmpSalary Number(10,2) CONSTRAINT emp_sal_chk CHECK (EmpSalary > 0 AND EmpSalary <
10000)

5. Not Null Constraints – doesn’t allow NULL values - column level declaration and cannot be done
at table level
acc_no Number(5) CONSTRAINT acc_no_notnull NOT NULL

6. Composite Primary Key – two or more columns are considered as Primary Key – table level
declaration
CONSTRAINT cust_idacc_no_pk PRIMARY KEY (CUST_ID, ACC_NO)

We can use ALTER command to ADD/MODIFY constraints on table

ALTER TABLE tablename MODIFY (colum1 NUMBER(5) NOT NULL, acc_no NUMBER(10) PRIMARY KEY);

ALTER TABLE tablename ADD CONSTRAINT pk1 PRIMARY KEY (cust_id, acc_no);

ALTER TABLE tablename ADD CONSTRAINT ck1 CHECK (Acc_type IN (‘Savings’, ‘Current’, ‘pf’));

DataTypes:

1. NUMBER
 NUMBER(p,s)
 p – precision - total number of digits before and after decimal point
 s – scale - total number of digits after decimal point
 to store 12345.67 – NUMBER(7, 2)

2. INTEGER
 To store whole numbers positive or negative

3. CHAR - Character
 stores fixed-length character strings
 CHAR(x) can hold characters up to x bytes
 Even when less than x bytes is used, declaration would still occupy a fixed size of x bytes
 Can be used when size of column is fixed, like Gender (M or F) – CHAR(1)

4. VARCHAR2
 No difference between VARCHAR and VARCHAR2. VARCHAR is older version. If we
declare something in VARCHAR it will be automatically taken as VARCHAR2
 Varrying character or VARCHAR is a variable length character datatype
 VARCHAR2(x) can accommodate maximum of x characters or x bytes of data
 However, for character data less than x bytes, only that much space will be used

5. DATE
 For input and output of dates, standard oracle date format is ‘DD-MON-YY’
 It won’t take any size with its declaration
 It occupies a fixed length, 7 bytes of memory and stores information about century,
year, month, day, hour, minute and second

Aggregate Functions:

 Count(*) – returns no of rows, regardless of NULLs


 Count(columnName) –returns no of rows that do not have NULL values
 Group By is associated with Aggregate Functions
 Having condition has to be based on some column that appears in the select else should be
included in where clause

Date Functions:

 Date functions can be used only for DATE datatypes


 Add or subtract no of days to a date
selectdate1 +/– num
 Add or subtract no of months to a date
Select ADD_MONTHS(date1, +/-num)
 Difference between 2 dates in terms of no of days
Select (date1 – date2) from….
 Difference between 2 dates in terms of no of months
Select MONTHS_BETWEEN(date1, date2) from….
 Default Date format in Oracle is ‘DD-MON-YY’, we can use to_char(date1, format) to change
default date format. Ex: TO_CHAR(orderdate, ‘YYYYMMDD’)
 To select system date – select sysdate from dual – dummy table in case there is no physical table
or view to refer from
Numeric Functions:

 ROUND – rounds up the number to the nearest integer


ROUND(4242.7) = 4243, ROUND(4242.4) = 4242
 CEIL – gives the next highest integer
CEIL(4242.7) = 4243
 FLOOR – gives the next lowest integer
FLOOR(4242.7) = 4242
 ABS – returns the positive value of number passed
ABS(-4242.7) = 4242.7

CASE Statements:

 Can be used to fetch or modify the records based on some conditions


 It has the functionality of an IF-THEN-ELSE statement
 We can use it in SELECT, UPDATE, ORDER BY and HAVING clause

Simple CASE expression

Compares the expression in CASE statement against values given in WHEN to determine the result

CASE expression
WHEN value1 THEN Result1
WHEN value2 THEN Result2
ELSE Result3
END

Scenario 1: Senior management of a company want to give salary increase to its employees depending
on their designation

SELECT statement with CASE expression

Select empid, designation, salary,

CASE designation

WHEN ‘Administrator’ THEN salary*1.1

WHEN ‘MANAGER’ THEN salary*1.05

WHEN ‘CLERK’ THEN salary*1.25

ELSE salary * 1.02


END Newsal From employee;

UPDATE statement with CASE expression

Update employee set salary =

CASE designation

WHEN ‘Administrator’ THEN salary*1.1

WHEN ‘MANAGER’ THEN salary*1.05

WHEN ‘CLERK’ THEN salary*1.25

ELSE salary * 1.02

END;

Scenario 2: Need to create a report showing salary band of employees. The salary band depends on the
current salary level of the employee

Searched CASE expression

Different conditions in WHEN expression is checked in a sequential manner till one is met. If non of them
meet, it will give the result in “ELSE” expression

CASE
WHEN Boolean Expression THEN Result1
WHEN Boolean Expression THEN Result2
ELSE Result3
END

Select empid, ename, salary,

CASE

WHEN salary between 0 and 999 THEN ‘BAND-A’

WHEN salary between 1000 and 1999 THEN ‘BAND-B’

WHEN salary between 2000 and 2999 THEN ‘BAND-C’

WHEN salary >=3000 THEN ‘BAND-High’

ELSE ‘INVALID Salary’ END “Salary Band”

From employee order by salary;


Handling NULLs - NULL Value Functions:

1. NVL function – evaluates the first argument and return the same if it is NOT NULL else it returns
second argument
NVL(expr1, expr2)
Select nvl(comm,0) from emp;
Select NVL(To_CHAR(comm), ‘Commission not available’) from emp;

2. NVL2 function - evaluates the first argument and return the second argument if it is NOT NULL
else it returns third argument
NVL2(expr1, expr2, expr3)

Select NVL2(To_CHAR(comm), ‘Commission available’, ‘Commission not available’)

Joins:

1. Left Outer Join – includes all records from the left table and matching records from right table
SELECT ……. FROM table1 LEFT OUTER JOIN table2 ON condition
2. Right Outer Join – includes all records from the right table and matching records from left table
SELECT ……. FROM table1 RIGHT OUTER JOIN table2 ON condition
3. Full Outer Join – includes all matching records from the left and right tables and then extra
records from both left and right tables
E.g. Match all customers and suppliers by country 
This returns suppliers that have no customers in their country, 
and customers that have no suppliers in their country, 
and customers and suppliers that are from the same country. 
SELECT ……. FROM table1 FULL OUTER JOIN table2 ON condition
4. Inner Join – includes only matching records from left and right tables
SELECT ……. FROM table1 INNER JOIN table2 ON condition

Independent SubQueries:

 A sub-query is a query within a query.


 Inner query is independent of Outer query
 Inner query is executed first and results are stored
 Outer query then runs on the stored results

Select col1 from table1 where col2 in (select col2 from table2 where col2 = ‘ABC’)

Scenario: To list the cust_id and loan_no for all customers who have taken a loan of amount greater
than loan amount of customer id = 111

customer_loan -> loan_no, bank, branch, loan_amount, custid

fd_account -> custid, fd_amount


loan_account -> custid, loan_amount

Select custid, loan_no from customer_loan where loan_amount >

(select loan_amount from customer_loan where custid = 114);

Correlated Subqueries:

 Inner query and Outer query are related to each other


 Execution of Inner query is dependent on execution of Outer query
 Inner query is executed separately for each row of the Outer query
 Here sub query will be after Having clause of Outer query

Scenario: To list all customers who have a fixed deposit amount less than the sum of all their loans

select custid, sum(fd_amount) from fd_account group by custid having sum(fd_amount) <

(select sum(loan_amount) from loan_account where loan_account.custid = fd_account.custid


group by custid);

Exists and NOT EXISTS Statements:

 Is used in combination with sub query and is considered to be met, if subquery returns at least
one row
 Can be used in DML statements – SELECT, INSERT, UPDATE, DELETE
 Is not very efficient, since sub-query is rerun for every row in the outer query’s table
Where EXISTS (subquery);
Where NOT EXISTS (subquery);

Scenario: The inventory manager wants to analyze which item have been sold (EXISTS) and not sold
(NOT EXISTS) for some time. Then wants to check if those items can be removed from ITEMS table. The
details about the Items are in ITEMS table and the sales records are stored in LINEITEMS table

ITEMS -> itemno, itemtype, desc

LINEITEMS -> lineitemno, billno, itemno, quantity

Select * from ITEMS where EXISTS

(select * from lineitems where items.itemno = lineitem.itemno);

Select * from ITEMS where NOT EXISTS

(select * from lineitems where items.itemno = lineitem.itemno);

Delete from ITEMS where NOT EXISTS


(select * from lineitems where items.itemno = lineitem.itemno);

Merge Statements:

 Can be used in DML statements – INSERT, UPDATE, DELETE


 Perform INSERT, UPDATE, DELETE operation on a table using single SQL statement
 Avoid multiple INSERT, UPDATE, DELETE DML statements
 Cannot update the same row of target table multiple times in the same MERGE statement
 Syntax
MERGE INTO targettablename
USING sourcetable_reference name or a SELECT statement
ON (Condition)
WHEN MATCHED THEN UPDATE set col1 = value1,…
WHEN NOT MATCHED THEN INSERT (col1, col2….) values (val1, val2….)
DELETE WHERE (delete_condition);

MERGE INTO emp_testing

Using emp

ON (emp_testing.empno = emp.empno)

WHEN NOT MATCHED Then insert values(emp.empno, emp.ename, emp.job, emp.mgr,


emp.hiredate, emp.sal, emp.comm,

emp.deptno, emp.first_name, emp.lname, emp.countryname)

WHEN MATCHED Then Update set deptno = emp.deptno

DELETE WHERE emp.job = 'CLERK';

Note: cannot update the same row of the target table multiple times in same MERGE statement

Multi Table Insert Statement:

 Is used to Insert into multiple tables from a select query


 Extension of INSERT INTO….SELECT
 Can be done only on tables, not on views or materialized views
 Syntax
INSERT ALL|FIRST
[WHEN CONDITION THEN] INTO target [VALUES]
[WHEN CONDITION THEN] INTO target [VALUES]

…….

[ELSE] INTO target [VALUES]


SELECT ….
From source_query;
 INSERT ALL – for every when conditions all the records will be evaluated
 INSERT FIRST – once the row met any when condition, the same row will not be evaluated
against other when conditions

Scenario:Company ABC has many departments and employees are allocated to one of them. We need
to create separate table for each of the department and insert emp record for the respective
departments into them.

INSERT ALL

WHEN deptno=10 THEN into dep10

WHEN deptno=20 THEN into dep20

WHEN deptno=30 THEN into dep30

ELSE into dep_default

Select * from emp;

INSERT ALL

WHEN sal<=1000 THEN INTO low_sal

WHEN sal<=2000 THEN INTO mid_sal

WHEN sal<=3000 THEN INTO high_sal

ELSE INTO verigh_sal

Select * from emp;

INSERT FIRST

WHEN sal<=1000 THEN INTO low_sal

WHEN sal<=2000 THEN INTO mid_sal

WHEN sal<=3000 THEN INTO high_sal

ELSE INTO verigh_sal

Select * from emp;


Intersect Queries:

 To find out matching records from Source query to Target query


 No of columns should be same in Source and Target query
 Type of Source and Target column should match one to one
 Syntax
Select query1
INTERSECT
Select query2

Minus Query:

 To find mismatch of records between source and target query


 No of columns should be same in Source and Target query
 Type of Source and Target column should match one to one
 Syntax
Select query1
MINUS
Select query2

RANK Functions:

 Can be used, if there is a requirement to rank output based on fact values


 Ranking can be done within a partition
 Two types – Normal Rank (RANK() ranks is skipped depending on how many same ranks
available) and Dense Rank (DENSE_RANK() no skipping of ranks)

RANK()OVER(
    PARTITION BY <expr1>[{,<expr2>...}]
    ORDER BY <expr1>[ASC|DESC],[{,<expr2>...}]
)

 First, the PARTITION BY clause distributes the rows in the result set into partitions by one or more
criteria.
 Second, the ORDER BY clause sorts the rows in each a partition.
 The RANK() function is operated on the rows of each partition and re-initialized when crossing
each partition boundary.

SELECT
    first_name,
    last_name,
    salary,
    RANK()OVER(ORDER BY salary)salary_rank
FROM
    employees;
In this example, we omitted the PARTITION BY clause so the whole result set was treated as a single
partition.
The ORDER BY clause sorted the rows in the result by salary. The RANK() function then is applied to each
row in the result considering the order of employees by salary in descending order.

SELECT
first_name,
lname,
deptno,
sal,
RANK()OVER(
PARTITION BY deptno
ORDER BY sal)salary_rank
FROM
emp;

SELECT
first_name,
lname,
deptno,
sal,
DENSE_RANK()OVER(
PARTITION BY deptno
ORDER BY sal)salary_rank
FROM
emp;

ROWID and ROWNUM

ROWID & ROWNUM are pseudocolumns which are not actual columns in the table but behave like
actual columns.You can select the values from pseudocolumns like ROWID & ROWNUM.ROWID &
ROWNUM are very important pseudocolumns in oracle which is used in data retrieval.

1. ROWID is nothing but the physical memory location on which that data/row is stored.ROWID
basically returns address of row.
2. ROWID uniquely identifies row in database.
3. ROWID is combination of data object number,data block in datafile,position of row and datafile in
which row resides.
4. ROWID is 16digits hexadecimal number whose datatype is also ROWID Or UROWID
5. The fastest way to access a single row is ROWID
6. ROWID is unique identifier of the ROW.

1. ROWNUM is magical column in Oracle which assigns the sequence number to the rows retrieves in
the table.
2. To limit the values in the table you can use rownum pseudocolumn
3. ROWNUM is nothing but logical sequence number given to the rows fetched from the table.
4. ROWNUM is logical number assigned temporarily to the physical location of the row.
5. You can limit the values in the table using rownum
6. ROWNUM is also unique temporary sequence number assigned to that row.

              ROWID                            ROWNUM 

1. ROWNUM is nothing but the


sequence which is allocated to that
data retrieval bunch.
2. ROWNUM is temporarily
allocated sequence to the rows.
1.ROWID is nothing but Physical memory allocation
2.ROWID is permanent to that row which identifies the address of 3.ROWNUM is numeric sequence
that row. number allocated to that row
temporarily.
3.ROWID is 16 digits Hexadecimal number which is uniquely
identifies the rows. 4.ROWNUM returns the sequence
number to that row.
4.ROWID returns PHYSICAL ADDRESS of that row.
5. ROWNUM is an dynamic value
5. ROWID is automatically generated unique id of a row and it is automatically
generated at the time of insertion of row. retrieved along with select
statement output.
6. ROWID is the fastest means of accessing data.
6.ROWNUM is not related to access
  of data.
Find nth row of a table

select ename, empno from emp where rownum =1 and rowid not in

(select rowid from emp where rownum < n);

In order to reach for nth record of emp table, the query first discards the top n-1 records and then
selects the top record from the remaining list.

To find duplicate rows

SELECT ename,empno, COUNT(*)

FROM emp GROUP BY ename, empno having count(ename) = 1;

To delete duplicate rows

delete from emp where empno in (

SELECT COUNT(empno)

FROM emp GROUP BY ename, empno having count(ename) > 1);

Display alternative rows in a table

Select * from emp where (rowid,0) in (select rowid, mod(rownum,2) from emp);

To find nth max salary

Select * from emp where sal in (

Select min(sal) from (select * from emp order by sal desc) where rownum <=5);

You might also like