Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 44

Sorting

Sorting helps to sort the records that are


retrieved. By default, it displays the
records in ascending order of primary key.
If we need to sort it based on different
columns, then we need to specify it in
ORDER BY clause

Nahida Nazir
output

Nahida Nazir
Apply Desc

Nahida Nazir
Row id
 ROWID:For each row in the database, the ROWID
pseudocolumn returns a row\’s address. The ROWID
contains 3 information about row
address:FileNo : FileNo means Table Number.
 DataBlockNo : DataBlockNo means the space assigned
by the oracle sql engine to save the record.
 RecordNo : Oracle engine mantains the record number
for each record.

Nahida Nazir
Syntax
SELECT ROWID address FROM neha4

WHERE id = 5;

Nahida Nazir
Aggregate Functions
 An aggregate function is a function where
the values of multiple rows are grouped
together as input on certain criteria to
form a single value of more significant
meaning.

Nahida Nazir
Aggregate functions
1) Count()
2) Sum()
3) Avg()
4) Min()
5) Max()

Nahida Nazir
Count function
The SQL COUNT function is an
aggregate function that returns the number
of rows returned by a query

Nahida Nazir
Count(*)
Select COUNT(*) from epm2 where da >
2000;

Nahida Nazir
For retrieving a single row
SELECT
COUNT(*)
FROM
laptop33
WHERE
id = 47

Nahida Nazir
Sum function
The SQL AGGREGATE SUM() function
returns the SUM of all selected column.
 syntax SELECT SUM(attribute name)
FROM tablename

Nahida Nazir
SUM() using multiple columns example

syntax SELECT SUM (attribute1 +


attribute2) FROM table;

Nahida Nazir
Average function
 The AVG() function returns the average
value of a numeric column.
 SELECT
AVG(id) FROM laptop33;

Nahida Nazir
Min aggregate function
 The MIN() function returns the smallest
value of the selected column.
 SELECT MIN(id)
FROM laptop33

Nahida Nazir
Max function
The MAX() function returns the largest
value of the selected column.
 SELECT Max(id)
FROM laptop33

Nahida Nazir
Group by
Group by clause is used to group the
results of a SELECT query based on one
or more columns.

Nahida Nazir
syntax
select department, sum(salary) from
professor1 group by department

Nahida Nazir
Syntax
create table salary5(name varchar(22),age int,
salary int , emp_id int);
insert into salary5 values('neha', 44, 33000, 54)
insert into salary5 values('raj',34, 70000,43)
insert into salary5 values('poosh', 39,65000,23)
insert into salary5 values ('riya', 39, 23000, 33)
select name, age, Count(*)
from Salary5
group by name, age
Nahida Nazir
Group by

Nahida Nazir
Having clause
The HAVING clause was added to SQL
because the WHERE keyword could not
be used with aggregate functions.

Nahida Nazir
Syntax for having clause
Select column_name, aggregate_functions
(column name)
From table_name
Where condition
Group by column name
Having condition order by column name

Nahida Nazir
coding
 create table professor(name varchar(22), department
varchar(22), salary float, id int);
 insert into professor values('raj', 'cse', 45000, 23)
 insert into professor values('rahul', 'cse', 34000,22)
 insert into professor values('rajat', 'english', 24000,33)
 insert into professor values('vikas', 'english', 94000,55)
 insert into professor values('amit', 'cse', 34000,29)
 insert into professor values('furqan', 'maths', 34000,92)
 insert into professor values('rahul', 'chemistry', 34000,22)
 insert into professor values('aditya', 'chemisty', 84000,62)

Nahida Nazir
output
select department, sum(salary) from
professor1 group by department having
department!='english';

Nahida Nazir
Joins
 A JOIN clause is used to combine rows
from two or more tables, based on a
related column between them.
 INNER JOIN
LEFT JOIN
RIGHT JOIN
FULL JOIN

Nahida Nazir
Inner join
The INNER JOIN command returns rows
that have matching values in both tables
The INNER JOIN keyword selects all
rows from both tables as long as there is a
match between the columns.

Nahida Nazir
Inner equi join
Syntax
Select column_name from table 1
Inner join table2
on column_name=column_name

Nahida Nazir
coding
create table z11( stuid int primary key, name varchar(20), city varchar(21));
insert into z11 values( 12, 'rahul', 'punjab')
insert into z11 values( 22, 'raj', 'phagwara')
insert into z11 values( 82, 'piya', 'jammu')
create table z22(depid int primary key, depname varchar(33), salary float,
stuid int,foreign key(stuid) references z11(stuid));
insert into z22 values( 45,'physics', 6000,12)
insert into z22 values( 85,'chemistry', 8000,22)

insert into z22 values( 95,'biology', 9000,82)

select z11.name, z22.depname from z11


inner join z22
on z11.stuid=z22.stuid;

Nahida Nazir
Equi join output

Nahida Nazir
Natural join
The SQL NATURAL JOIN is a type of
EQUI JOIN and is structured in such a
way that, columns with the same name of
associated tables will appear once only

Nahida Nazir
Syntax
SELECT * FROM
table1
NATURAL JOIN
table2

Nahida Nazir
Output

Nahida Nazir
Non equi join
 Non-equi joins are joins whose join
conditions use conditional operators other
than equals.
 syntax
 column_name from tabel1
 inner join table2
 on column_name<>column_name

Nahida Nazir
output

Nahida Nazir
Outer Join
The FULL OUTER JOIN returns a result
set that includes rows from both left and
right tables. When no matching rows exist
for the row in the left table, the columns
of the right table will have null and vice
versa.

Nahida Nazir
Syntax left outer join
select z111.name, z22.depname from z111
left join z22
on z111.stuid=z22.stuid

Nahida Nazir
Right outer join
Right join keyword returns all rows from
the right table , with the matching rows in
the left table.

Nahida Nazir
Cross Join
 The SQL CROSS JOIN produces a result
set which is the number of rows in the
first table multiplied by the number of
rows in the second table.
 It is also known as Cartesian join .

Nahida Nazir
Syntax
 select* from table1 cross join table2
 select attribute1,attribute2 from table1
cross join table2

Nahida Nazir
Cross join for selected columns

Nahida Nazir
Cross join of full table

Nahida Nazir
Self Join
 A self join is a join in which a table is
joined with itself.
 It is also called Unary relationships.

Nahida Nazir
Views
 Views in SQL are kind of virtual tables.
 A view also has rows and columns as
they are in a real table in the database.

Nahida Nazir
Types of View
Simple View
 Complex View

Nahida Nazir
Simple View

Nahida Nazir

You might also like