Data Manipulation (Part - I)
Data Manipulation (Part - I)
Week 4
National College of Ireland
Dublin, Ireland.
– it is non-procedural
– it is essentially free-format
5
Objectives of SQL
• SQL is a transform-oriented language with two major components:
ii. A DML (Data Manipulation Language) for retrieving and updating data.
For example, INSERT, UPDATE and SELECT Statements.
• Until SQL: 1999, SQL did not contain flow of control commands. These
had to be implemented using a programming or job-control language,
or interactively by the decisions of user. 6
Objectives of SQL
mysql> CREATE Schema/ Database TestingSQL
• Consists of standard English words:
1) CREATE TABLE Staff ( Sno VARCHAR(5),
fName VARCHAR(15),
lName VARCHAR(15),
StartDate DATE,
salary DECIMAL(7,2),
Primary Key (Sno));
2) INSERT INTO Staff VALUES ('SG16', ‘Peter’, 'Brown', ‘2000-01-17’, 8300);
3) SELECT Sno, lName, salary FROM Staff
7
WHERE salary > 10000;
Datatypes in SQL
• MySQL supports a number of SQL
standard data types in various
categories. Following Data types are
based on MySQL community server
5.6.
LONGBLOB
1. String
2. Numeric
3. DATETIME
8
Date
Datatypes
• The exact numeric data type is used to define numbers with an exact
representation.
• The precision gives the total number of significant decimal digits; that
is, the total number of digits, including decimal places but excluding
the point itself.
• Example, the exact numeric value −12.345 has precision 5 and scale 3.
• We can store images in MySQL using the data type blob, however, it is
not recommended to do this. 9
Writing SQL Commands
SQL statement consists of reserved words and user-defined words.
– Reserved words are a fixed part of SQL and must be spelt exactly
as required and cannot be split across lines.
• Most components of an SQL statement are case insensitive, except for literal
character data. It is more readable with indentation and lineation:
– If the clause has several parts, should each appear on a separate line and
be indented under start of clause. 10
Writing SQL Commands
• Use extended form of Backus Naur Form (BNF) notation:
– Upper-case letters represent reserved words.
– Lower-case letters represent user-defined words.
– | indicates a choice among alternatives.
– Curly braces ({ }) indicate a required element.
– Square brackets ([ ]) indicate an optional element.
– … indicates optional repetition (0 or more).
clause:
18
2. Range Search Condition
List all staff with a salary between 20,000 and 30,000.
SELECT Sno, fName, lName, position, salary
FROM Staff
WHERE salary BETWEEN 20000 AND 30000;
19
2. Range Search Condition
21
3. Set Membership
• Prefix match query means that the data starting with a particular value, (“Ad%”)
• Postfix match query means that the data ending with a particular value (“%lly”)24
4. Pattern Matching
• For example: Address LIKE 'H%' means the first character
must be H, but the rest of the string can be anything.
• Address LIKE 'H_ _ _' means that there must be exactly four
characters in the string, the first of which must be an H.
• DISTINCT has no effect with MIN/ MAX, but may have with SUM/
AVG. 29
SELECT Statement - Aggregates
31
Use of COUNT(DISTINCT)
33
http://dev.mysql.com/doc/refman/5.7/e
n/group-by-functions.html
Use of MIN, MAX, AVG
34
SELECT Statement - Grouping
37
Restricted Groupings
HAVING clause
• HAVING clause is designed for use with GROUP
BY to restrict groups that appear in the final result
table.
List all staff whose salary is greater than the average salary, and show by
how much.
SELECT Sno, fName, lName, position,
Salary - (SELECT AVG(salary) FROM Staff) As SalDiff
FROM Staff
WHERE salary >
(SELECT AVG(salary)
FROM Staff);
43
Subquery with Aggregate
• Instead, use subquery to find average salary (17000), and then use
outer SELECT to find those staff with salary greater than this:
44
Review Questions
45
Module References/ Resources
Recommended Book Resources Dr. Muhammad M Iqbal*
• Thomas Connolly, Carolyn Begg 2014, Database Systems: A Practical Approach
to Design, Implementation, and Management, 6th Edition Ed., Pearson
Education [ISBN: 1292061189] [Present in our Library]
Supplementary Book Resources
• Gordon S. Linoff, Data Analysis Using SQL and Excel, Wiley [ISBN:
0470099518]
• Eric Redmond, Jim Wilson, Seven Databases in Seven Weeks, Pragmatic
Bookshelf [ISBN: 1934356921]
• Baron Schwartz, Peter Zaitsev, Vadim Tkachenko, High Performance MySQL,
O'Reilly Media [ISBN: 1449314287]
Other Resources
• Website: http://www.thearling.com
• Website: http://www.mongodb.org
46
• Website: http://www.mysql.com