Chapter 5 (Advanced SQL)
Chapter 5 (Advanced SQL)
Systems
(6th Edition)
Lectured by
Daw Honey Htun
Assistant Lecturer (Ph.D, Thesis)
Department of Computer Engineering and Information Technology
Comparison Operators
'=' (equal to),
'<>' or '!=' (not equal to),
'>' (greater than),
'<' (less than),
'>=' (greater than or equal to),
'<=' (less than or equal to), to compare two numbers.
mysql> SELECT name, price FROM products WHERE price < 1.0;
mysql> SELECT name, quantity FROM products WHERE quantity <=
2000;
mysql> SELECT name, price FROM products WHERE productCode =
'PEN';
String Pattern Matching- Like or Not Like
Can perform pattern matching using operator LIKE (or NOT LIKE)
with wildcard characters.
The wildcard '_' matches any single character; '%' matches any
number of characters (including zero).
For example,
'abc%' matches strings beginning with 'abc';
'%xyz' matches strings ending with 'xyz';
'%aaa%' matches strings containing 'aaa';
'___' matches strings containing exactly three characters; and
'a_b%' matches strings beginning with 'a', followed by any single
character, followed by 'b', followed by zero or more characters.
-- "name" begins with 'PENCIL'
mysql> SELECT name, price FROM products WHERE name LIKE
'PENCIL%';
-- "name" begins with 'P', followed by any two characters,
-- followed by space, followed by zero or more characters
mysql> SELECT name, price FROM products WHERE name LIKE
'P__ %';
Arithmetic & Logical Operators
Perform arithmetic operations on numeric fields using arithmetic
operators
+ Addition
- Subtraction
* Multiplication
/ Division
DIV Integer Division
% Modulus (Remainder)
SELECT ...
FROM tableName
WHERE criteria
ORDER BY columnA ASC|DESC, columnB ASC|DESC,
...
Limit
A SELECT query on a large database may produce many rows.
Could use the LIMIT clause to limit the number of rows displayed.
Skip
could specify the number of rows to be skipped, followed by the
number of rows to be displayed in the LIMIT clause,
-- Skip the first two rows and display the next 1 row
mysql> SELECT * FROM products ORDER BY price LIMIT 2, 1;
Alias- AS
Could use the keyword AS to define an alias for an identifier (such as
column name, table name).
The alias will be used in displaying the name.
It can also be used as reference.
For example,
Query 1: Retrieve the name and address of all employees who work
for the 'Research' department.
Slide 8-18
NESTING OF QUERIES (cont.)
The outer query select an EMPLOYEE tuple if its DNO value is in the
result of either nested query.
Slide 8-19
CORRELATED NESTED QUERIES
If a condition in the WHERE-clause of a nested query references
an attribute of a relation declared in the outer query , the two
queries are said to be correlated.
Query 12: Retrieve the name of each employee who has a
dependent with the same first name as the employee.
Slide 8-23
EXISTS FUNCTION
Slide 8-24
Query 12: Retrieve the name of each employee who has a
dependent with the same first name as the employee.
AND
Three Meanings:
Unknown value
Unavailable or withheld value
Not applicable attribute
NATURAL JOIN,
OUTER JOIN (LEFT OUTER JOIN, RIGHT OUTER JOIN, FULL OUTER
JOIN),
or as:
Slide 8-32
Nested Join ( Multiway Join)
Another Example;
Q2 could be written as follows; this illustrates multiple joins in the
joined tables.
Q2: SELECT PNUMBER, DNUM, LNAME, BDATE, ADDRESS
FROM ( PROJECT JOIN DEPARTMENT ON
DNUM=DNUMBER)
JOIN EMPLOYEE ON MGRSSN=SSN) )
WHERE PLOCATION='Stafford’;
Slide 8-33
AGGREGATE FUNCTIONS
Include COUNT, SUM, MAX, MIN, and AVG
Query 15: Find the maximum salary, the minimum salary, and the
average salary among all employees.
Some SQL implementations may not allow more than one function in
the SELECT-clause
Slide 8-34
Query 16: Find the maximum salary, the minimum salary, and
the average salary among employees who work for the
'Research' department.
AND DNAME='Research‘;
Slide 8-35
Queries 17 and 18: Retrieve the total number of employees in
the company (Q17), and the number of employees in the
'Research' department (Q18).
Each subgroup of tuples consists of the set of tuples that have the
same value for the grouping attribute(s).
Slide 8-38
Query 21: For each project, retrieve the project number,
project name, and the number of employees who work on
that project.
Slide 8-40
Query 22: For each project on which more than two
employees work , retrieve the project number, project name,
and the number of employees who work on that project.
Chapter 9-42
Assertions: An Example
“The salary of an employee must not be greater than the salary of
the manager of the department that the employee works for’’
Chapter 9-44
SQL Triggers: An Example
A trigger to compare an employee’s salary to his/her supervisor during
insert or update operations:
Chapter 9-46
Specification of Views
SQL command: CREATE VIEW
a table (view) name
a possible list of attribute names (for example, when
arithmetic operations are specified or when we want the
names to be different from the attributes in the base
relations)
a query to specify the table contents
Chapter 9-47
SQL Views: An Example
Specify a different WORKS_ON table
FROM WORKS_ON_NEW
WHERE PNAME=‘Seena’;
DROP WORKS_ON_NEW;
Chapter 9-49
View Update
Chapter 9-50
Schema Change Statements in SQL
Checks must be done to ensure that the changes don’t affect the
rest of DB and make it inconsistent.
Chapter 9-51
Drop
Used to drop named schema elements (tables, domains, constraints)
Chapter 9-52
*** If there are some elements, DROP command will not be executed.
If the base relation within a schema and all such constraints, views and
other elements that reference the table are no longer needed, both
relation and definition can be deleted by DROP TABLE command.
Chapter 9-54
To add an attribute,
Chapter 9-57
END OF CHAPTER - 5
Chapter 9-58