SlideShare a Scribd company logo
SQL Advanced Topics 2 
Mohammad Saniee – Tarbiat 10.1 Modares University
 The EXISTS function in SQL is used to check 
whether the result of a correlated nested query is 
empty (contains no tuples) or not. 
 EXISTS and NOT EXISTS are usually used in 
conjunction with a correlated nested query. 
2 SQL - part two
 Q 16 revisited 
 Retrieve the name of each employee who has a 
dependent with the same first name and same sex 
as the employee. 
 Q16B is Q16 using EXISTS operator 
3 SQL - part two
4 SQL - part two 
 SELECT E.FNAME, E.LNAME 
 FROM EMPLOYEE AS E 
 WHERE EXISTS 
(SELECT * 
FROM DEPENDENT 
WHERE E.SSN=ESSN AND 
E.SEX=SEX AND 
E.FNAME=DEPENDENT_NAME);
 In Q16B, the nested query references the SSN, 
FNAME, and SEX attributes of the EMPLOYEE 
relation from the outer query. 
 We can think of Q16B as follows: 
◦ for each EMPLOYEE tuple, evaluate the nested query, 
which retrieves all DEPENDENT tuples with the same 
social security number, sex, and name as the 
EMPLOYEE tuple; 
◦ if at least one tuple EXISTS in the result of the nested 
query, then select that EMPLOYEE tuple. 
5 SQL - part two
 In general, EXISTS(Q) returns TRUE if there is at 
least one tuple in the result of query Q, and it 
returns FALSE otherwise. 
 NOT EXISTS(Q) returns TRUE if there are no 
tuples in the result of query Q, and it returns 
FALSE otherwise. 
6 SQL - part two
QUERY 6 
Retrieve the names of employees who have no 
dependents. 
SELECT FNAME, LNAME 
FROM EMPLOYEE 
WHERE NOT EXISTS 
7 SQL - part two 
(SELECT * 
FROM DEPENDENT 
WHERE SSN=ESSN);
 List the names of managers who have at least one 
dependent. 
SELECT FNAME, LNAME 
FROM EMPLOYEE 
WHERE EXISTS (SELECT * 
8 SQL - part two 
FROM DEPENDENT 
WHERE SSN=ESSN) 
AND 
EXISTS (SELECT * 
FROM DEPARTMENT 
WHERE SSN=MGRSSN);
 Query 3, which we used to illustrate the 
CONTAINS comparison operator, can be stated 
using EXISTS and NOT EXISTS in SQL systems. 
 There are two options. 
◦ The first is to use the well known set theory 
transformation that (S1 CONTAINS S2) is logically 
equivalent to (S2 EXCEPT S1) is empty 
9 SQL - part two
10 SQL - part two
11 SQL - part two 
SELECT FNAME, LNAME 
FROM EMPLOYEE 
WHERE NOT EXISTS 
( (SELECT PNUMBER 
FROM PROJECT 
WHERE DNUM=5) 
EXCEPT 
(SELECT PNO 
FROM WORKS_ON 
WHERE SSN=ESSN));
◦ The second option is shown as Q3B below. 
◦ Notice that we need two-level nesting in Q3B and that 
this formulation is quite a bit more complex. 
◦ However, CONTAINS is not part of SQL, and not all 
relational systems have the EXCEPT operator even 
though it is part of SQL2: 
12 SQL - part two
13 SQL - part two 
SELECT LNAME, FNAME 
FROM EMPLOYEE 
WHERE NOT EXISTS 
(SELECT * 
FROM WORKS_ON B 
WHERE (B.PNO IN 
(SELECT PNUMBER 
FROM PROJECT 
WHERE DNUM=5)) 
AND 
NOT EXISTS 
(SELECT * 
FROM WORKS_ON C 
WHERE C.ESSN=SSN AND 
C.PNO=B.PNO));
 In Q3B, the outer nested query selects any WORKS_ON 
(B) tuples whose PNO is of a project controlled by 
department 5, if there is not a WORKS_ON (C) tuple 
with the same PNO and the same SSN as that of the 
EMPLOYEE tuple under consideration in the outer 
query. 
 If no such tuple exists, we select the EMPLOYEE tuple. 
 The form of Q3B matches the following rephrasing of 
Query 3: select each employee such that there does not 
exist a project controlled by department 5 that the 
employee does not work on. 
14 SQL - part two
QUERY 17 : Retrieve the social security numbers 
of all employees who work on project number 1, 2, 
or 3. 
SELECT DISTINCT ESSN 
FROM WORKS_ON 
WHERE PNO IN (1, 2, 3); 
15 SQL - part two
 SQL allows queries that check whether a value is 
NULL—missing or undefined or not applicable. 
 However, rather than using = or ≠ to compare an 
attribute to NULL, SQL uses IS or IS NOT. 
 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. 
16 SQL - part two
Retrieve the names of all employees who do not 
have supervisors. 
Q18: 
SELECT FNAME, LNAME 
FROM EMPLOYEE 
WHERE SUPERSSN IS NULL; 
17 SQL - part two
 It is possible to rename any attribute that appears 
in the result of a query by adding the qualifier AS 
followed by the desired new name. 
 the AS construct can be used to alias both 
attribute and relation names, and it can be used in 
both the SELECT and FROM clauses. 
18 SQL - part two
For example, Q8A below shows how query Q8 
can be slightly changed to retrieve the last 
name of each employee and his or her 
supervisor, while renaming the resulting 
attribute names as EMPLOYEE_NAME and 
SUPERVISOR_NAME. 
The new names will appear as column 
headers in the query result: 
19 SQL - part two
SELECT E.LNAME AS EMPLOYEE_NAME, 
20 SQL - part two 
S.LNAME AS 
SUPERVISOR_NAME 
FROM EMPLOYEE AS E, EMPLOYEE AS S 
WHERE E.SUPERSSN=S.SSN;
◦ The concept of a joined table (or joined relation) was 
incorporated into SQL2 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. 
◦ For example, consider query Q1, which retrieves the name and 
address of every employee who works for the ‘Research’ 
department. 
◦ It may be easier first to specify the join of the EMPLOYEE and 
DEPARTMENT relations, and then to select the desired tuples and 
attributes. 
◦ This can be written in SQL2 as in Q1A: 
21 SQL - part two
SELECT FNAME, LNAME, ADDRESS 
FROM (EMPLOYEE JOIN DEPARTMENT 
22 SQL - part two 
ON DNO=DNUMBER) 
WHERE DNAME=‘Research’; 
The FROM-clause contains a single joined table. 
The attributes of such a table are all the attributes 
of the EMPLOYEE, followed by all the attributes of 
the DEPARTMENT.
◦ The concept of a joined table also allows the user to 
specify different types of join, such as NATURAL JOIN and 
various types of OUTER JOIN. 
◦ In a NATURAL JOIN on two relations R and S, no join 
condition is specified; an implicit equi-join condition for 
each pair of attributes with the same name from R and S is 
created. Each such pair of attributes is included only once 
in the resulting relation. 
◦ If the names of the join attributes are not the same in the 
base relations, it is possible to rename the attributes so that 
they match, and then to apply NATURAL JOIN. In this 
case, the AS construct can be used to rename a relation 
and all its attributes in the FROM clause. 
23 SQL - part two
 This is illustrated in Q1B, where the 
DEPARTMENT relation is renamed as DEPT and 
its attributes are renamed as DNAME, DNO (to 
match the name of the desired join attribute DNO 
in EMPLOYEE), MSSN, and MSDATE. The 
implied join condition for this NATURAL JOIN is 
EMPLOYEE.DNO = DEPT.DNO, because this is 
the only pair of attributes with the same name 
after renaming: 
24 SQL - part two
SELECT FNAME, LNAME, ADDRESS 
FROM (EMPLOYEE NATURAL JOIN 
(DEPARTMENT AS DEPT (DNAME, DNO, 
MSSN, MSDATE))) 
WHERE DNAME=‘Research; 
25 SQL - part two
◦ The default type of join in a joined table is an inner join, 
where a tuple is included in the result only if a matching 
tuple exists in the other relation. 
◦ For example, in query Q8A, only employees that have a 
supervisor are included in the result; 
◦ an EMPLOYEE tuple whose value for SUPERSSN is 
NULL is excluded. 
◦ If the user requires that all employees be included, an 
outer join must be used explicitly. 
◦ In SQL2, this is handled by explicitly specifying the 
OUTER JOIN in a joined table, as illustrated in Q8B: 
26 SQL - part two
SELECT E.LNAME AS EMPLOYEE_NAME, 
S.LNAME AS SUPERVISOR_NAME 
FROM (EMPLOYEE AS E LEFT OUTER 
27 SQL - part two 
JOIN EMPLOYEE AS S ON 
E.SUPERSSN=S.SSN);
 Join types in SQL2: 
◦ INNER JOIN (same as JOIN) 
◦ LEFT OUTER JOIN 
◦ RIGHT OUTER JOIN 
◦ FULL OUTER JOIN 
 In the latter three, the keyword OUTER may be 
omitted. 
 It is also possible to nest join specifications; that is, 
one of the tables in a join may itself be a joined 
table 
28 SQL - part two
Q2A is a different way of specifying query Q2, 
using the concept of a joined table: 
SELECT PNUMBER, DNUM, LNAME, 
ADDRESS, BDATE 
FROM ((PROJECT JOIN DEPARTMENT ON 
DNUM= DNUMBER) JOIN EMPLOYEE ON 
MGRSSN=SSN) 
29 SQL - part two 
WHERE PLOCATION=‘Stafford’;
 Aggregate functions: 
◦ COUNT, SUM, MAX, MIN, and AVG. 
 The COUNT function returns the number of tuples or 
values as specified in a query. 
 The functions SUM, MAX, MIN, and AVG are applied to a 
set or multiset of numeric values and return, respectively, 
the sum, maximum value, minimum value, and average 
(mean) of those values. 
 These functions can be used in the SELECT-clause or in 
a HAVING-clause (which we will introduce later). 
 The functions MAX and MIN can also be used with 
attributes that have nonnumeric domains if the domain 
values have a total ordering among one another 
30 SQL - part two
Find the sum of the salaries of all employees, the 
maximum salary, the minimum salary, and the 
average salary. 
SELECT SUM (SALARY), MAX (SALARY), 
MIN (SALARY), AVG (SALARY) 
31 SQL - part two 
FROM EMPLOYEE;
◦ If we want to get the preceding function values for 
employees of a specific department, say the ‘Research’ 
department we can write Query 20, where the 
EMPLOYEE tuples are restricted by the WHERE-clause 
to those employees who work for the ‘Research’ 
department. 
32 SQL - part two
 Find the sum of the salaries of all employees of the 
‘Research’ department, as well as the maximum 
salary, the minimum salary, and the average salary 
in this department. 
SELECT SUM (SALARY), MAX (SALARY), 
MIN(SALARY), AVG (SALARY) 
FROM EMPLOYEE, DEPARTMENT 
WHERE DNO=DNUMBER AND 
DNAME=‘Research’; 
33 SQL - part two
Q21: Retrieve the total number of employees in the 
company (Q21) 
34 SQL - part two 
SELECT COUNT (*) 
FROM EMPLOYEE;
Q22: Retrieve the number of employees in the 
‘Research’ department 
SELECT COUNT (*) 
FROM EMPLOYEE, DEPARTMENT 
WHERE DNO=DNUMBER AND 
35 SQL - part two 
DNAME=‘Research’; 
The asterisk (*) refers to the rows (tuples), so 
COUNT (*) returns the number of rows.
We may also use the COUNT function to count 
different values in a column rather than all tuples. 
QUERY 23: Count the number of distinct salary 
values in the database. 
SELECT COUNT (DISTINCT SALARY) 
FROM EMPLOYEE; 
36 SQL - part two
 The preceding examples show how functions are 
applied to retrieve a summary value from the 
database. 
 In some cases we may need to use functions to 
select particular tuples. 
 In such cases we specify a correlated nested 
query with the desired function, and we use that 
nested query in the WHERE-clause of an outer 
query. 
37 SQL - part two
 Q5: Retrieve the names of all employees who 
have two or more dependents. 
SELECT LNAME, FNAME 
FROM EMPLOYEE 
WHERE (SELECT COUNT (*) 
FROM DEPENDENT 
WHERE SSN=ESSN) >= 2; 
38 SQL - part two
 The correlated nested query counts the number of 
dependents that each employee has; 
 if this is greater than or equal to 2, the employee 
tuple is selected. 
39 SQL - part two
◦ In many cases we want to apply the aggregate 
functions to subgroups of tuples in a relation, based on 
some attribute values. 
◦ For example, we may want to find the average salary 
of employees in each department or the number of 
employees who work on each project. 
◦ In these cases we need to group the tuples that have 
the same value of some attribute(s), called the 
grouping attribute(s), and we need to apply the 
function to each such group independently. 
40 SQL - part two
◦ SQL has a GROUP BY-clause for this purpose. 
◦ The GROUP BY-clause specifies the grouping attributes, 
which should also appear in the SELECT-clause, so that 
the value resulting from applying each function to a 
group of tuples appears along with the value of the 
grouping attribute(s). 
41 SQL - part two
Q24: 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; 
42 SQL - part two
 In Q24, the EMPLOYEE tuples are divided into 
groups—each group having the same value for 
the grouping attribute DNO. 
 The COUNT and AVG functions are applied to 
each such group of tuples. 
 Notice that the SELECT-clause includes only the 
grouping attribute and the functions to be applied 
on each group of tuples. 
43 SQL - part two
Q25: For each project, retrieve the project number, the project 
name, and the number of employees who work on that 
project. 
SELECT PNUMBER, PNAME, COUNT (*) 
FROM PROJECT, WORKS_ON 
WHERE PNUMBER=PNO 
GROUP BY PNUMBER, PNAME; 
Q25 shows how we can use a join condition in conjunction with 
GROUP BY. 
In this case, the grouping and functions are applied after the 
joining of the two relations. 
44 SQL - part two
◦ Sometimes we want to retrieve the values of these 
functions only for groups that satisfy certain conditions. 
◦ For example, suppose that we want to modify Query 25 
so that only projects with more than two employees 
appear in the result. 
◦ SQL provides a HAVING-clause, which can appear in 
conjunction with a GROUP BY-clause, for this purpose. 
◦ HAVING provides a condition on the group of tuples 
associated with each value of the grouping attributes; 
and only the groups that satisfy the condition are 
retrieved in the result of the query. 
◦ This is illustrated by Query 26. 
45 SQL - part two
Q26: 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. 
SELECT PNUMBER, PNAME, COUNT (*) 
FROM PROJECT, WORKS_ON 
WHERE PNUMBER=PNO 
GROUP BY PNUMBER, PNAME 
HAVING COUNT (*) > 2; 
Notice that, while selection conditions in the WHERE-clause 
limit the tuples to which functions are applied, the HAVING-clause 
46 SQL - part two 
serves to choose whole groups. 
Figure 08.04(b) illustrates the use of HAVING and displays 
the result of Q26.
Q27:For each project, retrieve the project number, 
the project name, and the number of employees 
from department 5 who work on the project. 
SELECT PNUMBER, PNAME, COUNT (*) 
FROM PROJECT, WORKS_ON, EMPLOYEE 
WHERE PNUMBER=PNO AND SSN=ESSN 
47 SQL - part two 
AND DNO=5 
GROUP BY PNUMBER, PNAME;
 Here we restrict the tuples in the relation (and 
hence the tuples in each group) to those that 
satisfy the condition specified in the WHERE-clause— 
namely, that they work in department 
48 SQL - part two 
number 5.
 Notice that we must be extra careful when two 
different conditions apply 
◦ one to the function in the SELECT-clause and 
◦ another to the function in the HAVING-clause) 
49 SQL - part two
 Count the total number of employees whose 
salaries exceed $40,000 in each department, but 
only for departments where more than five 
employees work. Here, the condition (SALARY > 
40000) applies only to the COUNT function in the 
SELECT-clause. 
50 SQL - part two
 Suppose that we write the following incorrect 
query: 
SELECT DNAME, COUNT (*) 
FROM DEPARTMENT, EMPLOYEE 
WHERE DNUMBER=DNO AND 
51 SQL - part two 
SALARY>40000 
GROUP BY DNAME 
HAVING COUNT (*) > 5;
◦ This is incorrect because it will select only departments 
that have more than five employees who each earn more 
than $40,000. 
◦ The rule is that the WHERE-clause is executed first, to 
select individual tuples; the HAVING-clause is applied 
later, to select individual groups of tuples. 
◦ Hence, the tuples are already restricted to employees 
who earn more than $40,000, before the function in the 
HAVING-clause is applied. 
◦ One way to write the query correctly is to use a nested 
query, as shown in Query 28. 
52 SQL - part two
Q28: 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. 
53 SQL - part two
54 SQL - part two 
SELECT DNUMBER, COUNT (*) 
FROM DEPARTMENT, EMPLOYEE 
WHERE DNUMBER=DNO AND 
SALARY>40000 AND 
DNO IN (SELECT DNO 
FROM EMPLOYEE 
GROUP BY DNO 
HAVING COUNT (*) > 5) 
GROUP BY DNUMBER;

More Related Content

PPT
SQL querys in detail || Sql query slides
PPT
SQL || overview and detailed information about Sql
PPT
Chapter08
DOC
Best sql plsql material
DOC
Sql queries
PDF
ORACLE, SQL, PL/SQL Made very very Easy Happy Learning....
PPTX
Sql operator
PDF
Powerful Explain in MySQL 5.6
SQL querys in detail || Sql query slides
SQL || overview and detailed information about Sql
Chapter08
Best sql plsql material
Sql queries
ORACLE, SQL, PL/SQL Made very very Easy Happy Learning....
Sql operator
Powerful Explain in MySQL 5.6

What's hot (17)

DOCX
DBMS lab manual
PDF
MySQL partitions tutorial
RTF
Best sql plsql_material for B.TECH
PPT
Oracle Sql & PLSQL Complete guide
PPTX
PDF
Boost performance with MySQL 5.1 partitions
PDF
Mysql Explain Explained
PPTX
Optimizing queries MySQL
PPT
Les01-Oracle
PPTX
R programming
PDF
How to work with Subquery in Data Mining?
PPT
Oracle naveen Sql
DOCX
Learning sql from w3schools
PDF
Java script introducation & basics
 
RTF
Seistech SQL code
DBMS lab manual
MySQL partitions tutorial
Best sql plsql_material for B.TECH
Oracle Sql & PLSQL Complete guide
Boost performance with MySQL 5.1 partitions
Mysql Explain Explained
Optimizing queries MySQL
Les01-Oracle
R programming
How to work with Subquery in Data Mining?
Oracle naveen Sql
Learning sql from w3schools
Java script introducation & basics
 
Seistech SQL code
Ad

Viewers also liked (20)

DOC
Sql queries with answers
DOCX
Consultas de tablas con comando de SQL
PDF
consultas en sql server
PDF
Ejercicios sql
PPT
Bases de Datos Cap-V SQL: Manipulación de datos
PPT
Aggregating Data Using Group Functions
DOC
Complete Sql Server querries
PDF
Travel research-2015-boomer-travel-trends-infographic-aarp-res-gen
PDF
Sql tutorial, tutorials sql
PPT
Sql query [select, sub] 4
PPT
Aggregate functions
PDF
Sql wksht-5
PPT
Aggregate Functions,Final
PDF
Top 25 Coldest Cities in America
PDF
Ejercicios sql
PDF
DOC
Sql queires
ODT
Sql queries interview questions
PPT
Consultas base de datos en SQL
PPT
SQL : introduction
Sql queries with answers
Consultas de tablas con comando de SQL
consultas en sql server
Ejercicios sql
Bases de Datos Cap-V SQL: Manipulación de datos
Aggregating Data Using Group Functions
Complete Sql Server querries
Travel research-2015-boomer-travel-trends-infographic-aarp-res-gen
Sql tutorial, tutorials sql
Sql query [select, sub] 4
Aggregate functions
Sql wksht-5
Aggregate Functions,Final
Top 25 Coldest Cities in America
Ejercicios sql
Sql queires
Sql queries interview questions
Consultas base de datos en SQL
SQL : introduction
Ad

Similar to sql statement (20)

PPT
Module 3 Part I - Bk1 Chapter 07.ppt
PPT
Chapter07 database system in computer.ppt
PPT
Sql (DBMS)
PPT
Modules 1basic-sql.ppt for engineering dbms
PPT
SQL-examples.ppt
PPT
introduction of sql like you will be able to know lots of things from here ab...
PPT
SQL-examples.ppt
PPT
SQL-examples.ppt
PDF
04-Intro-SQL database management systems
PPTX
SQL-examples.pptx sql structured d query
PPT
SQL knowledge at its best ppt-pt-new.ppt
PPTX
Module 3.1.pptx
PPTX
SQL database management System a brief intro
PPT
The SQL data-definition language (DDL) allows the specification of informatio...
PPT
SQL query ppt with detailed information and examples.ppt
PPTX
More Complex SQL and Concurrency ControlModule 4.pptx
PPT
Introduction to-sql
PPT
sql ppt of nitj. Jalandhar proffersor mes shefali
PPTX
Module 3 Part I - Bk1 Chapter 07.ppt
Chapter07 database system in computer.ppt
Sql (DBMS)
Modules 1basic-sql.ppt for engineering dbms
SQL-examples.ppt
introduction of sql like you will be able to know lots of things from here ab...
SQL-examples.ppt
SQL-examples.ppt
04-Intro-SQL database management systems
SQL-examples.pptx sql structured d query
SQL knowledge at its best ppt-pt-new.ppt
Module 3.1.pptx
SQL database management System a brief intro
The SQL data-definition language (DDL) allows the specification of informatio...
SQL query ppt with detailed information and examples.ppt
More Complex SQL and Concurrency ControlModule 4.pptx
Introduction to-sql
sql ppt of nitj. Jalandhar proffersor mes shefali

Recently uploaded (20)

PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Sports Quiz easy sports quiz sports quiz
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Classroom Observation Tools for Teachers
PDF
Computing-Curriculum for Schools in Ghana
PPTX
master seminar digital applications in india
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Lesson notes of climatology university.
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Insiders guide to clinical Medicine.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Microbial diseases, their pathogenesis and prophylaxis
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Sports Quiz easy sports quiz sports quiz
Final Presentation General Medicine 03-08-2024.pptx
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Abdominal Access Techniques with Prof. Dr. R K Mishra
Classroom Observation Tools for Teachers
Computing-Curriculum for Schools in Ghana
master seminar digital applications in india
Microbial disease of the cardiovascular and lymphatic systems
2.FourierTransform-ShortQuestionswithAnswers.pdf
Lesson notes of climatology university.
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Insiders guide to clinical Medicine.pdf

sql statement

  • 1. SQL Advanced Topics 2 Mohammad Saniee – Tarbiat 10.1 Modares University
  • 2.  The EXISTS function in SQL is used to check whether the result of a correlated nested query is empty (contains no tuples) or not.  EXISTS and NOT EXISTS are usually used in conjunction with a correlated nested query. 2 SQL - part two
  • 3.  Q 16 revisited  Retrieve the name of each employee who has a dependent with the same first name and same sex as the employee.  Q16B is Q16 using EXISTS operator 3 SQL - part two
  • 4. 4 SQL - part two  SELECT E.FNAME, E.LNAME  FROM EMPLOYEE AS E  WHERE EXISTS (SELECT * FROM DEPENDENT WHERE E.SSN=ESSN AND E.SEX=SEX AND E.FNAME=DEPENDENT_NAME);
  • 5.  In Q16B, the nested query references the SSN, FNAME, and SEX attributes of the EMPLOYEE relation from the outer query.  We can think of Q16B as follows: ◦ for each EMPLOYEE tuple, evaluate the nested query, which retrieves all DEPENDENT tuples with the same social security number, sex, and name as the EMPLOYEE tuple; ◦ if at least one tuple EXISTS in the result of the nested query, then select that EMPLOYEE tuple. 5 SQL - part two
  • 6.  In general, EXISTS(Q) returns TRUE if there is at least one tuple in the result of query Q, and it returns FALSE otherwise.  NOT EXISTS(Q) returns TRUE if there are no tuples in the result of query Q, and it returns FALSE otherwise. 6 SQL - part two
  • 7. QUERY 6 Retrieve the names of employees who have no dependents. SELECT FNAME, LNAME FROM EMPLOYEE WHERE NOT EXISTS 7 SQL - part two (SELECT * FROM DEPENDENT WHERE SSN=ESSN);
  • 8.  List the names of managers who have at least one dependent. SELECT FNAME, LNAME FROM EMPLOYEE WHERE EXISTS (SELECT * 8 SQL - part two FROM DEPENDENT WHERE SSN=ESSN) AND EXISTS (SELECT * FROM DEPARTMENT WHERE SSN=MGRSSN);
  • 9.  Query 3, which we used to illustrate the CONTAINS comparison operator, can be stated using EXISTS and NOT EXISTS in SQL systems.  There are two options. ◦ The first is to use the well known set theory transformation that (S1 CONTAINS S2) is logically equivalent to (S2 EXCEPT S1) is empty 9 SQL - part two
  • 10. 10 SQL - part two
  • 11. 11 SQL - part two SELECT FNAME, LNAME FROM EMPLOYEE WHERE NOT EXISTS ( (SELECT PNUMBER FROM PROJECT WHERE DNUM=5) EXCEPT (SELECT PNO FROM WORKS_ON WHERE SSN=ESSN));
  • 12. ◦ The second option is shown as Q3B below. ◦ Notice that we need two-level nesting in Q3B and that this formulation is quite a bit more complex. ◦ However, CONTAINS is not part of SQL, and not all relational systems have the EXCEPT operator even though it is part of SQL2: 12 SQL - part two
  • 13. 13 SQL - part two SELECT LNAME, FNAME FROM EMPLOYEE WHERE NOT EXISTS (SELECT * FROM WORKS_ON B WHERE (B.PNO IN (SELECT PNUMBER FROM PROJECT WHERE DNUM=5)) AND NOT EXISTS (SELECT * FROM WORKS_ON C WHERE C.ESSN=SSN AND C.PNO=B.PNO));
  • 14.  In Q3B, the outer nested query selects any WORKS_ON (B) tuples whose PNO is of a project controlled by department 5, if there is not a WORKS_ON (C) tuple with the same PNO and the same SSN as that of the EMPLOYEE tuple under consideration in the outer query.  If no such tuple exists, we select the EMPLOYEE tuple.  The form of Q3B matches the following rephrasing of Query 3: select each employee such that there does not exist a project controlled by department 5 that the employee does not work on. 14 SQL - part two
  • 15. QUERY 17 : Retrieve the social security numbers of all employees who work on project number 1, 2, or 3. SELECT DISTINCT ESSN FROM WORKS_ON WHERE PNO IN (1, 2, 3); 15 SQL - part two
  • 16.  SQL allows queries that check whether a value is NULL—missing or undefined or not applicable.  However, rather than using = or ≠ to compare an attribute to NULL, SQL uses IS or IS NOT.  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. 16 SQL - part two
  • 17. Retrieve the names of all employees who do not have supervisors. Q18: SELECT FNAME, LNAME FROM EMPLOYEE WHERE SUPERSSN IS NULL; 17 SQL - part two
  • 18.  It is possible to rename any attribute that appears in the result of a query by adding the qualifier AS followed by the desired new name.  the AS construct can be used to alias both attribute and relation names, and it can be used in both the SELECT and FROM clauses. 18 SQL - part two
  • 19. For example, Q8A below shows how query Q8 can be slightly changed to retrieve the last name of each employee and his or her supervisor, while renaming the resulting attribute names as EMPLOYEE_NAME and SUPERVISOR_NAME. The new names will appear as column headers in the query result: 19 SQL - part two
  • 20. SELECT E.LNAME AS EMPLOYEE_NAME, 20 SQL - part two S.LNAME AS SUPERVISOR_NAME FROM EMPLOYEE AS E, EMPLOYEE AS S WHERE E.SUPERSSN=S.SSN;
  • 21. ◦ The concept of a joined table (or joined relation) was incorporated into SQL2 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. ◦ For example, consider query Q1, which retrieves the name and address of every employee who works for the ‘Research’ department. ◦ It may be easier first to specify the join of the EMPLOYEE and DEPARTMENT relations, and then to select the desired tuples and attributes. ◦ This can be written in SQL2 as in Q1A: 21 SQL - part two
  • 22. SELECT FNAME, LNAME, ADDRESS FROM (EMPLOYEE JOIN DEPARTMENT 22 SQL - part two ON DNO=DNUMBER) WHERE DNAME=‘Research’; The FROM-clause contains a single joined table. The attributes of such a table are all the attributes of the EMPLOYEE, followed by all the attributes of the DEPARTMENT.
  • 23. ◦ The concept of a joined table also allows the user to specify different types of join, such as NATURAL JOIN and various types of OUTER JOIN. ◦ In a NATURAL JOIN on two relations R and S, no join condition is specified; an implicit equi-join condition for each pair of attributes with the same name from R and S is created. Each such pair of attributes is included only once in the resulting relation. ◦ If the names of the join attributes are not the same in the base relations, it is possible to rename the attributes so that they match, and then to apply NATURAL JOIN. In this case, the AS construct can be used to rename a relation and all its attributes in the FROM clause. 23 SQL - part two
  • 24.  This is illustrated in Q1B, where the DEPARTMENT relation is renamed as DEPT and its attributes are renamed as DNAME, DNO (to match the name of the desired join attribute DNO in EMPLOYEE), MSSN, and MSDATE. The implied join condition for this NATURAL JOIN is EMPLOYEE.DNO = DEPT.DNO, because this is the only pair of attributes with the same name after renaming: 24 SQL - part two
  • 25. SELECT FNAME, LNAME, ADDRESS FROM (EMPLOYEE NATURAL JOIN (DEPARTMENT AS DEPT (DNAME, DNO, MSSN, MSDATE))) WHERE DNAME=‘Research; 25 SQL - part two
  • 26. ◦ The default type of join in a joined table is an inner join, where a tuple is included in the result only if a matching tuple exists in the other relation. ◦ For example, in query Q8A, only employees that have a supervisor are included in the result; ◦ an EMPLOYEE tuple whose value for SUPERSSN is NULL is excluded. ◦ If the user requires that all employees be included, an outer join must be used explicitly. ◦ In SQL2, this is handled by explicitly specifying the OUTER JOIN in a joined table, as illustrated in Q8B: 26 SQL - part two
  • 27. SELECT E.LNAME AS EMPLOYEE_NAME, S.LNAME AS SUPERVISOR_NAME FROM (EMPLOYEE AS E LEFT OUTER 27 SQL - part two JOIN EMPLOYEE AS S ON E.SUPERSSN=S.SSN);
  • 28.  Join types in SQL2: ◦ INNER JOIN (same as JOIN) ◦ LEFT OUTER JOIN ◦ RIGHT OUTER JOIN ◦ FULL OUTER JOIN  In the latter three, the keyword OUTER may be omitted.  It is also possible to nest join specifications; that is, one of the tables in a join may itself be a joined table 28 SQL - part two
  • 29. Q2A is a different way of specifying query Q2, using the concept of a joined table: SELECT PNUMBER, DNUM, LNAME, ADDRESS, BDATE FROM ((PROJECT JOIN DEPARTMENT ON DNUM= DNUMBER) JOIN EMPLOYEE ON MGRSSN=SSN) 29 SQL - part two WHERE PLOCATION=‘Stafford’;
  • 30.  Aggregate functions: ◦ COUNT, SUM, MAX, MIN, and AVG.  The COUNT function returns the number of tuples or values as specified in a query.  The functions SUM, MAX, MIN, and AVG are applied to a set or multiset of numeric values and return, respectively, the sum, maximum value, minimum value, and average (mean) of those values.  These functions can be used in the SELECT-clause or in a HAVING-clause (which we will introduce later).  The functions MAX and MIN can also be used with attributes that have nonnumeric domains if the domain values have a total ordering among one another 30 SQL - part two
  • 31. Find the sum of the salaries of all employees, the maximum salary, the minimum salary, and the average salary. SELECT SUM (SALARY), MAX (SALARY), MIN (SALARY), AVG (SALARY) 31 SQL - part two FROM EMPLOYEE;
  • 32. ◦ If we want to get the preceding function values for employees of a specific department, say the ‘Research’ department we can write Query 20, where the EMPLOYEE tuples are restricted by the WHERE-clause to those employees who work for the ‘Research’ department. 32 SQL - part two
  • 33.  Find the sum of the salaries of all employees of the ‘Research’ department, as well as the maximum salary, the minimum salary, and the average salary in this department. SELECT SUM (SALARY), MAX (SALARY), MIN(SALARY), AVG (SALARY) FROM EMPLOYEE, DEPARTMENT WHERE DNO=DNUMBER AND DNAME=‘Research’; 33 SQL - part two
  • 34. Q21: Retrieve the total number of employees in the company (Q21) 34 SQL - part two SELECT COUNT (*) FROM EMPLOYEE;
  • 35. Q22: Retrieve the number of employees in the ‘Research’ department SELECT COUNT (*) FROM EMPLOYEE, DEPARTMENT WHERE DNO=DNUMBER AND 35 SQL - part two DNAME=‘Research’; The asterisk (*) refers to the rows (tuples), so COUNT (*) returns the number of rows.
  • 36. We may also use the COUNT function to count different values in a column rather than all tuples. QUERY 23: Count the number of distinct salary values in the database. SELECT COUNT (DISTINCT SALARY) FROM EMPLOYEE; 36 SQL - part two
  • 37.  The preceding examples show how functions are applied to retrieve a summary value from the database.  In some cases we may need to use functions to select particular tuples.  In such cases we specify a correlated nested query with the desired function, and we use that nested query in the WHERE-clause of an outer query. 37 SQL - part two
  • 38.  Q5: Retrieve the names of all employees who have two or more dependents. SELECT LNAME, FNAME FROM EMPLOYEE WHERE (SELECT COUNT (*) FROM DEPENDENT WHERE SSN=ESSN) >= 2; 38 SQL - part two
  • 39.  The correlated nested query counts the number of dependents that each employee has;  if this is greater than or equal to 2, the employee tuple is selected. 39 SQL - part two
  • 40. ◦ In many cases we want to apply the aggregate functions to subgroups of tuples in a relation, based on some attribute values. ◦ For example, we may want to find the average salary of employees in each department or the number of employees who work on each project. ◦ In these cases we need to group the tuples that have the same value of some attribute(s), called the grouping attribute(s), and we need to apply the function to each such group independently. 40 SQL - part two
  • 41. ◦ SQL has a GROUP BY-clause for this purpose. ◦ The GROUP BY-clause specifies the grouping attributes, which should also appear in the SELECT-clause, so that the value resulting from applying each function to a group of tuples appears along with the value of the grouping attribute(s). 41 SQL - part two
  • 42. Q24: 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; 42 SQL - part two
  • 43.  In Q24, the EMPLOYEE tuples are divided into groups—each group having the same value for the grouping attribute DNO.  The COUNT and AVG functions are applied to each such group of tuples.  Notice that the SELECT-clause includes only the grouping attribute and the functions to be applied on each group of tuples. 43 SQL - part two
  • 44. Q25: For each project, retrieve the project number, the project name, and the number of employees who work on that project. SELECT PNUMBER, PNAME, COUNT (*) FROM PROJECT, WORKS_ON WHERE PNUMBER=PNO GROUP BY PNUMBER, PNAME; Q25 shows how we can use a join condition in conjunction with GROUP BY. In this case, the grouping and functions are applied after the joining of the two relations. 44 SQL - part two
  • 45. ◦ Sometimes we want to retrieve the values of these functions only for groups that satisfy certain conditions. ◦ For example, suppose that we want to modify Query 25 so that only projects with more than two employees appear in the result. ◦ SQL provides a HAVING-clause, which can appear in conjunction with a GROUP BY-clause, for this purpose. ◦ HAVING provides a condition on the group of tuples associated with each value of the grouping attributes; and only the groups that satisfy the condition are retrieved in the result of the query. ◦ This is illustrated by Query 26. 45 SQL - part two
  • 46. Q26: 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. SELECT PNUMBER, PNAME, COUNT (*) FROM PROJECT, WORKS_ON WHERE PNUMBER=PNO GROUP BY PNUMBER, PNAME HAVING COUNT (*) > 2; Notice that, while selection conditions in the WHERE-clause limit the tuples to which functions are applied, the HAVING-clause 46 SQL - part two serves to choose whole groups. Figure 08.04(b) illustrates the use of HAVING and displays the result of Q26.
  • 47. Q27:For each project, retrieve the project number, the project name, and the number of employees from department 5 who work on the project. SELECT PNUMBER, PNAME, COUNT (*) FROM PROJECT, WORKS_ON, EMPLOYEE WHERE PNUMBER=PNO AND SSN=ESSN 47 SQL - part two AND DNO=5 GROUP BY PNUMBER, PNAME;
  • 48.  Here we restrict the tuples in the relation (and hence the tuples in each group) to those that satisfy the condition specified in the WHERE-clause— namely, that they work in department 48 SQL - part two number 5.
  • 49.  Notice that we must be extra careful when two different conditions apply ◦ one to the function in the SELECT-clause and ◦ another to the function in the HAVING-clause) 49 SQL - part two
  • 50.  Count the total number of employees whose salaries exceed $40,000 in each department, but only for departments where more than five employees work. Here, the condition (SALARY > 40000) applies only to the COUNT function in the SELECT-clause. 50 SQL - part two
  • 51.  Suppose that we write the following incorrect query: SELECT DNAME, COUNT (*) FROM DEPARTMENT, EMPLOYEE WHERE DNUMBER=DNO AND 51 SQL - part two SALARY>40000 GROUP BY DNAME HAVING COUNT (*) > 5;
  • 52. ◦ This is incorrect because it will select only departments that have more than five employees who each earn more than $40,000. ◦ The rule is that the WHERE-clause is executed first, to select individual tuples; the HAVING-clause is applied later, to select individual groups of tuples. ◦ Hence, the tuples are already restricted to employees who earn more than $40,000, before the function in the HAVING-clause is applied. ◦ One way to write the query correctly is to use a nested query, as shown in Query 28. 52 SQL - part two
  • 53. Q28: 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. 53 SQL - part two
  • 54. 54 SQL - part two SELECT DNUMBER, COUNT (*) FROM DEPARTMENT, EMPLOYEE WHERE DNUMBER=DNO AND SALARY>40000 AND DNO IN (SELECT DNO FROM EMPLOYEE GROUP BY DNO HAVING COUNT (*) > 5) GROUP BY DNUMBER;