DBMS_Mod 4_Part1 (1)
DBMS_Mod 4_Part1 (1)
Slide 7- 3
More Complex SQL Retrieval Queries
Slide 7- 4
Comparisons Involving NULL
and Three-Valued Logic
• Meanings of NULL
• Unknown value
• Unavailable or withheld value
• Not applicable attribute
• Each individual NULL value considered to be different from every other
NULL value
• SQL uses a three-valued logic:
• TRUE, FALSE, and UNKNOWN (like Maybe)
• NULL = NULL comparison is avoided
Slide 7- 5
Comparisons Involving NULL
and Three-Valued Logic (cont’d.)
Slide 7- 6
Three-Valued
Logic
Slide 1- 6
Comparisons Involving NULL
and Three-Valued Logic (cont’d.)
• SQL allows queries that check whether an attribute value is NULL
• IS or IS NOT NULL
Slide 7- 7
• All examples discussed below refer to the COMPANY database shown
here.
Nested Queries, Tuples,
and Set/Multiset Comparisons
• Nested queries
• Complete select-from-where blocks within WHERE clause of another query
• Outer query and nested subqueries
• Comparison operator IN
• Compares value v with a set (or multiset) of values V
• Evaluates to TRUE if v is one of the elements in V
Slide 7- 8
Nested Queries (cont’d.)
SELECT [column_name ]
FROM [table_name]
WHERE expression operator
{ALL | ANY | SOME} ( subquery )
Nested Queries (cont’d.)
• EXISTS function
• Check whether the result of a correlated nested query is empty
or not. They are Boolean functions that return a TRUE or FALSE
result.
• EXISTS and NOT EXISTS
• Typically used in conjunction with a correlated nested query
• SQL function UNIQUE(Q)
• Returns TRUE if there are no duplicate tuples in the result of
query Q
USE of EXISTS
Q7:
• Joined table
• Permits users to specify a table resulting from a join
operation in the FROM clause of a query
• The FROM clause in Q1A
• Contains a single joined table. JOIN may also be called INNER
JOIN
Different Types of JOINed Tables in SQL
• Specify different types of join
• NATURAL JOIN
• Various types of OUTER JOIN (LEFT, RIGHT, FULL )
Q19: SELECT SUM (Salary), MAX (Salary), MIN (Salary), AVG (Salary)
FROM EMPLOYEE;
• The result can be presented with new names:
• HAVING clause
• Provides a condition to select or reject an entire group:
• Query 26. For each project on which more than two employees work,
retrieve the project number, the project name, and the number of
employees who work on the project.
• INCORRECT QUERY:
SELECT Dno, COUNT (*)
FROM EMPLOYEE
WHERE Salary>40000
GROUP BY Dno
HAVING COUNT (*) > 5;
Combining the WHERE and the HAVING Clause
Correct Specification of the Query:
• Note: the WHERE clause applies tuple by tuple whereas HAVING
applies to entire group of tuples
Use of CASE
• Single table derived from other tables called the defining tables
• Once a View is defined, SQL queries can use the View relation in the
FROM clause
• View is always up-to-date
• Responsibility of the DBMS and not the user
• DROP VIEW command
• Dispose of a view
Schema Change Statements in SQL
• DROP command
• Used to drop named schema elements, such as tables, domains, or constraint
• Drop behavior options:
• CASCADE and RESTRICT
• Example:
• DROP SCHEMA COMPANY CASCADE;
• This removes the schema and all its elements
including tables, views, constraints, etc.
• RESTRICT: drops only nothing in it
The ALTER table command
• Alter table actions include:
• Adding or dropping a column (attribute)
• Changing a column definition
• Adding or dropping table constraints
• Example:
• ALTER TABLE COMPANY.EMPLOYEE ADD COLUMN Job
VARCHAR(12);
Adding and Dropping Constraints
• Complex SQL:
• Nested queries, joined tables (in the FROM clause), outer joins, aggregate
functions, grouping
• Handling semantic constraints with CREATE ASSERTION and
CREATE TRIGGER
• CREATE VIEW statement
• Schema Modification for the DBAs using ALTER TABLE , ADD
and DROP COLUMN, ALTER CONSTRAINT etc.
Thankyou