Explain the different meanings of NULL with example. Why does SQL not allow to compare an attribute value to NULL using = and <> operators?
different meanings of NULL:
(* SQL doesn’t distinguish btwn these 3 meanings because it’s often not possible to determine which of the 3 meanings applies )
In addition to the IN operator, which comparison operators can be used to compare a single value v to a set or multiset V?
IN: compares a value v with a set or multiset of values V and evaluates to TRUE if v is one of the elements in V.
ex:
SELECT last_name, first_name, ssn
FROM Employee
WHERE p_no IN (
SELECT num FROM Project WHERE department=’Math’);
In addition to the IN operator, a number of other comparison operators can be used to compare a single value v (typically an attribute name) to a set or multiset v (typically a nested query).
The = 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.
The two keywords ANY and SOME have the same effect.
Other operators that can be combined with ANY and SOME include: >, >=, .
ex: #get the names of employees in New York whose salary is larger than at least one employee’s salary in San Francisco
SELECT last_name, first_name
FROM employee
WHERE
current_city=’New York’
AND salary > ANY (
SELECT salary FROM employee WHERE current_city=’San Francisco’);
The keyword ALL can also be combined with each of these attributes.
ex: #get the names of employees in New York whose salary is larger than all of the employee[‘]s[’] in San Francisco salaries
SELECT last_name, first_name
FROM employee
WHERE
current_city=’New York’
AND salary > ALL (
SELECT salary FROM employee WHERE current_city=’San Francisco’);
[nested queries: complete SELECT-FROM-WHERE blocks within another SQL query
[If nested query returns a single attribute AND a single tuple, the query result will be a single (AKA scalar) value.]
In such cases, it is permissible to use = instead of IN for the comparison operator.
[In general, the nested query will return a table, which is a multiset of tuples.]
What is the use of the EXISTS function in SQL?
EXISTS(Q) is used to check whether the result of a nested query is empty (contains no tuples) or not.
NOT EXISTS(Q):
[EXISTS, NOT EXISTS, and UNIQUE are boolean functions in SQL; can be used in a WHERE clause condition]
[UNIQUE(Q): can be used to test whether the result of a nested query is a set (no duplicates) or if it is a multiset (duplicates exist)
[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.]
a. Nested queries
nested queries: complete SELECT-FROM-WHERE blocks within another SQL query
USEFUL FOR:
Some queries require that existing tuples in the database be fetched and then used in a comparison condition.
In general, the nested query will return a table, which is a multiset of tuples.
can have several levels of nested queries
HOW ITS USED, OPTIONS:
can use operators:
IN
[[COMP OP]] SOME
[[COMP OP]] ANY (same as SOME)
[[COMP OP]] ALL
{COMP OPs: =, >, >=, }
= *if the nested query returns a single attribute AND a single tuple; query result is a single(AKA scalar) value*ex: SELECT E.first_name, E.last_name, E.ssn, P.num FROM Employee E WHERE E.p_no IN ( SELECT P.num FROM Project P WHERE P.department='math');
[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.]
b. Joined tables and outer joins
USEFUL FOR:
The concept of a joined table (AKA joined relation) was incorporated into SQL to permit users to specify a table resulting from a join operation in the FROM clause of a query. This construct may be easier to comprehend than mixing together all the select and join conditions in the WHERE clause.
HOW ITS USED, OPTIONS:
ex:
SELECT Fname, Lname, Address
FROM (EMPLOYEE JOIN DEPARTMENT ON Dno=Dnumber)
WHERE Dname=’Research’);
[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.]
c. Aggregate functions and grouping
AGGREGATE FUNCTIONS
AGGREGATE FUNCTIONS
USEFUL FOR:
HOW ITS USED, OPTIONS:
employee: ssn, email, salary (1111223333, 'user1@gt.edu', 50000), (2222334444, 'user2@gt.edu', 55000), (3333445555, 'user3@gt.edu', 55000), (4294967295, 'user4@gt.edu', 60000), (1111224444, 'user5@gt.edu', NULL), (1111225555, 'user6@gt.edu', 50000), (1111226666, 'user7@gt.edu', NULL), (1111227777, 'user8@gt.edu', 50000), (1111228888, 'user9@gt.edu', 50000)
* ex1: #get the total number of employees SELECT COUNT(*) FROM employee; #9
* ex2: #get a count of ALL salary values in employee SELECT COUNT(salary) FROM employee; #7
* ex3: #get a count of the distinct salary values in employee SELECT COUNT(DISTINCT salary) FROM employee; #3
ex4: #get the the average of all employees' salaries, the sum of all employees' salaries, the min salary, the max salary SELECT AVG(salary), SUM(salary), MIN(salary), MAX(salary) FROM employee; #52857.1429, 370000, 50000, 60000
[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.]
d. Triggers
USEFUL FOR:
HOW ITS USED, OPTIONS:
CREATE TRIGGER
BEFORE{or}AFTER
WHEN
ex: #creates a trigger called SALARY_VIOLATION; before inserting a new EMPLOYEE record or updating an EMPLOYEE’s SALARY or updating an EMPLOYEE’s SUPERVISOR, if the employee’s new salary is higher than the employee’s supervisor’s salary, then invoke the sproc INFORM_SUPERVISOR
CREATE TRIGGER SALARY_VIOLATION
BEFORE INSERT OR UPDATE OF SALARY, SUPERVISOR_SSN ON EMPLOYEE
FOR EACH ROW
WHEN (NEW.SALARY > (SELECT SALARY FROM EMPLOYEE WHERE SSN=NEW.SUPERVISOR_SSN) ) INFORM_SUPERVISOR(NEW.Supervisor_ssn, NEW.Ssn);
[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.]
e. Assertions and how they differ from triggers
USEFUL FOR:
* can be used to specify additional types of constraints that are outside the scope of the built-in relational model constraints (primary and unique keys, entity integrity, and referential integrity)
VS TRIGGERS:
HOW ITS USED, OPTIONS:
CREATE ASSERTION: can specify general constraints via declarative assertions
* CREATE ASSERTION
CHECK ();
** assertion condition must hold true on every DB state in order for the assertion to be satisfied
** any WHERE clause condition can be used
** many constraints can be specified using the EXISTS and NOT EXISTS style of SQL conditions
* DBMS is responsible for making sure that condition is not violated
* basic technique for writing assertions:
1. specify a query that selects any tuples that violate the desired condition
2. put the query inside a NOT EXISTS clause
** the assertion will specify that the result of this query must be empty so that the condition will always be true
CHECK clause and constraint condition can also be used to specify constraints on individual attrs and domains and on individual tuples
CREATE ASSERTION vs individual domain constraints vs tuple constaints:
ex: #the salary of an employee must not be greater than the salary of the manager of the department that the employee works for
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));
[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.]
f. The SQL WITH clause
USEFUL FOR:
HOW ITS USED, OPTIONS:
WITH AS ()
>;
ex: WITH BIGDEPTS (Dno) AS ( SELECT Dno FROM EMPLOYEE GROUP BY Dno HAVING COUNT(*) > 5) SELECT Dno, COUNT(*) FROM EMPLOYEE WHERE Salary > 40000 AND Dno IN BIGDEPTS GROUP BY Dno;
same as: SELECT Dno, COUNT(*) FROM EMPLOYEE WHERE Salary > 40000 AND Dno IN (SELECT Dno FROM EMPLOYEE GROUP BY Dno HAVING COUNT(*) > 5) GROUP BY Dno;
[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.]
g. SQL CASE construct
USEFUL FOR:
HOW ITS USED, OPTIONS: CASE WHEN THEN WHEN THEN WHEN THEN ELSE ;
ex: UPDATE EMPLOYEE SET Salary = CASE WHEN Dno=5 THEN Salary + 2000 WHEN Dno=4 THEN Salary + 1500 WHEN Dno=1 THEN Salary + 3000 ELSE Salary + 0;
[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.]
h. Views and their updatability
view (AKA virtual table): a single table that is derived from other tables
USEFUL FOR:
UPDATABILITY:
HOW ITS USED, OPTIONS:
CREATE VIEW ()
AS ;
ex: 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;
[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.]
i Schema change commands
USEFUL FOR:
HOW ITS USED, OPTIONS:
ex1: DROP SCHEMA COMPANY CASCADE;
ex2: DROP TABLE DEPENDENT CASCADE;
ex3: ALTER TABLE COMPANY.EMPLOYEE ADD COLUMN Job VARCHAR(12);
ex4: ALTER TABLE COMPANY.EMPLOYEE DROP COLUMN Address CASCADE;
ex5: ALTER TABLE COMPANY.DEPARTMENT ALTER COLUMN Mgr_ssn DROP DEFAULT;
ALTER TABLE COMPANY.DEPARTMENT ALTER COLUMN Mgr_ssn SET DEFAULT ‘3333445555’;
ex6: ALTER ATBLE COMPANY.EMPLOYEE DROP CONSTRAINT EMPSUPERFK CASCADE;
[Explain the different meanings of NULL with example.] Why does SQL not allow to compare an attribute value to NULL using = and <> operators?
SQL allows queries that check whether an attribute value is NULL.
Rather than using = or <> to compare an attribute value to NULL, SQL uses the comparison operators IS or IS NOT.
This is because SQL considers each NULL value as being distinct from every other NULL value, so equality comparison is not appropriate.
It follows that when a join condition is specified, tuples with NULL values for the join attributes are not included in the result (unless it is an OUTER JOIN).
ex: SELECT Fname, Lname FROM EMPLOYEE WHERE Super_ssn IS NULL;
correlated queries
correlated queries: whenever a condition in the WHERE clause of a nested query references some attribute of a relation declared in the outer query, the two queries are said to be correlated.
* we can understand a correlated query better by considering that the nested query is evaluated once for each tuple (or combination of tuples) in the other query.
ex: #Retrieve the name of each employee who has a dependent with the same first name and is the same sex as the employee.
SELECT E.Fname, E.Lname
FROM EMPLOYEE AS E
WHERE E.Ssn IN (
SELECT D.Essn
FROM DEPENDENT AS D
WHERE E.Fname = D.Dependent_name
AND E.Sex=D.Sex);
[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.]
c. Aggregate functions and grouping
GROUPING
GROUPING
USEFUL FOR:
GROUP BY
HAVING
HOW ITS USED, OPTIONS:
employee: ssn, email, salary, dept (1111223333, 'user1@gt.edu', 50000, 3), (2222334444, 'user2@gt.edu', 55000, 1), (3333445555, 'user3@gt.edu', 55000, 3), (4294967295, 'user4@gt.edu', 60000, 3), (1111224444, 'user5@gt.edu', NULL, 3), (1111225555, 'user6@gt.edu', 50000, 2), (1111226666, 'user7@gt.edu', NULL, NULL), (1111227777, 'user8@gt.edu', 50000, 2), (1111228888, 'user9@gt.edu', 50000, NULL)
department: num, d_name (1, 'human resources'), (2, 'research'), (3, 'engineering'), (4, 'marketing'), (5, 'sales')
* ex1: #get the number of employees in each department SELECT E.dept, COUNT(*) FROM employee E GROUP BY E.dept; #null, 2 #1, 1 #2, 2 #3, 4
* ex2: #get the average salary for employees in each department SELECT D.num, D.d_name, AVG(E.salary) FROM employee E INNER JOIN department D ON E.dept=D.num GROUP BY D.num; #1, human resources, 55000.0000 #2, research, 50000.0000 #3, engineering, 55000.0000
* ex3: #get the average salary for employees in each department that has at least 2 employees SELECT D.num, D.d_name, AVG(E.salary) FROM employee E INNER JOIN department D ON E.dept=D.num GROUP BY D.num HAVING COUNT(*) >= 2; #2, research, 50000.0000 #3, engineering, 55000.0000
* ex4: #get the average salary for employees whose salary is higher than 50000 in each department SELECT D.num, D.d_name, AVG(E.salary) FROM employee E INNER JOIN department D ON E.dept=D.num WHERE E.salary > 50000 GROUP BY D.num; #1, human resources, 55000.0000 #3, engineering, 57500.0000
* ex5: #get the average salary for employees whose salary is higher than 50000 in each department that has at least 2 employees SELECT D.num, D.d_name, AVG(E.salary) FROM employee E INNER JOIN department D ON E.dept=D.num WHERE E.salary > 50000 GROUP BY D.num HAVING COUNT(*) >= 2; #3, engineering, 57500.0000