0% found this document useful (0 votes)
26 views33 pages

CS SE IT 1105 Database Management Systems - Lecture 05

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

CS SE IT 1105 Database Management Systems - Lecture 05

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

CS/SE/IT 1105

Database Management Systems


Lecture 05
Structured Query Language (SQL)
24/01/2023

Bachelor of Science(Hons) in Computer Science|Software Engineering|Information Technology


Department of Computer Science
Faculty of Computing & Technology
Saegis Campus
Nugegoda.

Ms. Chathurangi D Weerasinghe, MSc (UCSC,Col), BSc (Ruh) Seagis Campus 1


Lecturecontent
❑ Introduction to SQL

❑ Data definition language

❑ Data manipulation language

Ms. Chathurangi D Weerasinghe, MSc (UCSC,Col), BSc (Ruh) Seagis Campus 2


Lecturecontent
At the end of this lecture students should be able to
❑Write syntactically correct SQL statements to create and modify
relations in a RDBMS

❑Write syntactically correct SQL statements to answer user defined


queries in a RDBMS

Ms. Chathurangi D Weerasinghe, MSc (UCSC,Col), BSc (Ruh) Seagis Campus 3


SQL
❑ SQL Initially called SEQUEL (for Structured English QUEry Language)
❑It was developed for an experimental relational database system called
System R
❑A joint effort between ANSI (American National Standard Institute) and ISO
(International Standards Organization) led to a standard version of SQL in 1986
(SQL1, SQL-86, etc.)
❑Major revisions have been proposed and SQL2 (also called SQL-92) has
subsequently been developed

Ms. Chathurangi D Weerasinghe, MSc (UCSC,Col), BSc (Ruh) Seagis Campus 4


Relational Model Vs.SQL
Terminology

Relational Model SQL

Relation Table

Attribute Column

Tuple Row

Ms. Chathurangi D Weerasinghe, MSc (UCSC,Col), BSc (Ruh) Seagis Campus 5


SQL: Review (contd.)
SQL is a comprehensive database language:
❑ Data Definition Language (DDL)
❑ Data Manipulation Language (DML)
❑ Facilities for security & authorization
❑ Facilities for transaction processing
❑Facilities for embedding SQL in general purpose languages
(Embedded SQL)…..

Ms. Chathurangi D Weerasinghe, MSc (UCSC,Col), BSc (Ruh) Seagis Campus 6


Data Definition Language(DDL)
❑DDL is the subset of SQL that supports the creation, deletion and
modifications for tables and views.
❑ Constraints can be defined on the tables
Constraint Purpose
Not Null Ensure that column doesn’t have null values
Unique Ensure that column doesn’t have duplicate values
Primary key Defines the primary key
Foreign key Defines a foreign key
Default Defines a default value for a column (When no values are
given)
Check Validates data in a column

Ms. Chathurangi D Weerasinghe, MSc (UCSC,Col), BSc (Ruh) Seagis Campus 7


Creating a Table - Example
CREATE TABLE STUDENT
(
studentId INTEGER PRIMARY KEY,
sName VARCHAR (30) NOT NULL,
nic CHAR(10) UNIQUE,
gpa FLOAT,
progId VARCHAR(10) DEFAULT ‘IT’,
FOREIGN KEY (progId) REFERENCES programs(id);

Ms. Chathurangi D Weerasinghe, MSc (UCSC,Col), BSc (Ruh) Seagis Campus 8


Modifications toTables
ALTER commands – to alter the definition of the object
• Ex : Adding a new column to a table
• ALTER TABLE student ADD age INT
• Ex : Adding a new constraint to a column
• ALTER TABLE student ADD CONSTRAINT chk_age CHECK (age > 18)
• Ex : removing a column from a table
• ALTER TABLE student DROP COLUMN age

DROP commands – for dropping objects


• Ex: Deleting a table
• DROP TABLE Employee

Ms. Chathurangi D Weerasinghe, MSc (UCSC,Col), BSc (Ruh) Seagis Campus 9


Data Manipulation Language(DML)
DML is the subset of SQL that allows users to write statements to
insert, delete, modify and display rows.
❑ INSERT INTO student VALUES (1000, ‘Amal’, ‘123456789V’, 3.2, ‘BM’)
❑INSERT INTO student(studentId, sName, nic) VALUES (1001, ‘Nimali’,
‘234567890V’)

StudentID SName nic gpa progId

1000 Amal 123456789V 3.2 BM

1001 Nimali 234567890V Null IT

Ms. Chathurangi D Weerasinghe, MSc (UCSC,Col), BSc (Ruh) Seagis Campus 10


Data Manipulation Language(DML)(Contd.)
❑ Deleting a row
• DELETE student WHERE studentId=1000

❑ Updating a row
• UPDATE student

✓ SET gpa=2.8

✓ WHERE studentId=1001

Ms. Chathurangi D Weerasinghe, MSc (UCSC,Col), BSc (Ruh) Seagis Campus 11


Selectclause
Select clause in SQL is the basic statement for retrieving information
from a database
Basic form
SELECT <attributes>
FROM <one or more relations>
WHERE <conditions>
Ex : display ids of all students whose gpa is above 3.0
❑ Select StudentId from student where gpa> 3.0

Ms. Chathurangi D Weerasinghe, MSc (UCSC,Col), BSc (Ruh) Seagis Campus 12


Clauses and operators used with SELECT
• IS [NOT] NULL operator
• LIKE operator
• DISTINCT operators
• BETWEEN operator
• ORDER BY clause
• Joins (inner & outer)
• Nested query (IN/SOME/ANY, ALL), [NOT] EXISTS
• Aggregate functions
• GROUP BY – HAVING clauses

Ms. Chathurangi D Weerasinghe, MSc (UCSC,Col), BSc (Ruh) Seagis Campus 13


LIKEoperator
•Used for matching patterns Student
•Syntax : <string> LIKE <pattern> StudentID Name gpa progId

• <pattern> may contain two special 1000 Amal 3.2 BM

symbols: 1001 Nimali Null IT


• % = any sequence of characters 1002 Aruni 3.0 SE
• _ = any single character 1003 Surani 2.5 IT

Ex : Find students whose name starts with a ‘A’ Name


Amal
Select Name From student where Name Like ‘A%’ Aruni

Ms. Chathurangi D Weerasinghe, MSc (UCSC,Col), BSc (Ruh) Seagis Campus 14


IS[NOT] NULLoperator
Student
• IS NULL :Used to check whether attribute StudentID Name gpa progId
value is null 1000 Amal 3.2 BM
1001 Nimali Null IT
•Ex : Find studentIDs of the students who have not
1002 Aruni 3.0 SE
completed a semester yet.
1003 Surani 2.5 IT
• Select studentId
1004 Imali Null BM
• From student
• Where gpa IS NULL StudentID
1001
1004

Ms. Chathurangi D Weerasinghe, MSc (UCSC,Col), BSc (Ruh) Seagis Campus 15


DISTINCToperator
• In a table, a column may contain Student StudentID Name gpa progId
many duplicate values. 1000 Amal 3.2 BM

• Duplicates in results can be 1001 Nimali Null IT

eliminated using DISTINCT 1002 Aruni 3.0 SE

operator 1003 Surani 2.5 IT

• Ex : ProgId 1004 Imali Null BM


BM
Select progId Select DISTINCT progId
IT ProgId
From student SE From student BM

IT IT

BM SE

Ms. Chathurangi D Weerasinghe, MSc (UCSC,Col), BSc (Ruh) Seagis Campus 16


BETWEEN operator
Student
• Used to check whether attribute value is StudentID Name gpa progId
within a range 1000 Amal 3.2 BM
• Ex :Find the students who will be 1001 Nimali Null IT
obtaining a first class (3.7<=gpa<=4.0) 1002 Aruni 3.8 SE
1003 Surani 2.5 IT
Select studentID 1004 Imali 4.0 BM
From student
Where gpa between 3.7 and 4.00 StudentID
1002
1004

Ms. Chathurangi D Weerasinghe, MSc (UCSC,Col), BSc (Ruh) Seagis Campus 17


ORDERBYClause
Student
• Used to order results based on a given field
StudentID Name gpa progId
• Ordering is ascending (ASC), unless you 1000 Amal 3.2 BM
specify the DESC keyword 1001 Nimali 2.8 IT
• Ex : display the student names and gpa’s in 1002 Aruni 3.8 SE
the ascending order of gpa’s. Name gpa 1003 Surani 2.5 IT
Select Name, gpa Surani 2.5 1004 Imali 4.0 BM
From student Nimali 2.8
Order by gpa Amal 3.2
Aruni 3.8
Imali 4.0

Ms. Chathurangi D Weerasinghe, MSc (UCSC,Col), BSc (Ruh) Seagis Campus 18


[INNER]Join
• Joins two tables based on a certain Student Program
condition SID Name gpa pid progId years Offer By
• Ex : Find the names of students who 1000 Amal 3.2 BM BM 3 CCCU
follow programs offered by Saegis 1001 Nimali 2.8 IT IT 4 Saegis
Select s.Name
1002 Aruni 3.8 SE
From student s, program p SE 3 Pearson
where s.pid=p.ProgId and offerBy=‘Saegis’ 1003 Surani 2.5 IT
Or
Select s.Name Name
From student s INNER JOIN program p on Nimali
s.pid=p.progId
Where offerBy=‘Saegis’ Surani

Ms. Chathurangi D Weerasinghe, MSc (UCSC,Col), BSc (Ruh) Seagis Campus 19


LEFTOUTERJOIN
Student Program
•Returns all rows from the table on the left
hand side of join, with the matching rows in SID Name gpa pid progId years Offer By
the table on the right hand side of the join. 1000 Amal 3.2 BM BM 3 CCCU
1001 Nimali 2.8 IT 4 Saegis
•The result is NULL in the right side when there IT
1002 Aruni 3.8 SE
is no match.
1003 Surani 2.5 IT
• Ex : For all the students display the name and
the offering institute
Select s.Name, p.offerBy Name offerBy
From student s LEFT OUTER JOIN program p Amal CCCU
on s.pid=p.progId Nimali Saegis
Aruni NULL
Surani Saegis
Ms. Chathurangi D Weerasinghe, MSc (UCSC,Col), BSc (Ruh) Seagis Campus 20
RIGHTOUTER JOIN
•Returns all rows from the table on the right Student Program
hand side of join, with the matching rows in SID Name gpa pid progId years Offer By
the table on the left hand side of the join. 1000 Amal 3.2 BM BM 3 CCCU
•The result is NULL in the left side when there 1001 Nimali 2.8 IT IT 4 Saegis
is no match. 1002 Aruni 3.8 SE 3 Pearson
SE
• Ex : For all the programs display the offering
1003 Surani 2.5 IT
institute and names of the students
following
Select s.Name, p.offerBy Name offerBy
From student s RIGHT OUTER JOIN program p Amal CCCU
on s.pid=p.progId
Nimali Saegis
Surani Saegis
NULL Pearson
Ms. Chathurangi D Weerasinghe, MSc (UCSC,Col), BSc (Ruh) Seagis Campus 21
INoperator
Student Grades
Used to check whether attribute value
matches any value within a value list SID Name gpa
SID cid Grade
1000 IT102 A
Ex : Find the students who has 1000 Amal 3.2
1000 IT100 B
obtained a ‘A’. 1001 Nimali 2.8
1001 IT102 A
1002 Aruni 3.8
Select s.Name 1002 IT102 C
From Student s 1002 IT200 C
Where s.SID IN ( Select SID Name
Amal
from Grades
Nimali
Where Grade=‘A’)

Ms. Chathurangi D Weerasinghe, MSc (UCSC,Col), BSc (Ruh) Seagis Campus 22


EXISTS operator
• Used to check if subquery returns Student Grades
any rows SID Name gpa SID cid Grade
1000 Amal 3.2 1000 IT102 A
•Ex : Find the students who has IT100 B
1001 Nimali 2.8 1000
obtained a ‘A’.
1002 Aruni 3.8 1001 IT102 A
Select s.Name 1002 IT102 C
From Student s 1002 IT200 C
Name
Where EXISTS ( Select *
Amal
from grades g Nimali

Where g.SID=s.SID and g.Grade=‘A’

Ms. Chathurangi D Weerasinghe, MSc (UCSC,Col), BSc (Ruh) Seagis Campus 23


Comparison operators with SOME,ANY& ALL
•Comparison operators such as =, <>, > ,>= ,< and <= could be modified using
operators SOME, ANY and ALL.
• SOME and ANY : used to compare a value to a list or subquery. Return true if at least
one of the comparison evaluates as true.
• ALL : used to compare a value to a list or subquery. If all of the comparisons
evaluate to true then the result of the ALL expression will be true. Otherwise the
result will be false

Ms. Chathurangi D Weerasinghe, MSc (UCSC,Col), BSc (Ruh) Seagis Campus 24


ANY and SOMEoperators
Student
Ex : Find BM students who has
StudentID Name Gpa progId
gpa greater than any of the IT
1000 Amal 3.2 BM
students.
1001 Nimali 2.8 IT
1002 Aruni 3.8 SE
Select s.Name
1003 Surani 2.5 BM
From student s
1004 Imali 4.0 IT
Where s.progID=‘BM’ and
s.gpa > ANY (
Name
select s1.gpa
from student s1 where Amal
s1.progId=‘IT’)

Ms. Chathurangi D Weerasinghe, MSc (UCSC,Col), BSc (Ruh) Seagis Campus 25


ALL operator
Student
Ex : Find IT students who has StudentID Name gpa progId
gpa greater than all the BM 1000 Amal 3.2 BM
students. 1001 Nimali 2.8 IT
1002 Aruni 3.8 SE
Select s.Name 1003 Surani 2.5 BM
From student s
1004 Imali 4.0 IT
Where s.progID=‘IT’ and
s.gpa > ALL (
select s1.gpa Name
from student s1 where Imali
s1.progId=‘BM’)

Ms. Chathurangi D Weerasinghe, MSc (UCSC,Col), BSc (Ruh) Seagis Campus 26


Aggregation
❑An aggregate function summarizes the results of an expression over a number of rows,
returning a single value.

❑Some of the commonly used aggregate functions are SUM, COUNT, AVG, MIN and MAX

Ms. Chathurangi D Weerasinghe, MSc (UCSC,Col), BSc (Ruh) Seagis Campus 27


Aggregation(Contd.)
Ex : Find the average, minimum, Student StudentID Name gpa progId
maximum gpa of students 1000 Amal 3.2 BM
1001 Nimali 2.8 IT
1002 Aruni 3.8 SE
1003 Surani 2.5 BM
1004 Imali 4.0 IT

Select AVG(gpa), MIN(gpa), MAX(gpa)


From student AVG(gpa) MIN(gpa) MAX(gpa)
3.26 2.5 4.0

Ms. Chathurangi D Weerasinghe, MSc (UCSC,Col), BSc (Ruh) Seagis Campus 28


Grouping (GROUP BYClause)
•Groups the data in tables and produces a single summary row for each
group.
•Grouping is done based on a values in a given field
•When using group by
• Each item in the SELECT list must be single valued per group.
• SELECT clause may only contain Columns names, aggregate function, constants or an
expression involving combinations of the above.
• All column names in SELECT list must appear in the GROUP BY clause unless the name
is used only in the aggregate function.

Ms. Chathurangi D Weerasinghe, MSc (UCSC,Col), BSc (Ruh) Seagis Campus 29


Grouping(Contd.)
Ex: Count the number of students Student SID CID Grade
SID CID Grade
who has followed each module.
1000 DBII A 1000 DBII A
1000 SEI B 1001 DBII A
1001 DBII A 1002 DBII C
1002 DBII C 1000 SEI B
1002 SPD C 1002 SPD C
Select CID, Count(SID)
From Student CID Count (SID)
Group by CID DBII 3
SEI 1
SPD 1

Ms. Chathurangi D Weerasinghe, MSc (UCSC,Col), BSc (Ruh) Seagis Campus 30


HAVINGClause
Used to apply conditions on the Student SID CID Grade
SID CID Grade
groupings
1000 DBII A 1000 DBII A
Ex: Find courses which is followed by 1000 SEI B 1001 DBII A
more than two students 1001 DBII A 1002 DBII C
1002 DBII C 1000 SEI B
Select CID, Count(SID)
From course 1002 SPD C 1002 SPD C
Group by CID
Having count(SID)>2
CID Count (SID)
DBII 3

Ms. Chathurangi D Weerasinghe, MSc (UCSC,Col), BSc (Ruh) Seagis Campus 31


SQL:Review (contd.)
Summary of SQL Queries…

❑ SELECT <attribute-list>
❑ FROM <table-list>
❑ [WHERE <condition>]
❑ [GROUP BY <group attribute(s)>]
❑ [HAVING <group condition>]
❑ [ORDER BY <attribute list>];

Ms. Chathurangi D Weerasinghe, MSc (UCSC,Col), BSc (Ruh) Seagis Campus 32


Any Questions?

Ms. Chathurangi D Weerasinghe, MSc (UCSC,Col), BSc (Ruh) Seagis Campus 33

You might also like