0% found this document useful (0 votes)
3 views

Kap5 More SQL

Chapter 5 covers advanced SQL concepts including complex retrieval queries, handling NULL values, nested queries, and the use of aggregate functions. It explains the importance of constraints, views, and various types of joins in SQL. Additionally, it discusses the EXISTS and UNIQUE functions, as well as the creation of assertions for enforcing constraints in a database.

Uploaded by

psvxdrbmxr
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Kap5 More SQL

Chapter 5 covers advanced SQL concepts including complex retrieval queries, handling NULL values, nested queries, and the use of aggregate functions. It explains the importance of constraints, views, and various types of joins in SQL. Additionally, it discusses the EXISTS and UNIQUE functions, as well as the creation of assertions for enforcing constraints in a database.

Uploaded by

psvxdrbmxr
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

More SQL

Kapitulli 5 (Përmbledhje)
Përmbajtja

▪ More Complex SQL Retrieval Queries


▪ Specifying Constraints as Assertions
▪ Views (Virtual Tables) in SQL
▪ Schema Change Statements in SQL
More Complex SQL Retrieval Queries

▪ Additional features allow users to specify more complex retrievals from


database:
▪ Nested queries, joined tables, outer joins, aggregate functions, and grouping
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
Comparisons Involving NULL
and Three-Valued Logic (2)
Kujdes! Every arithmetic operation that includes an operand with a NULL value
returns a NULL result

First Name Last Name Salary Bonus

John Smith 1000 500

Mary Smith 1000 1500

Peter White 1800 NULL

Nick Perry 1000 NULL

Nese ekzekutojme query-in:


SELECT * FROM employee WHERE bonus + salary > 1200;
Sa rekorde do jene si rezultat?
Comparisons Involving NULL
and Three-Valued Logic (3)
How are the records filtered in the WHERE clause?
Only records that evaluate to “true” in the WHERE clause are part of the query
result set. Records evaluating to “false” or “unknown” are not part of the result.

First Name Last Name Salary Bonus

John Smith 1000 500

Mary Smith 1000 1500


Comparisons Involving NULL
and Three-Valued Logic (4)

Value NOT Result AND True False Unknown

True False True True False Unknown

False True False False False False

Unknown Unknown Unknown Unknown False Unknown

OR True False Unknown

True True True True

False True False Unknown

Unknown True Unknown Unknown


Comparisons Involving NULL
and Three-Valued Logic (5)
 Nese duam te forcojme ne True and False çdo vlere atehere perdorim COALESCE()

We can sometimes avoid using “unknown” values in WHERE conditions by converting NULL
values to other values (like 0) using the COALESCE() function.
SELECT * FROM employee WHERE coalesce(bonus,0) > 200 OR salary <
1500

First Name Last Name Salary Bonus

John Smith 1000 500

Mary Smith 1000 1500

Peter White 1800 NULL


Nested Queries, Tuples,
and Set/Multiset Comparisons
▪ Nested queries
▪ Complete select-from-where blocks within WHERE clause of another query
▪ Outer query
▪ 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
Nested Queries (2)
There are a few rules that subqueries must follow −
▪ Subqueries must be enclosed within parentheses.
▪ A subquery can have only one column in the SELECT clause, unless multiple columns
are in the main query for the subquery to compare its selected columns.
▪ An ORDER BY command cannot be used in a subquery, although the main query
can use an ORDER BY. The GROUP BY command can be used to perform the same
function as the ORDER BY in a subquery.
▪ Subqueries that return more than one row can only be used with multiple value
operators such as the IN operator.
▪ The SELECT list cannot include any references to values that evaluate to a BLOB,
ARRAY, CLOB, or NCLOB.
▪ A subquery cannot be immediately enclosed in a set function.
▪ The BETWEEN operator cannot be used with a subquery. However, the BETWEEN
operator can be used within the subquery.
Nested Queries (3)
Nested Queries (4)

▪ Use tuples of values in comparisons


▪ Place them within parentheses
Nested Queries (5)

▪ Use other comparison operators to compare a single value v


▪ = ANY (or = SOME) operator
• Returns TRUE if the value v is equal to some value in the set V and is hence
equivalent to IN

▪ Other operators that can be combined with ANY (or SOME): >, >=, <, <=,
and <>
Nested Queries (6)

First of all, you can put a nested SELECT within the WHERE clause with comparison
operators or the IN, NOT IN, ANY, or ALL operators. The second group of operators
are used when your subquery returns a list of values (rather than a single value, as in
the previous example):
• The IN operator checks if a certain value is in the table returned by the subquery.
• The NOT IN operator filters out the rows corresponding to the values not present
in that table returned by a subquery.
• The ANY operator is used with comparison operators to evaluate if any of the
values returned by the subquery satisfy the condition.
• The ALL operator is also used with comparison operators to evaluate if all values
returned by the subquery satisfy the condition.
Nested Queries (7)

▪ Avoid potential errors and ambiguities


▪ Create tuple variables (aliases) for all tables referenced in SQL query
Nested Queries – Multiple Subqueries(8)
Ex. Let’s say you want to show all information about the students in the class with the highest number of students. To answer
this question, you’ll need to find the class with the maximal number of students and then define that class. Finally, you’ll need
to show information about the students in that class.
id name class_id GPA
id name subject class_id monthly_salary
1 Jack Black 3 3.45

1 Elisabeth Grey History 3 2,500


2 Daniel White 1 3.15

2 Robert Sun Literature [NULL] 2,000


3 Kathrine Star 1 3.85

3 John Churchill English 1 2,350


4 Helen Bright 2 3.10

4 Sara Parker Math 2 3,000


5 Steve May 2 2.40

id grade teacher_id number_of_students

1 10 3 21

2 11 4 25

3 12 1 28
Nested Queries – Multiple Subqueries(9)
To answer this question, you’ll need to find the class with the maximal number of students …
SELECT MAX(number_of_students)FROM classes

id name class_id GPA


id name subject class_id monthly_salary
1 Jack Black 3 3.45

1 Elisabeth Grey History 3 2,500

id grade teacher_id number_of_students

1 10 3 21
Nested Queries – Multiple Subqueries(10)
To answer this question, you’ll need to find the class with the maximal number of students and
then define that class …
SELECT id
FROM classes
WHERE number_of_students = (
SELECT MAX(number_of_students) FROM classes)

id name class_id GPA


id name subject class_id monthly_salary
1 Jack Black 3 3.45

1 Elisabeth Grey History 3 2,500

id grade teacher_id number_of_students

1 10 3 21
Nested Queries – Multiple Subqueries(11)
To answer this question, you’ll need to find the class with the maximal number of students and
then define that class. Finally, you’ll need to show information about the students in that class.
SELECT * FROM students
WHERE class_id = (
SELECT id FROM classes
WHERE number_of_students = (
SELECT MAX(number_of_students) FROM classes));
id name class_id GPA
id name subject class_id monthly_salary
1 Jack Black 3 3.45

1 Elisabeth Grey History 3 2,500

id grade teacher_id number_of_students

1 10 3 21
Nested Queries – outside of WHERE(12)
Ex. Let’s find out what subject area corresponds to the highest average teacher salary. You’ll
first need to calculate the average salary by subject, then use this table to find the maximum
average salary.
SELECT subject, MAX(salary_by_subject.avg_salary) AS max_salary
FROM (
SELECT subject, AVG(monthly_salary) AS avg_salary
FROM teachers
GROUP BY subject) salary_by_subject;

id name class_id GPA


id name subject class_id monthly_salary
1 Jack Black 3 3.45

1 Elisabeth Grey History 3 2,500

id grade teacher_id number_of_students

1 10 3 21
Correlated Nested Queries
▪ Correlated nested query
▪ Evaluated once for each tuple in the outer query
With a normal nested subquery, the inner SELECT query
runs first and executes once, returning values to be used
by the main query. A correlated subquery, however,
executes once for each candidate row considered by the
outer query. In other words, the inner query is driven by the
outer query.
NOTE : You can also use the ANY and ALL operator in a
correlated subquery.
Correlated Nested Queries(2)
Find all the employees who earn more than the average salary in their
department.
SELECT last_name, salary, department_id
FROM employees outer
WHERE salary >
(SELECT AVG(salary)
FROM employees
WHERE department_id =
outer.department_id);
The EXISTS and UNIQUE Functions in SQL

▪ EXISTS function
▪ Check whether the result of a correlated nested query is empty or not
▪ 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
The EXISTS and UNIQUE Functions in
SQL(2)
The EXISTS operator tests for existence of rows in the results set of the
subquery. If a subquery row value is found the condition is flagged TRUE and
the search does not continue in the inner query, and if it is not found then
the condition is flagged FALSE and the search continues in the inner query.
Find employees who have at least one person reporting to them.
SELECT employee_id, last_name, job_id, department_id
FROM employees outer
WHERE EXISTS (
SELECT * FROM employees
WHERE manager_id = outer.employee_id);
The EXISTS and UNIQUE Functions in
SQL(3)
The NOT EXISTS operator has the same logic as EXITS.
Find all departments that do not have any employees.
SELECT department_id, department_name
FROM departments d
WHERE NOT EXISTS (
SELECT *
FROM employees
WHERE department_id = d.department_id);
Explicit Sets and Renaming of Attributes
in SQL

▪ Can use explicit set of values in WHERE clause


▪ Use qualifier AS followed by desired new name
▪ Rename any attribute that appears in the result of a query
Explicit Sets and Renaming of Attributes
in SQL(2)

We have seen several queries with a nested query in the


WHERE clause. It is also possible to use an explicit set of
values in the WHERE clause, rather than a nested query.
Such a set is enclosed in parentheses in SQL.
Q.17 Retrieve the Social Security numbers of all employees
who work on project numbers 1, 2, or 3.
Q17: SELECT DISTINCT Essn FROM WORKS_ON WHERE Pno IN (1, 2, 3);
Joined Tables in SQL and Outer Joins

▪ 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
Joined Tables in SQL and Outer Joins
(2)
▪ Specify different types of join
▪ NATURAL JOIN
▪ Various types of OUTER JOIN
▪ NATURAL JOIN on two relations R and S
▪ No join condition specified
▪ Implicit EQUIJOIN condition for each pair of attributes with same name from R
and S
Joined Tables in SQL and Outer Joins
(3)
▪ Inner join
▪ Default type of join in a joined table
▪ Tuple is included in the result only if a matching tuple exists in the other relation
▪ LEFT OUTER JOIN
▪ Every tuple in left table must appear in result
▪ If no matching tuple
• Padded with NULL values for attributes of right table
Joined Tables in SQL and Outer Joins
(4)
▪ RIGHT OUTER JOIN
▪ Every tuple in right table must appear in result
▪ If no matching tuple
• Padded with NULL values for the attributes of left table

▪ FULL OUTER JOIN


▪ Can nest join specifications
Joined Tables in SQL and Outer Joins
(5)
Aggregate Functions in SQL

▪ Used to summarize information from multiple tuples into a single-tuple


summary
▪ Grouping
▪ Create subgroups of tuples before summarizing
▪ Built-in aggregate functions
▪ COUNT, SUM, MAX, MIN, and AVG
▪ Functions can be used in the SELECT clause or in a HAVING clause
Aggregate Functions in SQL (2)

▪ NULL values discarded when aggregate functions are applied to a


particular column
Grouping: The GROUP BY and HAVING
Clauses
▪ Partition relation into subsets of tuples
▪ Based on grouping attribute(s)
▪ Apply function to each such group independently
▪ GROUP BY clause
▪ Specifies grouping attributes
▪ If NULLs exist in grouping attribute
▪ Separate group created for all tuples with a NULL value in grouping attribute
Grouping: The GROUP BY and HAVING
Clauses (2)
▪ HAVING clause
▪ Provides a condition on the summary information
We want to count the total number of employees whose salaries
exceed $24,000 in each department, but only for departments
where more than two employees working.
SELECT Dno, COUNT (*) as TOTAL SELECT Dno, COUNT (*) as TOTAL
FROM EMPLOYEE FROM EMPLOYEE
WHERE Salary>24000 WHERE Salary>24000 AND Dno IN (
GROUP BY Dno SELECT Dno
HAVING COUNT (*) > 2; FROM EMPLOYEE
GROUP BY Dno
HAVING COUNT (*) > 2)
GROUP BY Dno;
Discussion and Summary of SQL
Queries
Specifying Constraints as Assertions

▪ CREATE ASSERTION
▪ Specify additional types of constraints outside scope of built-in
relational model constraints
▪ Specify a query that selects any tuples that violate the desired
condition
▪ Use only in cases where it is not possible to use CHECK on attributes
and domains

CREATE ASSERTION SALARY_CONSTRAINT CHECK (NOT EXISTS (


SELECT * FROM EMPLOYEE E, EMPLOYEE M, DEPARTMENT D
WHERE E.Salary>M.Salary AND
E.Dno = D.Dnumber AND
D.Mgr_ssn = M.Ssn ));
Specifying General Constraints as
Assertions in SQL (2)
CONSIDERATIONS WHEN CREATING ASSERTIONS
➢ When an assertion is created, the system tests it for validity.
➢ If the assertion is valid, any further modification to the database is allowed only if it does
not cause that assertion to be violated.
➢ This testing may result in significant overhead if the assertions are complex. Because of
this, the assert should be used with great care.
Views (Virtual Tables) in SQL

▪ Concept of a view in SQL


▪ Single table derived from other tables
▪ Considered to be a virtual table
Specification of Views in SQL

▪ CREATE VIEW command


▪ Give table name, list of attribute names, and a query to specify the contents of
the view
▪ Specify SQL queries on a view
▪ View always up-to-date
▪ Responsibility of the DBMS and not the user
▪ DROP VIEW command
▪ Dispose of a view
Specification of Views in SQL (2)
Q1:
CREATE VIEW WORKS_ON1 AS SELECT Fname, Lname, Pname, Hours
FROM EMPLOYEE, PROJECT, WORKS_ON
WHERE Ssn = Essn AND Pno = Pnumber;

SELECT * FROM WORKS_ON1;

DROP VIEW WORKS_ON1;

Q2:
CREATE VIEW DEPT_INFO(Dept_name, No_of_emps, Total_sal) AS
SELECT Dname, COUNT (*), SUM (Salary)
FROM DEPARTMENT, EMPLOYEE
WHERE Dnumber = Dno
GROUP BY Dname;

SELECT * FROM DEPT_INFO;

DROP VIEW DEPT_INFO;


View Implementation, View Update,
and Inline Views
▪ Complex problem of efficiently implementing a view for querying
▪ Query modification approach
▪ Modify view query into a query on underlying base tables
▪ Disadvantage: inefficient for views defined via complex queries that are time-
consuming to execute

SELECT Fname, Lname SELECT Fname, Lname


FROM WORKS_ON1 FROM EMPLOYEE, PROJECT, WORKS_ON
WHERE Pname = 'ProductX'; WHERE Ssn = Essn AND
Pno = Pnumber AND
Pname = 'ProductX';
View Implementation

▪ View materialization approach


▪ Physically create a temporary view table when the view is first queried
▪ Keep that table on the assumption that other queries on the view will follow
▪ Requires efficient strategy for automatically updating the view table when the
base tables are updated
View Implementation (2)

▪ Incremental update strategies


▪ DBMS determines what new tuples must be inserted, deleted, or modified in a
materialized view table

▪ The immediate update strategy updates a view as soon as the base tables are
changed;
▪ The lazy update strategy updates the view when needed by a view query;
▪ The periodic update strategy updates the view periodically (a view query may
get a result that is not up-to-date
View Update

▪ Update on a view defined on a single table without any aggregate


functions
▪ Can be mapped to an update on underlying base table
▪ View involving joins
▪ Often not possible for DBMS to determine which of the updates is intended
View Update(2)
Consideration to keep in mind
▪ Clause WITH CHECK OPTION
▪ Must be added at the end of the view definition if a view is to be updated
▪ A view with a single defining table is updatable if the view attributes
contain the primary key of the base relation, as well as all attributes
with the NOT NULL constraint that do not have default values
specified.
▪ Views defined on multiple tables using joins are generally not
updatable.
▪ Views defined using grouping and aggregate functions are not
updatable.
Inline Views

▪In-line view
▪Defined in the FROM clause of an SQL query

SELECT a2.ZIP_CODE, COUNT(a1.User_ID)


FROM
(SELECT User_ID, SUM(Score) FROM User_Score GROUP BY User_ID
HAVING SUM(Score) > 200) a1,
User_Address a2
WHERE a1.User_ID = a2.User_ID
GROUP BY a2.ZIP_CODE;
Schema Change Statements in SQL

▪ Schema evolution commands


▪ Can be done while the database is operational
▪ Does not require recompilation of the database schema
The DROP Command
▪ 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;

*If the RESTRICT option is chosen in place of CASCADE, the schema is


dropped only if it has no elements in it; otherwise, the DROP command
will not be executed.
To use the RESTRICT option, the user must first individually drop each
element in the schema, then drop the schema itself.
The ALTER 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);

▪ To drop a column
▪ Choose either CASCADE or RESTRICT
The ALTER Command (2)

▪ Change constraints specified on a table


▪ Add or drop a named constraint
Summary

▪ Complex SQL:
▪ Nested queries, joined tables, outer joins, aggregate functions, grouping
▪ CREATE ASSERTION
▪ Views
▪ Virtual or derived tables

You might also like