0% found this document useful (0 votes)
4 views

Dbms Lab 3

Uploaded by

aiml3b2023
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Dbms Lab 3

Uploaded by

aiml3b2023
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

1.

Querying (using ANY, ALL, IN, Exists, NOT EXISTS, UNION,


INTERSECT, Constraints etc.)
2. Queries using Aggregate functions, GROUP BY, HAVING and Creation and
dropping of Views.

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.

An aggregate function in SQL performs a calculation on multiple values


and returns a single value.
SQL provides many aggregate functions that include avg, count, sum,
min, max, etc.
An aggregate function ignores NULL values when it performs the
calculation, except for the count function.
The most common aggregate functions are:
COUNT()
Used to return the number of records in a table based on the mentioned
conditions.
Syntax:
[code]
SELECT COUNT(Column_Name)FROM TABLE_NAME
WHERE Condition;
[/code]

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]

What is GROUP BY in SQL?


GROUP BY in SQL is used to group all the rows that have the same (or
identical) value by one or more columns.
 It is used in conjunction with the aggregate function
 It follows WHERE clause in SELECT statement and precedes
the ORDER BY clause.
The below image shows how the GROUP BY clause works in SQL.

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

COUNT with GROUP BY


Example: Use the GROUP BY clause to count the number of
students by the grade.

Query

SELECT Grade, COUNT (StudentID) AS Number of Students


FROM Student
GROUP BY Grade;
Output
Grade Number of Students
A 5
B 4
C 1
F 3
O 2

GROUP BY with HAVING


Example: Find the grade in which the number of students is greater
than or equal to 3.
Query
SELECT Grade, COUNT (StudentID) AS Number of Students

FROM Student

GROUP BY Grade
HAVING Number of Students >= 3;

Output
Grade Number of Students
A 5
B 4
F 3

GROUP BY with ORDER BY


Example: Use the ORDER BY clause in the above example to
arrange the number of students in each grade in descending order.
Query
SELECT Grade, COUNT (StudentID) AS Number of Students
FROM Student
GROUP BY Grade
ORDER BY Number of Students DESC;
Output
Grade Number of Students
A 5
B 4
F 3
O 2
C 1

Key Differences Between WHERE and HAVING Clause


Here are the key differences between WHERE and HAVING clause in
SQL:
 The WHERE clause implements in row operations, whereas the
HAVING clause implements in column operations.
 The HAVING clause can contain an aggregate function, but the
WHERE clause cannot contain an aggregate function.
 You can use the WHERE clause with a single row function.
Whereas the HAVING clause is used with a multiple row
function.
 The HAVING clause is applied after the GROUP BY clause, but
the WHERE clause is applied before the GROUP BY clause.
 You can use the WHERE clause without the GROUP BY clause,
But you cannot use the HAVING clause without the GROUP
BY clause.
 You can use the WHERE clause with the SELECT, UPDATE,
and DELETE statements. Whereas you can use the HAVING
clause only with the SELECT statement.
 The WHERE clause in SQL allows you to filter data from specific
rows (individual rows) from a table based on certain conditions.
In contrast, the HAVING clause in SQL allows you to filter data
from a group of rows in a query based on conditions involving
aggregate values.
ORDER BY Clause in SQL
Sorting the result after extracting the data from the dataset is one of the
most basic thing we do to get the pattern or highest/lowest values in a
particular field (column). In SQL sorting is done using ORDER BY
clause.

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];

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
1003 Chhavi F HR PG March 15
1004 Dheeraj M HR UG January 12
1005 Evina F Marketing UG March 16
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
Now we will use the Employee dataset (that contains Employee ID,
Name, Gender, Department, Education, Month of Joining, and CTC) to
do some examples to get a clear understanding of how to use the
ORDER BY clause in SQL.

Sort According to Single Column


Example 1: Arrange the CTC of the employee in descending order.
Query

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

ORDER BY with WHERE


Example 2: Arrange the CTC (in decreasing) of the employees who
joined in the month of March.

SELECT *
FROM Employee
WHERE Month of Joining= ‘March’
ORDER BY CTC DESC;
output

Employee ID Name Gender Department Education Month of Joining CTC(in Lacs)


1005 Evina F Marketing UG March 16
1003 Chhavi F HR PG March 15
1007 Fredy F Sales PG March 10

You might also like