Download as pdf or txt
Download as pdf or txt
You are on page 1of 43

Database Management Abir Shafy Bindu

Assistant Engineer,
System DESCO
Structured Query
Language(SQL)
Data
Manipulation
Language
INSERT, UPDATE, DELETE
UPDATE: This command modifies data of
one or more records. An update command
syntax is UPDATE [table name] SET
[column name = value] where [condition].

Data INSERT: This command adds one or more


Manipulation records to a database table. The insert
command syntax is INSERT INTO [table

Language
name] [column(s)] VALUES [value(s)].

DELETE: This command removes one or


more records from a table according to
specified conditions. Delete command
syntax is DELETE FROM [table name]
where [condition].
INSERT
INSERT INTO tableName
(column1, column2, ...)
VALUES (value1, value2, ...)

INSERT INTO dbo.Patient


(Surname, FirstName, DOB, Sex, Weight, Height, Vaccinated)
VALUES (NULL, NULL,'2019-11-19', 'F', 14.0, 23.1, 'No')
UPDATE and DELETE
We will see next
Data Query
Language
SELECT
Semantics
Operator Operation
> Greater Than

Operators <
=
Less Than
Equal
<>, != Not Equal
>= Greater Than or Equal
<= Less Than or Equal
IN, NOT IN Check the attribute value matches with Specific
values.
LIKE, NOT LIKE String Matching
BETWEEN…AND Check the whether value is between the range
IS NULL, IS NOT Check the whether value is NULL
NULL
Operator Operation
> Greater Than

Operators <
=
Less Than
Equal
<>, != Not Equal
>= Greater Than or Equal
<= Less Than or Equal
IN, NOT IN Check the attribute value matches with Specific
values.
LIKE, NOT LIKE String Matching
BETWEEN…AND Check the whether value is between the range
IS NULL, IS NOT Check the whether value is NULL
NULL
The LIKE operator

S LIKE p: pattern matching on strings


p may contain two special symbols:
(1) % à any sequence of characters (zero or more)
(2) _ à any single character (exactly one)
The LIKE operator
ABIR
First_name LIKE p: pattern matching on
A%R
strings
p may contain two special symbols: LIKE ‘___R’

(1) % à any sequence of characters (zero or more) ABIR


(2) _ à any single character (exactly one)
R
Student
sid first_Name last_Name Gender city mobile no
11 Ashik Al Amin M Khulna 01212458695
21 Arif Zaman M Sylhet 01562400523

Practice Problem
32 Dipta Roy M Dhaka 01457203156
43 Fatema Farzana F Dhaka 06845201412
54 Saima Akhtar F Rajshahi 04875265489

• Write a SQL QUERY to show the students’


first_Name from Dhaka SELECT first_Name,
FROM STUDENT
• Write a SQL QUERY to display all attribute of WHERE city=‘Dhaka’
students’ whose first name starts with ‘A’
• Write a SQL QUERY to display all properties of
students’ whose last name has at least 5 letters.
Student
sid first_Name last_Name Gender city mobile no
11 Ashik Al Amin M Khulna 01212458695
21 Arif Zaman M Sylhet 01562400523

Practice Problem
32 Dipta Roy M Dhaka 01457203156
43 Fatema Farzana F Dhaka 06845201412
54 Saima Akhtar F Rajshahi 04875265489

SELECT *
FROM STUDENT
WHERE city=‘Dhaka’
sid first_Name last_Name Gender city mobile no
32 Dipta Roy M Dhaka 01457203156
43 Fatema Farzana F Dhaka 06845201412
Student
sid first_Name last_Name Gender city mobile no
11 Ashik Al Amin M Khulna 01212458695
21 Arif Zaman M Sylhet 01562400523

Practice Problem
32 Dipta Roy M Dhaka 01457203156
43 Fatema Farzana F Dhaka 06845201412
54 Saima Akhtar F Rajshahi 04875265489

• Write a SQL QUERY to show the students’


first_Name from Dhaka SELECT *
FROM STUDENT
• Write a SQL QUERY to display all attribute of WHERE last_name like ‘_ _ _ _ _%’
students’ whose first name starts with ‘A’
• Write a SQL QUERY to display all properties of
students’ whose last name has at least 5 letters.
sid first_Name last_Name Gender city mobile no
11 Ashik Al Amin M Khulna 01212458695
21 Arif Zaman M Sylhet 01562400523
32 Dipta Roy M Dhaka 01457203156
43 Fatema Farzana F Dhaka 06845201412
54 Saima Akhtar F Rajshahi 04875265489

Practice Problem • Write a SQL QUERY to show the students’


first_Name from Dhaka
SELECT first_Name
From Students
Where city = ‘Dhaka’
• UPPER()
• LOWER()
• INITCAP()

Practice Problem • Write a SQL QUERY to show the students’


first_Name from Dhaka
SELECT first_Name
From Students
Where UPPER(city) = ‘DHAKA’
sid first_Name last_Name Gender city mobile no
11 Ashik Al Amin M Khulna 01212458695
21 Arif Zaman M Sylhet 01562400523
32 Dipta Roy M Dhaka 01457203156
43 Fatema Farzana F Dhaka 06845201412
54 Saima Akhtar F Rajshahi 04875265489

Practice Problem Write a SQL QUERY to display all properties of


students’ whose first name starts with ‘A’

SELECT *
From Students
Where first_name like = ‘A%’
sid first_Name last_Name Gender city mobile no
11 Ashik Al Amin M Khulna 01212458695
21 Arif Zaman M Sylhet 01562400523
32 Dipta Roy M Dhaka 01457203156
43 Fatema Farzana F Dhaka 06845201412
54 Saima Akhtar F Rajshahi 04875265489

Practice Problem Write a SQL QUERY to display all properties of


students’ whose first name starts with ‘A’

SELECT *
From Students
Where first_name like ‘A%’
sid first_Name last_Name Gender city mobile no
11 Ashik Al Amin M Khulna 01212458695
21 Arif Zaman M Sylhet 01562400523
32 Dipta Roy M Dhaka 01457203156
43 Fatema Farzana F Dhaka 06845201412
54 Saima Akhtar F Rajshahi 04875265489

Practice Problem Write a SQL QUERY to display all properties of


students’ whose last name has at least 5 letters.
SELECT *
From Students
Where last_name like ‘_ _ _ _ _%’
sid first_Name last_Name Gender city mobile no
11 Ashik Al Amin M Khulna 01212458695
21 Arif Zaman M Sylhet 01562400523

Practice Problem
32 Dipta Roy M Dhaka 01457203156
43 Fatema Farzana F Dhaka 06845201412
54 Saima Akhtar F Rajshahi 04875265489

Write a SQL QUERY to show the students’


attribute from Sylhet, Khulna or Rajshahi

SELECT * FROM STUDENTS


WHERE CITY IN (‘Khulna’, ‘Sylhet’, ‘Rajshahi’)
sid first_Name last_Name Gender city mobile no
11 Ashik Al Amin M Khulna 01212458695
21 Arif Zaman M Sylhet 01562400523

Practice Problem
32 Dipta Roy M Dhaka 01457203156
43 Fatema Farzana F Dhaka 06845201412
54 Saima Akhtar F Rajshahi 04875265489

• Write a SQL QUERY to show the students’


first_Name from Sylhet, Khulna or Rajshahi
• Write a SQL QUERY to display all properties of
students’ whose mobile no starts with ‘014’
DISTINCT
GRADES
SELECT sid
sid course grade Marks
From GRADES
11 CSE A 70
21 CSE B 50
32 CSE A 65
43 CSE D 55
54 CSE F 20
21 MATH C 40
32 MATH A+ 90
DISTINCT
GRADES
SELECT sid
sid
From GRADES
11
21
32
43
54
21
32
DISTINCT
GRADES
SELECT DISTINCT sid
sid course grade Marks
From GRADES
11 CSE A 70
21 CSE B 50
32 CSE A 65
43 CSE D 55
54 CSE F 20
21 MATH C 40
32 MATH A+ 90
DISTINCT

SELECT DISTINCT sid


sid
From GRADES
11
21
32
43
54
AND-OR
• Like Programing Language two or more conditions may be joined
with AND/OR Operator
• AND has higher precedence over OR .
Practice Problem
• Display the sid of the students from the table :
Grade(sid, course, grade, mark)
who got more than 60 marks in CSE. sid course grade Marks
11 CSE A 70
21 CSE B 50
SELECT sid 32 CSE A 65
FROM GRADE 43 CSE D 55
54 CSE F 20
WHERE mark > 60 and course=‘CSE’
21 MATH C 40
32 MATH A+ 90
Formulas in SQL
Function Definition • It is possible to use formulas where simple
literals or column references can appear
-------------- -----------------
SELECT (formula on column name)
value1 + value2 Addition FROM …
value1 - value2 Subtraction
• Formula can be created using an infix operator
value1 * value2 Multiplication
• Any operation with NULL returns a NULL
value1 / value2 Division Value
Practice Problem
• Each row of the table
weather(Date, temp_C, hum,
rain) table contains the date,
temperature in Celsius, humidity
and rain amount respectively.
• Write a query to show the
temperature of each record in
Fahrenheit scale.
! #$%& Select date, ((9* temp_C +160)/5) AS temp_F
• Formulla: =
" ' From weather
Aggregate Grouping
• Result(sid, course, grade, mark) table contains the marks and grade of
all courses for each students.
• Write a query to find the highest mark of course CSE.
• Write a query to find the highest marks for each course.
• Result(sid, course, grade, mark)
table contains the marks and
grade of all courses for each
students.
• Write a query to find the
highest mark of course CSE.

Problem 1
SELECT MAX(mark)
FROM RESULT
WHERE course = ‘CSE’
• Result(sid, course, grade, mark)
table contains the marks and grade
of all courses for each students.
• Write a query to find the highest
mark of each course.

Problem 2
SELECT course, MAX(mark)
FROM RESULT
Group by course
Aggregate
Functions in
SQL
Interesting Problem
q How to get distinct records from the Employees
table without using distinct keyword?

• Answer: use group by


sid course grade Marks
SELECT sid 11 CSE A 70
FROM RESULT 21 CSE B 50

Group by sid 32 CSE A 65


43 CSE D 55
54 CSE F 20
sid 21 MATH C 40
11 32 MATH A+ 90
21
32
43
54
SELECT dept, MAX(Salary)
Problem FROM Employees
GROUP BY dept

id dept salary
1 it 50 dept Max(Salary)
2 hr 40 it 85
3 hr 70 hr 70
4 finance 75 finance 75
5 it 85
6 finance 65
7 hr 35 Display the dept and id of
8 it 45 highest paid employee of
9 hr 67
each department
Problem
id dept salary dept Max(Salary)
1 it 50 it 85
2 hr 40 hr 70
3 hr 70 finance 75
4 finance 75
5 it 85
SELECT dept, id
6 finance 65
FROM EMPLOYEE
7 hr 35 WHERE (dept, salary) in (SELECT dept, max(salary)
8 it 45 FROM EMPLOYEE
GROUP BY DEPT )
9 hr 67
DML Again
Delete, Update
Practice Problem
• Update the students’ grade to
UPDATE RESULT
‘A+’ who got 70 to above
SET GRADE = ‘A+’
marks.
WHERE marks>=70
Practice Problem
• Delete the students’ record
who got ‘F’ grade. DELETE FROM RESULT
WHERE grade = ‘F’
Any Questions?
Thanks!

You might also like