Kap5 More SQL
Kap5 More SQL
Kapitulli 5 (Përmbledhje)
Përmbajtja
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
▪ 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)
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
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)
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 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;
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
▪ 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
▪ 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
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;
▪ 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
▪In-line view
▪Defined in the FROM clause of an SQL query
▪ To drop a column
▪ Choose either CASCADE or RESTRICT
The ALTER Command (2)
▪ Complex SQL:
▪ Nested queries, joined tables, outer joins, aggregate functions, grouping
▪ CREATE ASSERTION
▪ Views
▪ Virtual or derived tables