Dbms Lab 3
Dbms Lab 3
Patients Table
Patient Patient Ag Postal
Sex Address State Country Hemoglobin
ID Name e Code
Flat no 201,
Vasavi
01 Sheela F 23 500023 Telangana India 82.321
Heights,
Yakutapura
Building no 2,
02 Rehan M 21 560063 Karnataka India 83.231
Yelahanka
H No 1,
03 Anay M 56 132140 Haryana India 94.567
Panipat
House no 12,
04 Mahira F 42 382421 Gujarat India 78.567
Gandhinagar
Sunflower
Maharashtr
05 Nishant M 12 Heights, 400080 India 65.234
a
Thane
Aggregate Functions
Aggregate functions are used to perform mathematical calculations on
data. They usually return a single value as output.
Example:
[code]
SELECT COUNT(PatientID) FROM Patients;
[/code
SUM()
Used to return an arithmetic addition of numeric values of a column in a
table.
Syntax:
[code]
SELECT SUM(Column_Name) FROM TABLE_NAME;
[/code]
Example:
[code]
SELECT SUM(Age)FROM Patients;
[/code]
AVG()
Used to return an average of numeric values of a column in a table.
Syntax:
[code]
SELECT AVG(Column_Name) FROM TABLE_NAME;
[/code]
Example:
[code]
SELECT AVG(Age)FROM Patients;
[/code]
MIN()
Used to return the least numeric values from a column in a table.
Syntax:
[code]
SELECT MIN(Column_Name) FROM TABLE_NAME;
[/code]
Example:
[code]
SELECT MIN(Age)FROM Patients;
[/code]
MAX()
Used to return the greatest numeric values from a column in a table.
Syntax:
[code]
SELECT MAX(Column_Name) FROM TABLE_NAME;
[/code]
Example:
[code]
SELECT MAX(Age)FROM Patients;
[/code]
FIRST()
Used to return the first value from a column in a table.
Syntax:
[code]
SELECT FIRST(Column_Name) FROM TABLE_NAME;
[/code]
Example:
[code]
SELECT FIRST(PatientName)FROM Patients;
[/code]
LAST()
Used to return the last value from a column in a table.
Syntax:
[code]
SELECT LAST(Column_Name) FROM TABLE_NAME;
[/code]
Example:
[code]
SELECT LAST(PatientName)FROM Patients;
[/code]
The above table (left table) contains the record of Employees (Employee
ID, Name, and Department), and when we apply the GROUP BY clause
on the Department column, it returns the result set that includes the
unique value in the department column (Sales, Marketing, and
Education).
Syntax
SELECT column_names
FROM table_name
WHERE conditions
GROUP BY column_names
ORDER BY column_names;
Now, we will take some examples to understand how to use the GROUP
BY clause in SQL.
Let’s have a Student table that contains the StudentID, Name,
Percentage, and corresponding Grade.
Student ID Name Percentage Grade Remark
1001 Ajay 87 A Excellent
1002 Babloo 81 A Excellent
1003 Chhavi 79 B Good
1004 Dheeraj 93 O Excellent
1005 Evina 95 O Excellent
1006 Fredy 80.7 A Excellent
1007 Garima 63 C Poor
1008 Hans 49 F Fail
1009 Ivanka 88 A Excellent
1010 Jai 74 B Good
1011 Kundan 88 A Excellent
1012 Himanshi 37 F Fail
1013 Atul 67 B Good
1014 Jaya 48 F Fail
1015 Aquib 75 B Good
Query
FROM Student
GROUP BY Grade
HAVING Number of Students >= 3;
Output
Grade Number of Students
A 5
B 4
F 3
SQL ORDER BY
SQL ORDER BY clause is used after the WHERE clause (i.e. after
filtering the data) to sort the result in either Ascending or Descending
order.
ASC: Keyword is used to arrange the result in Ascending Order
DESC: Keyword is used to arrange the result in Descending Order.
Note:
1. Ascending is a default sort order.
2. ORDER BY clause comes after the WHERE, GROUP BY, and
HAVING clause (if present in query)
Syntax:
SELECT column_list
FROM table_name
WHERE conditions
ORDER BY column_names [ASC | DESC];
SELECT *
FROM Employee
ORDER BY CTC DESC;.
output
Employee Month of CTC(in
Name Gender Department Education
ID Joining Lacs)
1001 Ajay M Engineering Doctoral January 25
1002 Babloo M Engineering UG February 23
1005 Evina F Marketing UG March 16
1003 Chhavi F HR PG March 15
1004 Dheeraj M HR UG January 12
1006 Garima M Sales UG December 10
1007 Fredy F Sales PG March 10
1008 Hans M Admin PG November 8
1009 Ivanka F Admin Intermediate April 7
1010 Jai M Peon December 4
SELECT *
FROM Employee
WHERE Month of Joining= ‘March’
ORDER BY CTC DESC;
output