Unit 4 Notes - Bcom
Unit 4 Notes - Bcom
Numeric Functions
Input Value
Function
Argument Returned
Absolute
value of m,
The
absolute
ABS
m = value value of a
(m)
number is
the number
without its
sign.
Remainder of
MOD m = value, n =
m divided by
( m, n ) divisor
n
POWER m = value, n = m raised to
( m, n ) exponent the nth power
m = value, n =
ROUND m rounded to
number of
( m [, the nth
decimal places,
n]) decimal place
default 0
m = value, n =
TRUNC m truncated
number of
( m [, to the nth
decimal places,
n]) decimal place
default 0
n = angle
SIN ( n ) expressed in sine (n)
radians
n = angle
COS ( n ) expressed in cosine (n)
radians
n = angle
TAN
expressed in tan (n)
(n)
radians
SQRT ( n n = value positive
) square root of
n
Here are some examples of the use of some of
these numeric functions:
1. select round (83.28749, 2) from marks;
2. select sqrt (3.67) from marks;
3. select power (2.512, 5) from marks;
String Functions
Input
Function Value Returned
Argument
First letter of each
s= word is changed
INITCAP
character to uppercase and
(s)
string all other letters
are in lower case.
s= All letters are
LOWER (
character changed to
s)
string lowercase.
UPPER s= All letters are
(s) character changed to
string uppercase.
s1 and s2 Concatenation of
CONCAT are s1 and s2.
( s1, s2 ) character Equivalent to s1 ||
strings s2
s= Returns the
LENGTH
character number of
(s)
string characters in s.
Here are some examples of the use of String
functions:
1. select concat ('Alan', 'Turing') as
"NAME" from customers;
2. select initcap (‘now is the time for all
good men’) from customers;
Group Functions
Input
Function Value Returned
Argument
AVG
col =
( [ DISTINC The average value
column
T | ALL ] of that column
name
col )
Number of rows
COUNT returned including
none
(*) duplicates and
NULLs
COUNT Number of rows
col =
( [ DISTINC where the value of
column
T | ALL ] the column is not
name
col ) NULL
MAX
col =
( [ DISTINC Maximum value in
column
T | ALL ] the column
name
col )
MIN col = Minimum value in
( [ DISTINC column
T | ALL ]
name the column
col )
SUM
col =
( [ DISTINC Sum of the values
column
T | ALL ] in the column
name
col )
ID
4
5
7
SELECT * FROM CUSTOMERS WHERE ID
IN…….
Output:
ID NAME AGE ADDRESS SALARY
4 Chaitali 25 Mumbai 6500.00
5 Hardik 27 Bhopal 8500.00
7 Muffy 24 Indore 10000.00
Subqueries with the INSERT Statement
Subqueries also can be used with INSERT
statements. The INSERT statement uses the
data returned from the subquery to insert into
another table. The selected data in the
subquery can be modified with any of the
character, date or number functions.
Syntax
INSERT INTO table_name [ (column1 [, column2
]) ] SELECT [ *|column1 [, column2 ] FROM
table1 [, table2 ] [ WHERE VALUE OPERATOR
]
Example
Consider a table CUSTOMERS_BKP with
similar structure as CUSTOMERS table. Now
to copy the complete CUSTOMERS table
into the CUSTOMERS_BKP table, we can
use the following syntax.
SQL> INSERT INTO CUSTOMERS_BKP
SELECT * FROM CUSTOMERS WHERE ID IN
(SELECT ID FROM CUSTOMERS);
Subqueries with the UPDATE
Statement
The subquery 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.
Syntax
UPDATE table SET column_name = new_value [
WHERE OPERATOR [ VALUE ] (SELECT
COLUMN_NAME FROM TABLE_NAME)
[ WHERE) ];
Example
Assuming, we have CUSTOMERS_BKP
table available which is backup of
CUSTOMERS table. The following example
updates SALARY by 0.25 times in the
CUSTOMERS table for all the customers
whose AGE is greater than or equal to 27.
SQL> UPDATE CUSTOMERS SET
SALARY = SALARY * 0.25 WHERE AGE
IN (SELECT AGE FROM
CUSTOMERS_BKP WHERE AGE >= 27);
This would impact two rows and finally
CUSTOMERS table would have the
following records.
ID NAME AGE ADDRESS SALARY
1 Ramesh 35 Ahmedabad 125.00
2 Khilan 25 Delhi 1500.00
3 Kaushik 23 Kota 2000.00
4 Chaitali 25 Mumbai 6500.00
5 Hardik 27 Bhopal 2125.00
6 Komal 22 MP 4500.00
7 Muffy 24 Indore 10000.00
******
SQL JOIN
A JOIN clause is used to combine rows from two
or more tables, based on a related column between
them.
Basic SQL JOIN types
SQL Server supports many kinds of different
joins including (4 MAIN TYPES) INNER
JOIN, SELF JOIN, CROSS JOIN,
and OUTER JOIN. In fact, each join type
defines the way two tables are related in a
query. OUTER JOINS can further be divided
into (3 SUBPOINTS) LEFT OUTER
JOINS, RIGHT OUTER JOINS,
and FULL OUTER JOINS.
SQL INNER JOIN creates a result table
by combining rows that have matching values
in two or more tables.
SQL SELF JOIN joins the table to itself
and allows comparing rows within the same
table.
SQL CROSS JOIN creates a result table
containing paired combination of each row of
the first table with each row of the second
table.
SQL LEFT OUTER JOIN includes in a
result table unmatched rows from the table
that is specified before the LEFT OUTER
JOIN clause.
SQL RIGHT OUTER JOIN creates a
result table and includes into it all the records
from the right table and only matching rows
from the left table.
FULL (OUTER) JOIN returns all
records when there is a match in either left or
right table.
Let's look at a selection from the "Orders"
table:
OrderID CustomerID OrderPlaced
10308 2 1996-09-18
10309 37 1996-09-19
10310 77 1996-09-20
Then, look at a selection from the "Customers"
table:
CustomerI CustomerNa ContactNam Countr
D me e y
1 Alfreds Maria German
Futterkiste Anders y
2 Ana Trujillo Ana Trujillo Mexico
Emparedados y
helados
3 Antonio Antonio Mexico
Moreno Moreno
Taquería
The "CustomerID" column in the "Orders" table
refers to the "CustomerID" in the "Customers"
table. The relationship between the two tables
above is the "CustomerID" column.
Then, we can create the following SQL statement
(that contains an INNER JOIN), that selects
records that have matching values in both tables:
Example
SELECT Orders.OrderID,
Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID
=Customers.CustomerID;
Output:
OrderI CustomerNam OrderDat
D e e
10308 Ana Trujillo 9/18/1996
Emparedadosy
helados
10365 Antonio Moreno 11/27/1996
Taquería
10383 Around the 12/16/1996
Horn
10355 Around the 11/15/1996
Horn
10278 Berglunds 8/12/1996
snabbköp
1. INNER JOIN
INNER JOIN statement returns only those
records or rows that have matching values
and is used to retrieve data that appears in
both tables.
Syntax
SELECT column_name(s) FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
Example
SELECT Orders.OrderID,
Customers.CustomerName FROM Orders
INNER JOIN Customers ON Orders.CustomerID
= Customers.CustomerID;
2. FULL OUTER JOIN
The FULL OUTER JOIN returns a result
that includes rows from both left and right
tables. In case, no matching rows exist for the
row in the left table, the columns of the right
table will have nulls. Correspondingly, the
column of the left table will have nulls if
there are no matching rows for the row in the
right table.
Syntax:
SELECT column_name(s) FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;
Example
SELECT Customers.CustomerName,
Orders.OrderID FROM Customers
FULL OUTER JOIN Orders ON Customers.Cust
omerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
Syntax
SELECT column_name(s) FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
Example
SELECT Customers.CustomerName,
Orders.OrderID FROM Customers
LEFT JOIN Orders ON Customers.CustomerID =
Orders.CustomerID
ORDER BY Customers.CustomerName;
4. RIGHT OUTER JOIN
The RIGHT OUTER JOIN works by the
same principle as the LEFT OUTER JOIN.
The RIGHT OUTER JOIN selects data
from the right table (Table B) and matches
this data with the rows from the left table
(Table A). The RIGHT JOIN returns a result
set that includes all rows in the right table,
whether or not they have matching rows from
the left table. In case, a row in the right table
does not have any matching rows in the left
table, the column of the left table in the result
set will have nulls.
Syntax:
SELECT column_name(s) FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
Example:
SELECT Orders.OrderID, Employees.LastName,
Employees.FirstName FROM Orders
RIGHT JOIN Employees ON Orders.EmployeeID
= Employees.EmployeeID
ORDER BY Orders.OrderID;
5. SELF JOIN
The SELF JOIN allows you to join a table to
itself. This implies that each row of the table
is combined with itself and with every other
row of the table. The SELF JOIN can be
viewed as a join of two copies of the same
table. The table is not actually copied, but
SQL performs the command as though it
were.
Syntax:
SELECT column_name(s) FROM table1 T1,
table1 T2 WHERE condition;
Example:
SELECT A.CustomerName AS CustomerNam
e1,
B.CustomerName AS CustomerName2, A.C
ity
FROM Customers A, Customers B
WHERE A.CustomerID = B.CustomerID
AND A.City = B.City
ORDER BY A.City;
6. CROSS JOIN
The CROSS JOIN command in SQL, also
known as a cartesian join, returns all
combinations of rows from each table.
Envision that you need to find all
combinations of size and color. In that case, a
CROSS JOIN will be an asset. Note, that this
join does not need any condition to join two
tables. In fact, CROSS JOIN joins every row
from the first table with every row from the
second table and its result comprises all
combinations of records in two tables.
Syntax:
SELECT column_name(s) FROM table1
CROSS JOIN table2;
Example:
SELECT Customers.CustomerName,
Orders.OrderID FROM Customers
CROSS JOIN Orders;
******
Block Structure
PL/SQL blocks have a pre-defined structure in which the code is to be
grouped. Below are different sections of PL/SQL blocks.
1. Declaration section
2. Execution section
3. Exception-Handling section
The below picture illustrates the different PL/SQL block and their
section order.
Declaration Section
This is the first section of the PL/SQL blocks. This section is an optional
part. This is the section in which the declaration of variables, cursors,
exceptions, subprograms, pragma instructions and collections that are
needed in the block will be declared. Below are few more
characteristics of this part.
Execution Section
Execution part is the main and mandatory part which actually executes
the code that is written inside it. Since the PL/SQL expects the
executable statements from this block this cannot be an empty block,
i.e., it should have at least one valid executable code line in it. Below
are few more characteristics of this part.
Exception-Handling Section
The exception is unavoidable in the program which occurs at run-time
and to handle this Oracle has provided an Exception-handling section
in blocks. This section can also contain PL/SQL statements. This is an
optional section of the PL/SQL blocks.
This is the section where the exception raised in the execution
block is handled.
This section is the last part of the PL/SQL block.
Control from this section can never return to the execution block.
This section starts with the keyword ‘EXCEPTION’.
This section should always be followed by the keyword ‘END’.
DECLARE --optional
<declarations>
BEGIN --compulsory
<executable statements. At least
one executable statement is compulsory>
EXCEPTION --optional
<exception handles>
END; -- compulsory
/
Note: A block should always be followed by ‘/’ which sends the
information to the compiler about the end of the block.
DDL Triggers
The Data uses manipulation Language (DML) command events that begin
with Insert, Update, and Delete set off the DML triggers. corresponding to
insert_table, update_view, and delete_table.
SQL Server
create trigger deep
on emp
for
insert,update ,delete
as
print 'you can not insert,update and delete this table i'
rollback;
Output:
Logon Triggers
What is a Procedure?
A procedure is a set of instructions which takes input and performs a
certain task. In SQL, procedures do not return a value. In Java, procedures
and functions are same and also called subroutines.
In SQL, a procedure is basically a precompiled statement which is stored
inside the database. Therefore, a procedure is sometimes also called
a stored procedure. A procedure always has a name, list of parameters,
and compiled SQL statements. In SQL, a procedure does not return any
value.
Conclusion
The most significant difference that you should note here is that a function
is used to calculate the result using the given inputs, while a procedure is
used to perform a certain task in order.