CH 7 - More SQL: Complex Queries, Triggers, Views, and Schema Modification Flashcards
How to check in SQL an attribute value is NULL?
- SQL uses the comparison operators IS or IS NOT
7.4. Discuss how each of the following constructs is used in SQL, and discuss the various options for each construct. Specify what each construct is useful for. Nested queries
- nested queries, which are complete select-from-where blocks within another SQL query. That other query is called the outer query.
- These nested queries can also appear in the WHERE clause or the FROM clause or the SELECT clause or other SQL clauses as needed
- useful when you need to get information of different subqueries and then join
- useful when existing values in the database be fetched and then used in a comparison condition
How to solve this? retrieve a PROJECT tuple if the PNUMBER value of that tuple is in a group A of tuples or group B of tuples
- Use a nested query
EXISTS and UNIQUE
- Boolean functions that return TRUE or FALSE; hence, they can be used in a WHERE clause condition
Query 6. Retrieve the names of employees who have no dependents.
SELECT Fname, Lname FROM EMPLOYEE WHERE NOT EXISTS ( SELECT * FROM DEPENDENT WHERE Ssn = Essn );
What are Explicit Sets?
It is also possible to use an explicit set of values in the WHERE clause,
WHERE Pno IN (1, 2, 3);
Query 23. Count the number of distinct salary values in the database.
SELECT COUNT (DISTINCT Salary) FROM EMPLOYEE;
7.3. Discuss how NULLs are treated in comparison operators in SQL. How are NULLs treated when aggregate functions are applied in an SQL query? How are NULLs treated if they exist in grouping attributes?
- aggregate functions: NULL values are discarded when aggregate func- tions are applied to a particular column (attribute); the only exception is for COUNT(*) because tuples instead of values are counted.
- grouping: If NULLs exist in the grouping attribute, then a separate group is created for all tuples with a NULL value in the grouping attribute. F
retrieve the names of all employees who have two or more dependents (Query 5),
SELECT Lname, Fname FROM EMPLOYEE WHERE ( SELECT COUNT (*) FROM DEPENDENT WHERE Ssn = Essn ) > = 2;
Query 24. For each department, retrieve the department number, the number of employees in the department, and their average salary.
SELECT Dno, COUNT (*), AVG (Salary) FROM EMPLOYEE
GROUP BY
Dno;
HAVING
Sometimes we want to retrieve the values of these functions only for groups that satisfy certain conditions.
WHERE and HAVING clause
. The rule is that the WHERE clause is executed first, to select individual tuples or joined tuples; the HAVING clause is applied later, to select individual groups of tuples.
For each department that has more than five employees, retrieve the department number and the number of its employees who are making more than $40,000.
SELECT COUNT (*) FROM EMPLOYEE WHERE Salary>40000 AND Dno IN ( SELECT Dno FROM EMPLOYEE GROUP BY Dno HAVING COUNT (*) > 5) GROUP BY Dno,
WITH
. The WITH clause allows a user to define a table that will only be used in a particular query;
CASE
SQL also has a CASE construct, which can be used when a value can be different based on certain conditions. This can be used in any part of an SQL query where a value is expected, including when querying, inserting or updating tuples