0% found this document useful (0 votes)
10 views44 pages

SQL Queries Basics & Advanced

SQL queries document

Uploaded by

Rohit Solanki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views44 pages

SQL Queries Basics & Advanced

SQL queries document

Uploaded by

Rohit Solanki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

Database Systems (Atzeni, Ceri, Paraboschi, Torlone)

Chapter 4: SQL

SQL queries
• SQL queries are expressed by the select statement
• Syntax:
select AttrExpr [[ as ] Alias ] {, AttrExpr [[ as ] Alias ] }
from Table [[ as ] Alias ] {, [[ as ] Alias ] }
[ where Condition ]
• The three parts of the query are usually called:
– target list
– from clause
– where clause
• The query considers the cartesian product of the tables in the
from clause, considers only the rows that satisfy the condition
in the where clause and for each row evaluates the attribute
expressions in the target list
McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999
21
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

Example database
EMPLOYEE FirstName Surname Dept Office Salary City
Mary Brown Administration 10 45 London
Charles White Production 20 36 Toulouse
Gus Green Administration 20 40 Oxford
Jackson Neri Distribution 16 45 Dover
Charles Brown Planning 14 80 London
Laurence Chen Planning 7 73 Worthing
Pauline Bradshaw Administration 75 40 Brighton
Alice Jackson Production 20 46 Toulouse

DEPARTMENT DeptName Address City


Administration Bond Street London
Production Rue Victor Hugo Toulouse
Distribution Pond Road Brighton
Planning Bond Street London
Research Sunset Street San José

McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999


22
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

Simple SQL query


• Find the salaries of employees named Brown:
select Salary as Remuneration
from Employee
where Surname = ‘Brown’

• Result:
Remuneration
45
80

McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999


23
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

* in the target list


• Find all the information relating to employees named Brown:
select *
from Employee
where Surname = ‘Brown’

• Result:

FirstName Surname Dept Office Salary City


Mary Brown Administration 10 45 London
Charles Brown Planning 14 80 London

McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999


24
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

Attribute expressions
• Find the monthly salary of the employees named White:
select Salary / 12 as MonthlySalary
from Employee
where Surname = ‘White’

• Result:
MonthlySalary
3.00

McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999


25
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

Simple join query


• Find the names of the employees and the cities in which they
work:
select Employee.FirstName, Employee.Surname,
Department.City
from Employee, Department
where Employee.Dept = Department.DeptName
FirstName Surname City
• Result: Mary Brown London
Charles White Toulouse
Gus Green London
Jackson Neri Brighton
Charles Brown London
Laurence Chen London
Pauline Bradshaw London
Alice Jackson Toulouse

McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999


26
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

Table aliases
• Find the names of the employees and the cities in which they
work (using an alias):
select FirstName, Surname, D.City
from Employee, Department D
where Dept = DeptName

FirstName Surname City


• Result: Mary Brown London
Charles White Toulouse
Gus Green London
Jackson Neri Brighton
Charles Brown London
Laurence Chen London
Pauline Bradshaw London
Alice Jackson Toulouse

McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999


27
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

Predicate conjunction
• Find the first names and surnames of the employees who work
in office number 20 of the Administration department:
select FirstName, Surname
from Employee
where Office = ‘20’ and
Dept = ‘Administration’

• Result:
FirstName Surname
Gus Green

McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999


28
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

Predicate disjunction
• Find the first names and surnames of the employees who work
in either the Administration or the Production department:
select FirstName, Surname
from Employee
where Dept = ‘Administration’ or
Dept = ‘Production’

• Result:
FirstName Surname
Mary Brown
Charles White
Gus Green
Pauline Bradshaw
Alice Jackson

McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999


29
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

Complex logical expression


• Find the first names of the employees named Brown who work
in the Administration department or the Production department:
select FirstName
from Employee
where Surname = ‘Brown’ and
(Dept = ‘Administration’ or
Dept = ‘Production’)
• Result:
FirstName
Mary

McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999


30
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

Operator like

• Find the employees with surnames that have ‘r’ as the second
letter and end in ‘n’:
select *
from Employee
where Surname like ‘_r%n’
• Result:

FirstName Surname Dept Office Salary City


Mary Brown Administration 10 45 London
Gus Green Administration 20 40 Oxford
Charles Brown Planning 14 80 London

McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999


31
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

Management of null values


• Null values may mean that:
– a value is not applicable
– a value is applicable but unknown
– it is unknown if a value is applicable or not
• SQL-89 uses a two-valued logic
– a comparison with null returns FALSE
• SQL-2 uses a three-valued logic
– a comparison with null returns UNKNOWN
• To test for null values:
Attribute is [ not ] null

McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999


32
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

Duplicates
• In relational algebra and calculus the results of queries do not
contain duplicates
• In SQL, tables may have identical rows
• Duplicates can be removed using the keyword distinct

select City select distinct City


from Department from Department
City City
London London
Toulouse Toulouse
Brighton Brighton
London San José
San José

McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999


34
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

Inner join in SQL-2


• Find the names of the employees and the cities in which they
work:
select FirstName, Surname, D.City
from Employee inner join Department as D
on Dept = DeptName

FirstName Surname City


• Result: Mary Brown London
Charles White Toulouse
Gus Green London
Jackson Neri Brighton
Charles Brown London
Laurence Chen London
Pauline Bradshaw London
Alice Jackson Toulouse

McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999


36
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

Example database, drivers and cars

DRIVER FirstName Surname DriverID


Mary Brown VR 2030020Y
Charles White PZ 1012436B
Marco Neri AP 4544442R

AUTOMOBILE CarRegNo Make Model DriverID


ABC 123 BMW 323 VR 2030020Y
DEF 456 BMW Z3 VR 2030020Y
GHI 789 Lancia Delta PZ 1012436B
BBB 421 BMW 316 MI 2020030U

McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999


37
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

Left join
• Find the drivers with their cars, including the drivers without
cars:
select FirstName, Surname, Driver.DriverID
CarRegNo, Make, Model
from Driver left join Automobile on
(Driver.DriverID = Automobile.DriverID)
• Result:
FirstName Surname DriverID CarRegNo Make Model
Mary Brown VR 2030020Y ABC 123 BMW 323
Mary Brown VR 2030020Y DEF 456 BMW Z3
Charles White PZ 1012436B GHI 789 Lancia Delta
Marco Neri AP 4544442R NULL NULL NULL

McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999


38
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

Full join
• Find all the drivers and all the cars, showing the possible
relationships between them:
select FirstName, Surname, Driver.DriverID
CarRegNo, Make, Model
from Driver full join Automobile on
(Driver.DriverID = Automobile.DriverID)
• Result:
FirstName Surname DriverID CarRegNo Make Model
Mary Brown VR 2030020Y ABC 123 BMW 323
Mary Brown VR 2030020Y DEF 456 BMW Z3
Charles White PZ 1012436B GHI 789 Lancia Delta
Marco Neri AP 4544442R NULL NULL NULL
NULL NULL NULL BBB 421 BMW 316

McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999


39
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

Table variables
• Table aliases may be interpreted as table variables
• They correspond to the renaming operator ρ of relational
algebra
• Find all the same surname (but different first names) of an
employee belonging to the Administration department:
select E1.FirstName, E1.Surname
from Employee E1, Employee E2
where E1.Surname = E2.Surname and
E1.FirstName <> E2.FirstName and
E2.Dept = ‘Administration’
• Result:
FirstName Surname
Charles Brown

McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999


40
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

Ordering
• The order by clause, at the end of the query, orders the rows
of the result; syntax:
order by OrderingAttribute [ asc | desc ]
{, OrderingAttribute [ asc | desc ] }
• Extract the content of the AUTOMOBILE table in descending order
of make and model:
select *
from Automobile
order by Make desc, Model desc
• Result: CarRegNo Make Model DriverID
GHI 789 Lancia Delta PZ 1012436B
DEF 456 BMW Z3 VR 2030020Y
ABC 123 BMW 323 VR 2030020Y
BBB 421 BMW 316 MI 2020030U
McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999
41
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

Aggregate queries
• Aggregate queries cannot be represented in relational algebra
• The result of an aggregate query depends on the consideration
of sets of rows
• SQL-2 offers five aggregate operators:
– count
– sum
– max
– min
– avg

McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999


42
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

Operator count
• count returns the number of rows or distinct values; syntax:
count(< * | [ distinct | all ] AttributeList >)
• Find the number of employees:
select count(*)
from Employee
• Find the number of different values on the attribute Salary for all
the rows in EMPLOYEE:
select count(distinct Salary)
from Employee
• Find the number of rows of EMPLOYEE having a not null value on
the attribute Salary:
select count(all Salary)
from Employee
McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999
43
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

Sum, average, maximum and minimum


• Syntax:
< sum | max | min | avg > ([ distinct | all ] AttributeExpr )

• Find the sum of the salaries of the Administration department:

select sum(Salary) as SumSalary


from Employee
where Dept = ‘Administration’

• Result: SumSalary
125

McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999


44
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

Aggregate queries with join


• Find the maximum salary among the employees who work in a
department based in London:
select max(Salary) as MaxLondonSal
from Employee, Department
where Dept = DeptName and
Department.City = ‘London’

• Result: MaxLondonSal
80

McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999


45
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

Aggregate queries and target list


• Incorrect query:
select FirstName, Surname, max(Salary)
from Employee, Department
where Dept = DeptName and
Department.City = ‘London’
• Whose name? The target list must be homogeneous

• Find the maximum and minimum salaries of all employees:


select max(Salary) as MaxSal,
min(Salary) as MinSal
from Employee
• Result: MaxSal MinSal
80 36
McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999
46
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

Group by queries
• Queries may apply aggregate operators to subsets of rows
• Find the sum of salaries of all the employees of the same
department:
select Dept, sum(Salary)as TotSal
from Employee
group by Dept

• Result: Dept TotSal


Administration 125
Distribution 45
Planning 153
Production 82

McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999


47
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

Semantics of group by queries, 1


• First, the query is executed without group by and without
aggregate operators:
select Dept, Salary
from Employee

Dept Salary
Administration 45
Production 36
Administration 40
Distribution 45
Planning 80
Planning 73
Administration 40
Production 46

McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999


48
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

Semantics of group by queries, 2


• then the query result is divided in subsets characterized by
the same values for the attributes appearing as argument of the
group by clause (in this case attribute Dept):
• Finally, the aggregate operator is applied separately to each
subset
Dept Salary
Administration 45
Administration 40 Dept TotSal
Administration 40 Administration 125
Distribution 45
Distribution 45
Planning 80 Planning 153
Planning 73 Production 82
Production 36
Production 46

McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999


49
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

Group by queries and target list


• Incorrect query:
select Office
from Employee
group by Dept
• Incorrect query:
select DeptName, count(*), D.City
from Employee E join Department D
on (E.Dept = D.DeptName)
group by DeptName
• Correct query:
select DeptName, count(*), D.City
from Employee E join Department D
on (E.Dept = D.DeptName)
group by DeptName, D.City

McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999


50
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

Group predicates
• When conditions are on the result of an aggregate operator, it is
necessary to use the having clause
• Find which departments spend more than 100 on salaries:
select Dept
from Employee
group by Dept
having sum(Salary) > 100
• Result:
Dept
Administration
Planning

McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999


51
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

where or having?

• Only predicates containing aggregate operators should appear


in the argument of the having clause

• Find the departments in which the average salary of employees


working in office number 20 is higher than 25:

select Dept
from Employee
where Office = ‘20’
group by Dept
having avg(Salary) > 25

McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999


52
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

Set queries
• A single select cannot represent unions
• Syntax:

SelectSQL { < union | intersect | except > [ all ] SelectSQL }

• Find the first names and surnames of the employees:


select FirstName as Name
from Employee
union
select Surname
from Employee
• Duplicates are removed (unless the all option is used)

McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999


54
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

Intersection
• Find the surnames of employees that are also first names:
select FirstName as Name
from Employee
intersect
select Surname
from Employee

• equivalent to:
select E1.FirstName as Name
from Employee E1, Employee E2
where E1.FirstName = E2.Surname

McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999


55
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

Difference
• Find the surnames of employees that are not also first names:
select FirstName as Name
from Employee
except
select Surname
from Employee

• Can be represented with a nested query (see later)

McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999


56
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

Nested queries
• In the where clause may appear predicates that:
– compare an attribute (or attribute expression) with the result
of an SQL query; syntax:
ScalarValue Operator < any | all > SelectSQL
• any: the predicate is true if at least one row returned by
SelectSQL satisfies the comparison
• all: the predicate is true if all the rows returned by
SelectSQL satisfy the comparison
– use the existential quantifier on an SQL query; syntax:
exists SelectSQL
• the predicate is true if SelectSQL returns a non-empty
result
• The query appearing in the where clause is called nested query

McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999


57
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

Simple nested queries, 1


• Find the employees who work in departments in London:
select FirstName, Surname
from Employee
where Dept = any (select DeptName
from Department
where City = ‘London’)

• Equivalent to (without nested query):


select FirstName, Surname
from Employee, Department D
where Dept = DeptName and
D.City = ‘London’

McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999


58
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

Simple nested queries, 2


• Find the employees of the Planning department, having the
same first name as a member of the Production department:
– without nested queries:
select E1.FirstName, E1.Surname
from Employee E1, Employee E2
where E1. FirstName = E2.FirstName and
E2.Dept = ‘Production’ and
E1.Dept = ‘Planning’
– with a nested query:
select FirstName, Surname
from Employee
where Dept = ‘Planning’ and
FirstName = any
(select FirstName
from Employee
where Dept = ‘Production’)

McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999


59
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

Negation with nested queries


• Find the departments in which there is no one named Brown:
select DeptName
from Department
where DeptName <> all (select Dept
from Employee
where Surname = ‘Brown’)
• Alternatively:
select DeptName
from Department
except
select Dept
from Employee
where Surname = ‘Brown’

McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999


60
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

Operators in and not in


• Operator in is a shorthand for = any
select FirstName, Surname
from Employee
where Dept in (select DeptName
from Department
where City = ‘London’)

• Operator not in is a shorthand for <> all


select DeptName
from Department
where DeptName not in (select Dept
from Employee
where Surname = ‘Brown’)

McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999


61
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

max and min with a nested query


• Queries using the aggregate operators max and min can be
expressed with nested queries
• Find the department of the employee earning the highest salary
– with max:
select Dept
from Employee
where Salary in (select max(Salary)
from Employee
– with a nested query:
select Dept
from Employee
where Salary >= all (select Salary
from Employee
McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999
62
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

Complex nested queries, 1


• The nested query may use variables of the external query
(‘transfer of bindings’)
• Semantics: the nested query is evaluated for each row of the
external query
• Find all the homonyms, i.e., persons who have the same first
name and surname, but different tax codes:
select *
from Person P
where exists (select *
from Person P1
where P1.FirstName = P.FirstName
and P1.Surname = P.Surname
and P1.TaxCode <> P.TaxCode)

McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999


63
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

Complex nested queries, 2


• Find all the persons who do not have homonyms:
select *
from Person P
where not exists
(select *
from Person P1
where P1.FirstName = P.FirstName
and P1.Surname = P.Surname
and P1.TaxCode <> P.TaxCode)

McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999


64
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

Tuple constructor
• The comparison with the nested query may involve more than
one attribute
• The attributes must be enclosed within a pair of curved brackets
(tuple constructor)
• The previous query can be expressed in this way:
select *
from Person P
where (FirstName,Surname) not in
(select FirstName, Surname
from Person P1
where P1.TaxCode <> P.TaxCode)

McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999


65
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

Comments on nested queries


• The use of nested queries may produce ‘less declarative’
queries, but they often improve readability
• Complex queries can become very difficult to understand
• The use of variables must respect visibility rules
– a variable can be used only within the query where it is
defined or within a query that is recursively nested in the
query where it is defined

McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999


66
Database Systems (Atzeni, Ceri, Paraboschi, Torlone)
Chapter 4: SQL

Scope of variables
• Incorrect query:
select *
from Employee
where Dept in
(select DeptName
from Department D1
where DeptName = ‘Production’) or
Dept in (select DeptName
from Department D2
where D2.City = D1.City)
• The query is incorrect because variable D1 is not visible in the
second nested query

McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999


67

You might also like