0% found this document useful (0 votes)
82 views92 pages

Course - Database Management Systems (DBMS) : Course Instructor Dr. K. Subrahmanyam Department of CSE, KLU

This document discusses a Database Management Systems course taught by Dr. K. Subrahmanyam in the Department of Computer Science and Engineering at KL University. The document covers DML statements like SELECT, UPDATE, DELETE, and INSERT used to extract, update, delete and insert data into databases. It also discusses the INSERT INTO and UPDATE statements in SQL and provides examples. Finally, it discusses schema change statements in SQL like DROP TABLE and TRUNCATE TABLE and provides examples of using these statements.

Uploaded by

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

Course - Database Management Systems (DBMS) : Course Instructor Dr. K. Subrahmanyam Department of CSE, KLU

This document discusses a Database Management Systems course taught by Dr. K. Subrahmanyam in the Department of Computer Science and Engineering at KL University. The document covers DML statements like SELECT, UPDATE, DELETE, and INSERT used to extract, update, delete and insert data into databases. It also discusses the INSERT INTO and UPDATE statements in SQL and provides examples. Finally, it discusses schema change statements in SQL like DROP TABLE and TRUNCATE TABLE and provides examples of using these statements.

Uploaded by

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

Course – Database Management

Systems (DBMS)

Course Instructor
Dr. K. Subrahmanyam
Department of CSE,KLU

CSE, KL luniversity
DML statements
• SELECT - extracts data from a database
• UPDATE - updates data in a database
• DELETE - deletes data from a database
• INSERT INTO - inserts new data into a
database

CSE, KL luniversity
The INSERT INTO Statement
• The INSERT INTO statement is used to insert a new row in a table.
• SQL INSERT INTO Syntax
It is possible to write the INSERT INTO statement in two forms.

The first form doesn't specify the column names where the data will be inserted, only
their values:
INSERT INTO table_name
VALUES (value1, value2, value3,...)

The second form specifies both the column names and the values to be inserted:
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)

CSE, KL luniversity
SQL UPDATE Statement
• The UPDATE statement is used to update existing records in a table.
SQL UPDATE Syntax
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value

update student1
set emailid=‘[email protected]
where name=‘Dinesh’;

CSE, KL luniversity
Topics Covered in Todays Class
- Schema change statements in SQL
- Basic SQL queries

CSE, KL luniversity
Schema change statements in SQL
• Drop command can be used to drop tables or constraints.

• The DROP TABLE Statement


General Syntax: DROP TABLE table_name

• The DROP DATABASE Statement


General Syntax: DROP DATABASE database_name

• The TRUNCATE TABLE Statement


What if we only want to delete the data inside the table, and not the table itself ?
Then, use the TRUNCATE TABLE statement:
General Syntax: TRUNCATE TABLE table_name

CSE, KL luniversity
Schema change statements in SQL
student Table
• department
Drop command
Table
USN Name Dep_nu
m
D_ID Dep_name
1BM14CS001 Avinash 10
10 CSE 1BM14CS002 Balaji 10
20 ISE 1BM14CS003 Ram 10
1BM14CS004 Mohan 20
1BM14CS005 Nikil 20

Assume the above two tables were created using following create commands
create table department (D_ID integer, Dep_name varchar(3),primary key (D_ID));

create table student (USN varchar(10),Name varchar(20),Dep_num integer,


primary key(usn),foreign key (Dep_num) references department(D_ID));

Note: Assume after above two table creation, values shown above are inserted
into tables
CSE, KL luniversity
Schema change statements in SQL
student Table
• department
Drop command
Table
USN Name Dep_nu
m
D_ID Dep_name
1BM14CS001 Avinash 10
10 CSE 1BM14CS002 Balaji 10
20 ISE 1BM14CS003 Ram 10
1BM14CS004 Mohan 20
1BM14CS005 Nikil 20

Question
1. When the following SQL Command is executed, what will be the output

insert into student values (‘1BM14CS006’,’Patel’,50);

CSE, KL luniversity
Schema change statements in SQL
student Table
• department
Drop command
Table
USN Name Dep_nu
m
D_ID Dep_name
1BM14CS001 Avinash 10
10 CSE 1BM14CS002 Balaji 10
20 ISE 1BM14CS003 Ram 10
1BM14CS004 Mohan 20
1BM14CS005 Nikil 20

Question
1. When the following SQL Command is executed, what will be the output

insert into student values (‘1BM14CS006’,’Patel’,50);

Cannot add a row: a foreign key constraint fails


CSE, KL luniversity
Schema change statements in SQL
student Table
• department
Drop command
Table
USN Name Dep_nu
m
D_ID Dep_name
1BM14CS001 Avinash 10
10 CSE 1BM14CS002 Balaji 10
20 ISE 1BM14CS003 Ram 10
1BM14CS004 Mohan 20
1BM14CS005 Nikil 20

Question
1. When the following SQL Command is executed, what will be the output

drop table department;

CSE, KL luniversity
Schema change statements in SQL
student Table
• department
Drop command
Table
USN Name Dep_nu
m
D_ID Dep_name
1BM14CS001 Avinash 10
10 CSE 1BM14CS002 Balaji 10
20 ISE 1BM14CS003 Ram 10
1BM14CS004 Mohan 20
1BM14CS005 Nikil 20

Question
1. When the following SQL Command is executed, what will be the output

drop table department;

Department Table will not be deleted because of foreign key constraint


CSE, KL luniversity
Schema change statements in SQL
student Table
• department
Drop command
Table
USN Name Dep_nu
m
D_ID Dep_name
1BM14CS001 Avinash 10
10 CSE 1BM14CS002 Balaji 10
20 ISE 1BM14CS003 Ram 10
1BM14CS004 Mohan 20
1BM14CS005 Nikil 20

Question
1. When the following SQL Command is executed, what will be the output

drop table student;

CSE, KL luniversity
Schema change statements in SQL
student Table
• department
Drop command
Table
USN Name Dep_nu
m
D_ID Dep_name
1BM14CS001 Avinash 10
10 CSE 1BM14CS002 Balaji 10
20 ISE 1BM14CS003 Ram 10
1BM14CS004 Mohan 20
1BM14CS005 Nikil 20

Question
1. When the following SQL Command is executed, what will be the output

drop table student;

student Table will be deleted


CSE, KL luniversity
Schema change statements in SQL
student Table
• department
Drop command
Table
USN Name Dep_Nu
m
D_ID Dep_name
1BM14CS001 Avinash 10
10 CSE 1BM14CS002 Balaji 10
20 ISE 1BM14CS003 Ram 10
1BM14CS004 Mohan 20
1BM14CS005 Nikil 20

Question
1. When the following SQL Command is executed, what will be the output

drop table department cascade constraints;

CSE, KL luniversity
Schema change statements in SQL
student Table
• department
Drop command
Table
USN Name Dep_Nu
m
D_ID Dep_name
1BM14CS001 Avinash 10
10 CSE 1BM14CS002 Balaji 10
20 ISE 1BM14CS003 Ram 10
1BM14CS004 Mohan 20
1BM14CS005 Nikil 20

Question
1. When the following SQL Command is executed, what will be the output

drop table department cascade constraints;

Department Table will be deleted and foreign key constraint to student


Table will be dropped
CSE, KL luniversity
Schema change statements in SQL
Student Table
• department
Drop command
Table
USN Name Dep_Nu
m
D_ID Dep_name
1BM14CS001 Avinash 10
10 CSE 1BM14CS002 Balaji 10
20 ISE 1BM14CS003 Ram 10
1BM14CS004 Mohan 20
1BM14CS005 Nikil 20

Question
1. Whether the following two SQL commands will be executed successfully

drop table department cascade constraints;


insert into student values (‘1BM14CS006’,’Patel’,50);

CSE, KL luniversity
Schema change statements in SQL
student Table
• department
Drop command
Table
USN Name Dep_Nu
m
D_ID Dep_name
1BM14CS001 Avinash 10
10 CSE 1BM14CS002 Balaji 10
20 ISE 1BM14CS003 Ram 10
1BM14CS004 Mohan 20
1BM14CS005 Nikil 20

Question
1. Whether the following two SQL commands will be executed successfully
drop table department cascade constraints;
insert into student values (‘1BM14CS006’,’Patel’,50);

Into the student table new row will be inserted with dep_num 50

CSE, KL luniversity
Schema change statements in SQL
• Drop Command
General syntax:
drop table table_name [drop_behavior]

There are two drop behavior options


1. Cascade: All constraints that references the table are dropped
automatically along with the table itself.
2. Restrict: Table is dropped only it is not referenced in any constraints.

CSE, KL luniversity
Schema change statements in SQL
• Alter command
General Syntax:
alter table table_name [add|drop|alter] column
column_name;

Adding or dropping column


Changing column definition
Adding or dropping table constraints
CSE, KL luniversity
Schema change statements in SQL
• Alter command
Student Table
USN Name Dep_Nu
m
1BM14CS001 Avinash 10
1BM14CS002 Balaji 10
1BM14CS003 Ram 10
1BM14CS004 Mohan 20
1BM14CS005 Nikil 20

CSE, KL luniversity
Schema change statements in SQL
• Alter command
Student Table
USN Name Dep_Nu
m
1BM14CS001 Avinash 10
1BM14CS002 Balaji 10
1BM14CS003 Ram 10
1BM14CS004 Mohan 20
1BM14CS005 Nikil 20

CSE, KL luniversity
Schema change statements in SQL
• Alter command
Student Table
USN Name Dep_Nu
m
1BM14CS001 Akash 10
1BM14CS002 Bharath 10
1BM14CS003 Ragu 10
1BM14CS004 Mohan 20
1BM14CS005 Nikil 20
Question
1. What will be the contents of the student table when the following SQL
commands are executed ?

CSE, KL luniversity
Schema change statements in SQL
• Alter command
Student Table
USN S-Name Dep_Num email
1BM14CS001 Akash 10
1BM14CS002 Bharath 10
1BM14CS003 Ragu 10
1BM14CS004 Mohan 20
1BM14CS005 Nikil 20
1BM14CS006 Patel 10 patel@gmail.
com

CSE, KL luniversity
Basic queries in SQL
• To retrieve data from database table, basic
SQL statement is SELECT
• Syntax:
select column_name_list
from table_name
where condition;

CSE, KL luniversity
SQL select statement
student Table
usn name dep_nu marks
m
1BM14CS001 Avinash 10 100
1BM14CS002 Balaji 10 80
1BM14CS003 Chandan 10 45
1BM14CS004 Dinesh 10 60
1BM14IS001 Avinash 20 90

CSE, KL luniversity
SQL select statement
Student Table
usn name dep_nu marks
m
1BM14CS001 Avinash 10 100
1BM14CS002 Balaji 10 80
1BM14CS003 Chandan 10 45
1BM14CS004 Dinesh 10 60
1BM14IS001 Avinash 20 90

CSE, KL luniversity
SQL select statement
Student Table
usn name dep_nu marks
m
1BM14CS001 Avinash 10 100
1BM14CS002 Balaji 10 80
1BM14CS003 Chandan 10 45
1BM14CS004 Dinesh 10 60
1BM14IS001 Avinash 20 90

CSE, KL luniversity
SQL select statement
Student Table
usn name dep_nu marks
m
1BM14CS001 Avinash 10 100
1BM14CS002 Balaji 10 80
1BM14CS003 Chandan 10 45
1BM14CS004 Dinesh 10 60
1BM14IS001 Avinash 20 90

CSE, KL luniversity
SQL select statement
Student Table
usn name dep_nu marks
m
1BM14CS001 Avinash 10 100
1BM14CS002 Balaji 10 80
1BM14CS003 Chandan 10 45
1BM14CS004 Dinesh 10 60
1BM14IS001 Avinash 20 90

CSE, KL luniversity
Select statement
• Where clause conditions
=
!=
<
>
<=
>=

CSE, KL luniversity
SQL select statement
student Table
usn name dep_nu marks
department Table m
D_ID D-Name 1BM14CS001 Avinash 10 100
1BM14CS002 Balaji 10 80
10 CSE
1BM14CS003 Chandan 10 45
20 ISE
1BM14CS004 Dinesh 10 60
1BM14IS005 Avinash 20 90

1. List out the USN’s of the students who belong to department number 10?

CSE, KL luniversity
SQL select statement
student Table
usn name dep_nu marks
department Table m
D_ID D-Name 1BM14CS001 Avinash 10 100
1BM14CS002 Balaji 10 80
10 CSE
1BM14CS003 Chandan 10 45
20 ISE
1BM14CS004 Dinesh 10 60
1BM14IS005 Avinash 20 90

1. List out the USN’s of the students who belong to department number 10?

CSE, KL luniversity
SQL select statement
student Table
usn name dep_nu marks
department Table m
d_id dep_name 1BM14CS001 Avinash 10 100
1BM14CS002 Balaji 10 80
10 CSE
1BM14CS003 Chandan 10 45
20 ISE
1BM14CS004 Dinesh 10 60
1BM14IS005 Avinash 20 90

Question
1. List out the USN’s of the students who belong to CSE department ?

CSE, KL luniversity
SQL select statement
student Table
usn name dep_nu marks
department Table m
d_id dep_name 1BM14CS001 Avinash 10 100
1BM14CS002 Balaji 10 80
10 CSE
1BM14CS003 Chandan 10 45
20 ISE
1BM14CS004 Dinesh 10 60
1BM14IS005 Avinash 20 90

Question
1. List out the USN’s of the students who belong to CSE department ?

CSE, KL luniversity
SQL select statement
student Table
usn name dep_nu marks
department Table m
d_id dep_name 1BM14CS001 Avinash 10 100
1BM14CS002 Balaji 10 80
10 CSE
1BM14CS003 Chandan 10 45
20 ISE
1BM14CS004 Dinesh 10 60
1BM14IS005 Avinash 20 90

CSE, KL luniversity
SQL select statement: Aliasing
student Table
usn name dep_nu marks
department Table m
d_id dep_name 1BM14CS001 Avinash 10 100
1BM14CS002 Balaji 10 80
10 CSE
1BM14CS003 Chandan 10 45
20 ISE
1BM14CS004 Dinesh 10 60
1BM14IS005 Avinash 20 90

CSE, KL luniversity
SQL select statement: Distinct
Student Table
usn name dep_nu marks
m
1BM14CS001 Avinash 10 100
1BM14CS002 Balaji 10 80
1BM14CS003 Chandan 10 45
1BM14CS004 Dinesh 10 60
1BM14IS001 Avinash 20 90

Avinash
Balaji
Chandan
Dinesh
Avinash
CSE, KL luniversity
SQL select statement: Distinct
Student Table
usn name dep_nu marks
m
1BM14CS001 Avinash 10 100
1BM14CS002 Balaji 10 80
1BM14CS003 Chandan 10 45
1BM14CS004 Dinesh 10 60
1BM14IS001 Avinash 20 90

Avinash
Balaji
Chandan
Dinesh

CSE, KL luniversity
SQL select statement: Order By
Student Table
usn name dep_nu marks
m
1BM14CS001 Avinash 10 100
1BM14CS002 Balaji 10 80
1BM14CS003 Chandan 10 45
1BM14CS004 Dinesh 10 60
1BM14IS001 Avinash 20 90

CSE, KL luniversity
SQL select statement: Order By
Student Table
usn name dep_nu marks
m
1BM14CS001 Avinash 10 100
1BM14CS002 Balaji 10 80
1BM14CS003 Chandan 10 45
1BM14CS004 Dinesh 10 60
1BM14IS001 Avinash 20 90

CSE, KL luniversity
SQL select statement: Order By
Student Table
usn name dep_nu marks
m
1BM14CS001 Avinash 10 100
1BM14CS002 Balaji 10 80
1BM14CS003 Chandan 10 45
1BM14CS004 Dinesh 10 60
1BM14IS001 Avinash 20 90

Question
List usn, name of the students who belong to department number 10 ordered by
ascending order of their marks ?

CSE, KL luniversity
SQL select statement: Order By
Student Table
usn name dep_nu marks
m
1BM14CS001 Avinash 10 100
1BM14CS002 Balaji 10 80
1BM14CS003 Chandan 10 45
1BM14CS004 Dinesh 10 60
1BM14IS001 Avinash 20 90

Question
List usn, name of the students who belong to department number 10 ordered by
ascending order of their marks ?

CSE, KL luniversity
Activity: To do
Employee table
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7369 SMITH CLERK 7902 17-Dec-80 800   20
7499 ALLEN SALESMAN 7698 20-Feb-81 1600 300 30
7521 WARD SALESMAN 7698 22-Feb-81 1250 500 30
7566 JONES MANAGER 7839 02-Apr-81 2975   20
7654 MARTIN SALESMAN 7698 28-Sep-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-May-81 2850   30
7782 CLARK MANAGER 7839 09-Jun-81 2450   10
7788 SCOTT ANALYST 7566 09-Dec-82 3000   20
7839 KING PRESIDENT   17-Nov-81 5000   10
7844 TURNER SALESMAN 7698 08-Sep-81 1500 0 30
7876 ADAMS CLERK 7788 12-Jan-83 1100   20
7900 JAMES CLERK 7698 03-Dec-81 950   30
7902 FORD ANALYST 7566 03-Dec-81 3000   20
7934 MILLER CLERK 7782 23-Jan-82 1300   10

Write SQL queries for the following


1. Display all the information of the Employee table ?
2. Display unique Jobs from Employee table?
3. List names of the employees in the ascending order of their Salaries ?
CSE, KL luniversity
Topics Covered in Today’s class
Unit 1: Basic queries in SQL

w.r.t SELECT statement


SQL operators: LIKE, IN, BETWEEN

CSE, KL luniversity
Structured Query Language (SQL)
SQL can be divided into two parts:
1. The Data Manipulation Language (DML) and
2. The Data Definition Language (DDL)

1. DDL statements in SQL are:


– CREATE DATABASE - creates a new database
– ALTER DATABASE - modifies a database
– CREATE TABLE - creates a new table
– ALTER TABLE - modifies a table
– DROP TABLE - deletes a table

2. DML part of SQL:


– SELECT - extracts data from a database
– UPDATE - updates data in a database
– DELETE - deletes data from a database
– INSERT INTO - inserts new data into a database

CSE, KL luniversity
The SQL SELECT Statement
• The SELECT statement is used to select data from a database.
• The result is stored in a result table, called the result-set.

• Note: SQL is not case sensitive. SELECT is the same as select.


CSE, KL luniversity
w.r.t to SELECT SQL statement
1. SQL Alias: We can give a table or a column another name by using an alias.
• SQL Alias Syntax for Tables
SELECT column_name(s)
FROM table_name AS alias_name;
• SQL Alias Syntax for Columns
SELECT column_name AS alias_name
FROM table_name;

2. The DISTINCT keyword can be used to return only distinct (different) values.
• SQL SELECT DISTINCT Syntax
SELECT DISTINCT column_name(s)
FROM table_name

CSE, KL luniversity
w.r.t to SELECT sql statement
3. The ORDER BY keyword is used to sort the result-set by a specified column.
• SQL ORDER BY Syntax
SELECT column_name(s)
FROM table_name
ORDER BY column_name(s) ASC | DESC;
4. The WHERE clause is used to filter records. The WHERE clause is used to extract only those records that fulfill
a specified criterion.
• SQL WHERE Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name operator value
• With the WHERE clause, the following operators can be used:

CSE, KL luniversity
Substring Pattern Matching: SQL LIKE Operator

• The LIKE operator is used in a WHERE clause to search for a specified pattern in a
column.
• SQL LIKE Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern

CSE, KL luniversity
SQL LIKE Operator
• The LIKE operator is used in a WHERE clause to search for a specified pattern in a
column.
• SQL LIKE Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern

Example: Write SQL statement to list the names starting with letter “A” from following
student table.

CSE, KL luniversity
SQL LIKE Operator
Write SQL statement to list the names starting with letter “A”
from the following student table.

CSE, KL luniversity
SQL LIKE Operator
Write SQL statement to list the names starting with letter “A”
from the following student table.

Note:
Wildcard character %, A substitute for zero or more characters

CSE, KL luniversity
SQL LIKE Operator

Question
Write SQL statement to list name of the students whose names end
with letter ‘h’?

Note:
Wildcard character %, A substitute for zero or more characters

CSE, KL luniversity
SQL LIKE Operator

Question
Write SQL statement to list name of the students whose names end
with letter ‘h’?

CSE, KL luniversity
SQL LIKE Operator

Question
Write SQL statement to list name of the students whose names
are having the substring ‘in’?

Note:
Wildcard character %, A substitute for zero or more characters

CSE, KL luniversity
SQL LIKE Operator

Question
Write SQL statement to list name of the students whose names
are having the substring ‘in’?

CSE, KL luniversity
SQL LIKE Operator

Question
Write SQL statement to list name of the students whose names
start with letter ‘A’ or ‘B’

CSE, KL luniversity
SQL LIKE Operator

Question
Write SQL statement to list name of the students whose names
start with letter ‘A’ or ‘B’

CSE, KL luniversity
SQL LIKE Operator

Question
Write SQL statement to list name of the students whose names
start with letter ‘A’ or ‘D’ but end with letter ‘h’

CSE, KL luniversity
SQL LIKE Operator

Question
Write SQL statement to list name of the students whose names
start with letter ‘A’ or ‘D’ but end with letter ‘h’

CSE, KL luniversity
SQL LIKE Operator

Question
Write SQL statement to list name of the students whose third letter in the
name is ‘a’ .

CSE, KL luniversity
SQL LIKE Operator

Question
Write SQL statement to list name of the students whose third letter in the
name is ‘a’ .

Note:
An underscore (_) in the pattern matches exactly one character
A percent sign (%) in the pattern can match zero or more characters
underscore (_) and percent sign (%)  are referred as wildcard characters
CSE, KL luniversity
SQL Wildcard Characters
• In SQL, wildcard characters are used with the SQL LIKE operator.
• SQL wildcards are used to search for data within a table. 

With SQL, the wildcards are:


Wildcard Description
% A substitute for zero or more characters
_ A substitute for a single character
[charlist] Sets and ranges of characters to match
[^charlist] Matches only a character NOT specified within the brackets
or
[!charlist]

CSE, KL luniversity
SQL IN Operator
• The IN operator allows you to specify multiple values in a WHERE clause.
SQL IN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...)

CSE, KL luniversity
SQL IN Operator
• The IN operator allows you to specify multiple values in a WHERE clause.
SQL IN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...)

Example: List USN's of the students with name equal to "Avinash" or "Dinesh" from the table
above.

CSE, KL luniversity
SQL IN Operator
• The IN operator allows you to specify multiple values in a WHERE clause.

Example: List USN's of the students with name equal to "Avinash" or "Dinesh" from the table
above.

CSE, KL luniversity
SQL BETWEEN operator
• The BETWEEN operator selects a range of data between two values. The values can be
numbers, text, or dates.
SQL BETWEEN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2

Example: List USN’s and Names of students whose marks is in between 40 and 80

CSE, KL luniversity
SQL BETWEEN operator
• The BETWEEN operator selects a range of data between two values. The values can be
numbers, text, or dates.

Example: List USN’s and Names of students whose marks is in between 40 and 80

CSE, KL luniversity
Activity To Do
Consider table of Nobel prize winners:
nobel(yr, subject, winner)
Answer the following

CSE, KL luniversity
Activity To Do
Consider table of Nobel prize winners:
nobel(yr, subject, winner)
Answer the following

CSE, KL luniversity
Activity To Do
Consider table of Nobel prize winners:
Answer
nobel(yr, the following
subject, winner)

CSE, KL luniversity
Activity To Do
Consider table of Nobel prize winners:
nobel(yr,
Answer subject, winner)
the following

CSE, KL luniversity
Aggregate Functions in SQL
• Why we need aggregate functions ??
• Example say we want find maximum marks scored by the
students in the class.

• We use aggregate function to group multiple rows together to


form a single value output.
CSE, KL luniversity
Topics Covered in Todays Class
Unit 1: Basic queries in SQL

Aggregate functions
Group BY Clause
Having Clause

CSE, KL luniversity
Arithmetic operators in Queries
• Arithmetic operators for addition (+), subtraction (-), multiplication(*), and
division (/) can be applied to numeric values or attributes with numeric
domain.

• Example:

CSE, KL luniversity
Aggregate Functions in SQL
Useful aggregate functions:
• AVG() - Returns the average value
• COUNT() - Returns the number of rows
• MAX() - Returns the largest value
• MIN() - Returns the smallest value
• SUM() - Returns the sum

CSE, KL luniversity
Aggregate Functions in SQL
The AVG() Function
• The AVG() function returns the average value of a numeric column.
• SQL AVG() Syntax
SELECT AVG(column_name) FROM table_name

Find average marks of all the students in the class.

CSE, KL luniversity
Aggregate Functions in SQL
• SQL COUNT() Function
The COUNT() function returns the number of rows that matches a specified
criteria.
• SQL COUNT(column_name) Syntax
The COUNT(column_name) function returns the number of values (NULL
values will not be counted) of the specified column:
SELECT COUNT(column_name) FROM table_name

• SQL COUNT(*) Syntax


The COUNT(*) function returns the number of records in a table:
SELECT COUNT(*) FROM table_name

• SQL COUNT(DISTINCT column_name) Syntax


The COUNT(DISTINCT column_name) function returns the number of
distinct values of the specified column:
SELECT COUNT(DISTINCT column_name) FROM table_name

CSE, KL luniversity
Aggregate Functions in SQL
• SQL COUNT(*) Syntax
The COUNT(*) function returns the number of records in a table.
.

Find total number of records in the above student table

CSE, KL luniversity
Aggregate Functions in SQL
• SQL COUNT(*) Syntax
The COUNT(*) function returns the number of records in a table.

Find total number of students who belong to department number


10

CSE, KL luniversity
Aggregate Functions in SQL
• SQL COUNT(column_name) Syntax
The COUNT(column_name) function returns the number of values
(NULL values will not be counted) of the specified column.

Find total number of students who have Email IDs

CSE, KL luniversity
Aggregate Functions in SQL

What will be the out put of following SQL queries ?

CSE, KL luniversity
Aggregate Functions in SQL

CSE, KL luniversity
SQL GROUP BY Statement
• Why we need Group by Statement
• Say we want to find total number of students by department wise.

• Partition the set of records into groups based on certain criteria.


• Groups are formed on the basis of certain attribute.
• Aggregate functions are calculated for each group.

CSE, KL luniversity
SQL GROUP BY Statement
• The GROUP BY statement is used in conjunction with the aggregate
functions to group the result-set by one or more columns.
• SQL GROUP BY Syntax
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name

CSE, KL luniversity
SQL GROUP BY Statement
• The GROUP BY statement is used in conjunction with the aggregate
functions to group the result-set by one or more columns.

Find total number of students in each department

CSE, KL luniversity
SQL GROUP BY Statement

Whether the following SQL queries will “Find total number of


students in each department”

CSE, KL luniversity
SQL GROUP BY Statement

Whether the following SQL queries will “Find total number of


students in each department”

CSE, KL luniversity
SQL GROUP BY Statement
• The GROUP BY statement is used in conjunction with the aggregate
functions to group the result-set by one or more columns.

What will be the output of following SQL statement

CSE, KL luniversity
SQL GROUP BY Statement
• The GROUP BY statement is used in conjunction with the aggregate
functions to group the result-set by one or more columns.

What will be the output of following SQL statement

CSE, KL luniversity
Answer the following

CSE, KL luniversity
Answer the following

CSE, KL luniversity

You might also like