Course - Database Management Systems (DBMS) : Course Instructor Dr. K. Subrahmanyam Department of CSE, KLU
Course - Database Management Systems (DBMS) : Course Instructor Dr. K. Subrahmanyam Department of CSE, KLU
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.
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));
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
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
Question
1. When the following SQL Command is executed, what will be the output
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
Question
1. When the following SQL Command is executed, what will be the output
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
Question
1. When the following SQL Command is executed, what will be the output
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
Question
1. Whether the following two SQL commands will be executed successfully
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]
CSE, KL luniversity
Schema change statements in SQL
• Alter command
General Syntax:
alter table table_name [add|drop|alter] column
column_name;
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
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)
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.
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.
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.
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
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
CSE, KL luniversity
Aggregate Functions in SQL
• SQL COUNT(*) Syntax
The COUNT(*) function returns the number of records in a table.
.
CSE, KL luniversity
Aggregate Functions in SQL
• SQL COUNT(*) Syntax
The COUNT(*) function returns the number of records in a table.
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.
CSE, KL luniversity
Aggregate Functions in SQL
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.
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.
CSE, KL luniversity
SQL GROUP BY Statement
CSE, KL luniversity
SQL GROUP BY 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.
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.
CSE, KL luniversity
Answer the following
CSE, KL luniversity
Answer the following
CSE, KL luniversity