0% found this document useful (0 votes)
9 views25 pages

Lecture 30

Uploaded by

Rashda khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views25 pages

Lecture 30

Uploaded by

Rashda khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

Database

Management
System

Lecture - 30
Order By Example
Q: Display the students’ data in the
ascending order of names
SELECT * from STUDENT
ORDER BY stName
Practice Query
Display the name and cgpa of
students for all those students
who are in second or above
semester in descending order of
names
Functions in SQL
Built-in functions are pre-written
programs to perform specific
tasks
Accept certain arguments and
return the result
Categories of Functions
Depending on the arguments and the
return value, categorized
– Mathematical (ABS, ROUND, SIN, SQRT)
– String (LOWER, UPPER, SUBSTRING, LEN)
– Date (DATEDIFF, DATEPART, GETDATE())
– System (USER, DATALENGTH, HOST_NAME)
– Conversion (CAST, CONVERT)
Using Functions

SELECT upper(stName), lower(stFName),


stAdres, len(convert(char, stAdres)),
FROM student
Aggregate Functions
Operate on a set of rows and return a
single value, like, AVG, SUM,
MAX, MIN, STDEV
Attribute list cannot contain other
attributes if an aggregate function is
being used
Aggregate Function Example
SELECT avg(cgpa) as 'Average
CGPA', max(cgpa) as 'Maximum
CGPA' from student
Output is……
Aggregate Function Example
SELECT avg(cgpa) as 'Average
CGPA', max(cgpa) as 'Maximum
CGPA' from student
SELECT convert( decimal(5,2),
avg(cgpa)) as 'Average CGPA',
max(cgpa) as 'Maximum CGPA' from
student
Group By Clause
SELECT stName, avg(cgpa) as 'Average
CGPA', max(cgpa) as 'Maximum CGPA'
from student
 SELECT prName, max(cgpa) as ‘Max
CGPA', min(cgpa) as ‘Min CGPA'
FROM student GROUP BY prName
HAVING Clause
We can restrict groups by using
having clause; groups satisfying
having condition will be selected
HAVING Clause
SELECT select_list
[ INTO new_table ]
FROM table_source
[ WHERE search_condition ]
[ GROUP BY group_by_expression ]
[ HAVING search_condition ]
[ ORDER BY order_expression [ ASC |
DESC ] ]
HAVING Example
SELECT prName, min(cgpa), max(cgpa)
FROM student
GROUP BY prName
HAVING max(cgpa) > 3
Where and having can be combined
Accessing Multiple Tables
Cartesian Product
Inner join
Outer Join
Full outer join
Semi Join
Natural Join
Cartesian Product
No specific command; Select is
used
Simply give the names of the
tables involved; Cartesian product
will be produced
Cartesian Product
Produces m x n rows

Select * from program, course


Cartesian Product
Certain columns can be selected, same
column name needs to be qualified
Similarly can be applied to more than one
tables, and even can be applied on the
same table
SELECT * from Student, class, program
Database
Management
System

Lecture - 30

You might also like